Thursday, July 9, 2009

What is a function call by value and call by reference in C++?

Call by reference passes the address of the variable.


Call by value makes a copy of the variable.





If you change the value in the variable when passing by reference you will also change the value in the variable in the previous function.





If you change the value in the variable when passing by value the value in the variable in the previous function will remain the same.





Example code is included so you can see the difference.





#include %26lt;iostream%26gt;


using namespace std;





void call_by_value(int x,int y);


void call_by_reference (int %26amp;x,int %26amp;y);


int main()


{


int a=1;


int b=2;


call_by_value(a,b);


cout%26lt;%26lt;a%26lt;%26lt;' '%26lt;%26lt;b%26lt;%26lt;endl; //a is still 1 and b is still 2





call_by_reference(a,b);


cout%26lt;%26lt;a%26lt;%26lt;' '%26lt;%26lt;b%26lt;%26lt;endl; //a is now 5 and y is now 3


return 0;


}





void call_by_value(int x,int y)


{


x=5;


y=3;


}


void call_by_reference (int %26amp;x,int %26amp;y)


{


x=5;


y=3;


}

What is a function call by value and call by reference in C++?
call by reference is the instance variable numX. call by value is what numX contains 54


How to pass two dimension array to function(by reference in c++)?

arrays are always passed by reference in c++ (so just pass the array like you would an int and it is passed by reference)

How to pass two dimension array to function(by reference in c++)?
void func_for_array(type** array, int size, int size2)


{


};





void main()


{


type array[10][10];


func_for_array(array,10,10);


};


Pass by reference functions in C++?

write a pass by reference functions for 3 courses u take


the course name (without spaces)


number of creds


grade value (decimal number)





so far i got the value ret function right but im stuck my code so far is





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;string%26gt;


using namespace std;


//fn prototype for passing num of students in class


void DisplayStudent(double num, double answer);


void DisplayName(coursename%26amp;);














int main ()


{


double num,answer;


cout%26lt;%26lt;"Enter the total number of students in each class \n";


cin%26gt;%26gt;num;


answer=num;


cout%26lt;%26lt;"What course you want to take\n":


cin%26gt;%26gt;name;


//function call


DisplayStudent(answer,num);


DisplayName(coursename%26amp;)


return 0;


}


//function goes here


void DisplayStudent(double num,double answer)


{


cout%26lt;%26lt;" the total number of students="%26lt;%26lt;answer;


}


voidDisplayName(coursename%26amp;)


{ cout%26lt;%26lt;"what course"%26lt;%26lt;answer;


cin%26gt;%26gt;course;


}





if u can help much appreciated !!!!!!!!!!

Pass by reference functions in C++?
#include %26lt;iostream%26gt;





using namespace std;





int calculateGPA(double, double);





void main ()


{





char[] course;


double totalCredits = 0.0;


double totalPoints = 0.0;


double hours, grade;


int continue = 1;





//1 equates to true in C or C++, 0 equates to false


while(continue)


{


cout%26lt;%26lt;"Enter class: ";


cin%26gt;%26gt;course;


cout%26lt;%26lt;"\nEnter credit hours: ";


cin%26gt;%26gt;(double)hours;


cout%26lt;%26lt;"\nEnter course grade: ";


cin%26gt;%26gt;(double)grade;





//add to total credits for GPA calculation


totalCredits += hours;


//this is tricky, I know this calculation because Im a student


//you should know this to from report cards


totalPoints += (grade * hours);





cout%26lt;%26lt;"\nEnter 1 to add another class or 0 to quit";


cin%26gt;%26gt;(int)continue;





}//end while





//once user quits entering info, calculate statistics


cout %26lt;%26lt; "GPA = " %26lt;%26lt; calculateGPA(totalPoints, totalCredits);





}//end main








int calculateGPA(totalPoints, totalCredits)


{


return (int)(totalPoints/totalCredits);


}
Reply:This should be your function prototype:





void DisplayName( string %26amp;courseName );





Keep at it!

poppy

Difference between call by value and call by reference in C?

Other answers look good, I thought I would add with some C code to further illustrate. In the below examples, the "changeit" function changes the value of the variable passed to it. Note that in pass by reference, we add the "*" indirection operator that says the changeit function does not accept an integer, but a pointer to an integer. And when we call the function, we do not pass i, but we pass %26amp;i, the address of i. The end result is that when we PASS BY VALUE the variable in the calling function is NOT changed, because it was passed by value (that is, it was copied) to the function, and when the function says i=99 it is ONLY changing its own local copy, while the i back in main() stays untouched. In CALL BY REFERENCE we are not making a copy, but actually passing a pointer, the actual memory address where the variable lives, so now if the function changes it, we are changing the actual variable. Look at these two example programs below. The first one will print out i=1 and i=1, while the second one will print i=1 and i=99.





// CALL BY VALUE


#include %26lt;stdio.h%26gt;


void changeit(int);


void main()


{


int i=1;


printf("i = %d\n",i);


changeit(i);


printf("i = %d\n",i);


}





void changeit(int i)


{


i=99;


}





// CALL BY REFERENCE


#include %26lt;stdio.h%26gt;


void changeit(int *);


void main()


{


int i=1;


printf("i = %d\n",i);


changeit(%26amp;i);


printf("i = %d\n",i);


}





void changeit(int *i)


{


*i=99;


}

Difference between call by value and call by reference in C?
CALL BY VALUE= In call by value, a copy of the variable is made and passed to the function as argument.By doing this changes made to the parameter of the function have no effects on the variable in the calling function, because the changes are made only to the copys.





CALL BY REFERENCE= Is a method in which the address of each argument is passed to the function, by doing this the changes made to the parameter of the function will affect the variable in the calling function.
Reply:in call by value if ur swapping values then they will not be swapped because the actual value of the identifier will be considered


in call by reference if ur swapping values then they will be swapped because the address of the identifier will be considered.


Explain call by value and call by reference in c++?

Call by value:- Passing only the values so that only copy of the value is sent to functions


Eg:


int main()


{


int a=10,b=20,c;


c=add(a,b);


cout%26lt;%26lt;c;


return 0;


}


int add(int x,int y) // copy of values ie a,b is received here


{


int z;


z=x+y;


return x;


}


call by Reference:


the address of the value is passed and since address is passed the original value will change since address is modified


Eg:


int main()


{


int a=10,b=20,c;


c=add(%26amp;a,%26amp;b); // address is passed


cout%26lt;%26lt;c;


return 0;


}


int add(int *x,int *y) // address locatin is received


{


int z;


z=x+y;


return x;


}


is it ok!


Or Wanted more

Explain call by value and call by reference in c++?
in call by value the actual parameters are mapped to the formal parameters an dany changes made to the formal parameters will not be affected to the actual parametrers bcoz we are just copying the parameters


but in call by reference any changes made to the formal parameters will effect the actual parameters bcoz in call by reference the address is passed as a parameters
Reply:In call by value the values are called in the subroutine which are in the main program by saving them in a memory called stack which stores the values used in the main program %26amp; by using appropriate commands we can access them in the subroutine.


In call by reference, the address of the variable is used in the subroutine to access them.
Reply:For all your C++ doubts refer


http://cetus-links.org/oo_c_plus_plus.ht...
Reply:in call by value actual parameters are passed. so a new memory area is created for the passed parameters in local memory(stack). so the actual parameters are not modified here.





in call by reference we pass address of the variables. so the parameter passed is referenced to the original variable. so any changes made inside the function reflects in change of original variable as memory area is same


What is " const reference" in C++? How can I use it?

I'm a beginner. Can anybody help me?

What is " const reference" in C++? How can I use it?
Do you mean like a const pointer or const variable?


The 'const' keyword tells the compiler that the variable following the 'const' will not change.


It is a safety measure for your sake. If you mistakingly try to modify a 'const' variable in your program, the compiler will issue an error when you compile the code.





Examples:


const char a=15; // constant char


const char hello[]="hello!"; // constant string


const char *bah=%26amp;a; // constant pointer to a
Reply:I mean a const reference in functions, for example : ErrorCode Stack%26lt;Entry%26gt;::push(const Entry %26amp;item); after 3 days thinking, finally, I get the answer, const reference is 50% like value reference and the rest 50%, it's like a reference Report It



Compare the outputs for call by value,call by reference andcall by address wuthin a single program in c++?

comparing outputs for call by value, call by address and call by reference with in single program in c++

Compare the outputs for call by value,call by reference andcall by address wuthin a single program in c++?
hopefully this will help...





whether you call by value (e.g. myFunction(int x)), call by reference (e.g. myFunction(int%26amp; x)) or call by address (e.g. myFunction(int *x)) will be determined by the code before and after the actual function call (i.e. the software design).





Without knowing the design or more detail of your question, the outputs can be engineered identically. Consider this a cat-skinning issue and I await your further questions...

cosmos