pass parameters in C++

Two ways of passing parameters by reference in C++
..................................................
Passing parameters by reference is useful when you want the values of variables to be passed can be modified by the function in which the values are passed to. In C++ you have two options to pass the parameters:
1. Use * operator
Example:
int sum(int *a,int *b){

*a=100; //a is modified
*b=150; //b is modified
int s=*a+(*b);
return(s);

}
From main function you can call the function by the following lines of code:
int a=10;
int b=20;
cout<<"a+b="<<sum(&a,&b)<<"\n";

1. Use & operator
Example:
int sum(int &a,int &b){

a=100; //a is modified
b=150; //b is modified
int s=a+b;
return(s);

}

From main function you can call the function by the following lines of code:
int a=10;
int b=20;
cout<<"a+b="<<sum(a,b)<<"\n";


Posted by: khan breu | post date: 11-28-2012 | Subject: C++ Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.