urgent. please answer

I had a problem in understanding the algorithm of this program.Can you tell how this program is working ??

void fungsi(int a, int &b,int &c)
{
int y=1;
a=a*b++;
c=b+y;
cout<<a<<b<<c<<y;
}

main(){
int a=2,b=3,c=3,y=2;
fungsi(a+b,c,y);
cout<<a<<b<<c<<y;
fungsi(b,a,c);
cout<<a<<b<<c<<y;
}
The first thing to notice is that function fungsi() receives three parameters. The first is passed by value (meaning the function receives a copy of the argument). The other two are passed by reference - meaning the variable within the function is an alias of the variable which was passed, and thus it may change the original variable.

See Arguments passed by value and by reference
http://www.cplusplus.com/doc/tutorial/functions/

The last thing to note is that it is the position of the parameter which matters
 
void fungsi(int a, int &b,int &c)
not the name. That is to say, the variable names a, b, c, y in function fungsi() are not the same as the variables a, b, c, y in main().

I suggest you take a pencil and paper and work through the code line by line, one step at a time to keep track of the value of each variable and its current value.

Last edited on
Topic archived. No new replies allowed.