# Thread: A function question

1. ## A function question

Hello guys, I am utterly confused about functions. Neither, my professor or the text book did a good job of explaining the exact structure and happenings of function paramaters. On a recent test I had a pece of code that looked like the one I posted below this. I got the question wrong and when I compiled it at home the value of the final variables was a=7, b=1, c=8.

Could someone please explain to me how this works, and maybe a method of tracing the variables?

thanks a bunch,

axon

Code:
```#include <iostream>
using namespace std;

int a;
int b;
int c;

void c1(int b, int a)
{
a+=1;
b+=1;
c+=1;
}

void c2(int b, int& a)
{
a+=3;
b+=3;
c+=3;
}

void c3(int c, int& b)
{
a+=3;
b+=3;
c+=3;
}

int main()
{
a=b=c=1;

c1(a, b);
c2(b, c);
c3(c, a);

cout << "the value of the variables is: ";
cout << a << " and " << b << " and " << c <<endl;

return 0;

}```

2. void Func(int Var)

Here, a copy of the parameter is created, thus not changing the original variable that was passed to it.

void Func(int& Var)

Here, a reference to the variable is passed, so any changes to this variable will change the one that was passed.

3. First of all, u are using global variables and the value of a global variable altered in any function is reflected back in all other functions. However, the local variables a,b, and c are also referred.

Try tracing the program with F7 if you are using a TC++ or Borland C++ compiler and add the watches for a,b,c,::a,::b,::c.

You will notice the problem.

4. Random Question:

What does "namespacestd;" do?

5. Oh, I think I'm starting to understand now. But the confusing part is that lets say the calling funtion says c1(a, c), and the function parameters are c1(int& b, int& a)...does the value of c stay the same, or does it change?

As for the namespace std question: namespace std is a collection of name definitions.

axon

6. The namespace keyword allows you to partition the global namespace consisting of huge numbers of variable names, class names and function names into declarative regions, thus allowing a name used in one library to be used by another library. This reduces collisions and enhances scope definition. in the std namespace, the C++ library is defined.

if the using namespace statement was not used , the members of a namespace would have to be referred everytime by the scope resolution op. however specifying the namespace name to be accessed by the using statement

Code:
`e.g using namespace std`
brings all or specific members within the specified namesopace into the current namespace so that they can be used without qualification.

For more details, consult a book...

7. The variable, c in the calling module is referenced as variable,a in the called module, and if a changes in the called module, c will change.

8. ## REFERENCES

does the value of c stay the same, or does it change?
It looks like you're supposed to be learning references (i.e. passing by reference) When you use the & symbol, you are using a "reference" .

The use of references and pointers are the ways to make a function affect more than one variable. YES, you CAN change the original variable. The "normal" way of passing by value creates a new local variable (sometimes with the same name) and the function may "return" only ONE value.

A common exercise demonstrating the use of references (or pointers) is to create a function that "swaps" the values of A & B.

Note that the variable names inside the function may be different than the variable names outside the function. But the reference still "refers" to the original variable.

This stuff can seem really confusing at first, but it will become almost second-nature soon!

[EDIT]
If your book does not explain this stuff clearly, and if you have an "extra" \$30.00 or so, I recommend "Teach Yourself C++ In 21 Days" by Jesse Liberty. He explains the basics very clearly. (I don't remember if he addresses "using namespase std")

9. ## Re: REFERENCES

This stuff can seem really confusing at first, but it will become almost second-nature soon! [/B]
I hope so!!

axon

10. Learning how to pass functions by value and reference and tracing are important. Catch on to it quickly- you will find college/univ instructors will expect you to know it. A lot of good test question come from this concept as well.

Popular pages Recent additions