0% found this document useful (0 votes)
2 views

Passing Arguments

The document explains how to pass arguments to functions in C++, detailing three methods: call by value, call by address, and call by reference. It describes how each method works, including examples of swapping values using these techniques. Additionally, it poses questions and provides examples of functions that calculate factorials, check for prime numbers, and find maximum values using references.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Passing Arguments

The document explains how to pass arguments to functions in C++, detailing three methods: call by value, call by address, and call by reference. It describes how each method works, including examples of swapping values using these techniques. Additionally, it poses questions and provides examples of functions that calculate factorials, check for prime numbers, and find maximum values using references.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Function 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(pass by value)


2. Call by address/pointer(pass by address)
3. Call by reference

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:

// function definition to swap the values.


void swap(int x, int y) {
int temp;
temp = x; /* save the value of x */
x = y; /* put y into x */
y = temp; /* put x into y */
}
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.


swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}

2. Call by address/pointer(pass by address)


 In call by address, instead of passing values, addresses are passed.
 Function operates on address rather than values.
 Here the formal arguments are pointers to the actual arguments.
 Hence changes made in the arguments are permanent.
Example:

// function definition to swap the values.


void swap(int *x, int *y) {
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put x into y */

}
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;

swap(&a, &b);

cout << "After swap, value of a :" << a << endl;


cout << "After swap, value of b :" << b << endl;

return 0;
}

3. Call by reference

Question: What is reference variable?


Answer:

 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.

// function definition to swap the values.


void swap(int &x, int &y) {
int temp;
temp = x;
x = y;
y = temp;

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);

cout << "After swap, value of a :" << a << endl;


cout << "After swap, value of b :" << b << endl;

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.

void add(int &x,int &y)


{
int z;
z=x+y;
cout<<"Sum="<<z;
}

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;

Return reference to calling function

#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;
}

You might also like