Passing Arguments
Passing Arguments
If a function is to use arguments, it must declare variables that accept the values of the arguments.
These variables are called the formal parameters of the function.
The formal parameters behave like other local variables inside the function and are created upon
entry into the function and destroyed upon exit.
While calling a function, there are three ways/methods that arguments can be passed to a function –
1. Call by value-
In call by value, value of actual argument is passes to the formal argument and
operation is done on the formal arguments. Any change in the formal argument does
not affect the actual argument because formal arguments are photocopy of the
actual arguments.
Hence, when function is called by call by value method, it does not affect the actual
contents of the actual arguments.
Changes made in the formal argument are local to the block of called function. Once
control returns back to the calling function the change made vanish.
By default, C++ uses call by value to pass arguments.
Example:
}
int main () {
// local variable declaration:
int a = 100;
int b = 200;
swap(&a, &b);
return 0;
}
3. Call by reference
In C++ reference type/variable, declared with “&” operator are nearly identical but not
exactly same to pointer type.
A reference variable provides an alias for a previously defined variable i.e the same
variable’s value can be used by two different names; the original name and alias
name.
A reference variable must be initialized at the time of declaration.
A reference variable is an alias i.e another name for an already existing
variable
Eg:
int k = 0;
int &kk=k; //kk is an alias for k
kk = 10; //any operation on kk will give the same result as operations on k.
Call by reference copies the reference of an argument into the formal parameter. Inside the
function, the reference is used to access the actual argument used in the call. This means that
changes made to the formal parameter affect the actual argument.
int main () {
// local variable declaration:
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
/* calling a function to swap the values using variable reference.*/
swap(a, b);
return 0;
}
Q: Write a member function in C++ which accepts one number as a parameter/argument and
calculate the factorial of given number.
Q: Write a member function in C++ which accepts one number as a parameter/argument and
Check Whether a Number is Prime or Not.
Q: Write a member function in C++ which accepts one number as a parameter/argument and
calculate the factorial of given number and return the calculated factorial to the calling function.
Q: Write a function in C++ which accepts one number as a parameter/argument and return the
reverse number of the given number to the calling function.
Q: Write a C++ program to add two integer values passed as reference to the function.
main()
{
int a,b,s=0;
system("CLS");
cout<<"Enter the two numbers:";
cin>>a>>b;
add(a,b);
cout<<endl<<"sum=\t"<<s;
return 0;
#include<iostream>
using namespace std;
int &add(int x,int y)
{
int z=x+y;
int &r=z;
return r;
}
main()
{
int a,b,s=0;
system("CLS");
cout<<"Enter the two numbers:";
cin>>a>>b;
s=add(a,b);
cout<<endl<<"sum=\t"<<s;
return 0;
}
Q: Write a function using reference variable as argument to change the values of pair of integer.
Q: Write a C++ function to find the maximum value returned as reference, among integer values
passed as reference to the function.
#include<iostream>
using namespace std;
int &getMax(int &x,int &y)
{
int m;
if(x>y)
m=x;
else
m=y;
int &rs=m;
return rs;
}
main()
{
int a=120,b=20,rs;
rs=getMax(a,b);
cout<<endl<<"Maximum number="<<rs;
return 0;
}