Sei sulla pagina 1di 16

# 1

2

## 3 ways to pass arguments to function

– Pass-by-value
– Pass-by-reference with reference
arguments (pass by reference)
– Pass-by-reference with pointer arguments

3

## Calling Functions by Reference

• Call by reference
– Allows you to change actual location in
memory
– Pass address of argument using &
operator

4

## • Arguments passed to function using reference

arguments
– Modify original values of arguments
– More than one value “returned”

5

Example (1)

## • The following pass by value program can be written using

A) A Pass by Value Program

## int sum(int x, int y)

{
return x+y;
}

void main()
{
cout<<sum(2,3); //5 is displayed
}
6

## B) A Pass by Reference Program

void sum(int x, int y, int& result)
{
result= x+y;
}

void main()
{
int x;
sum(2,3,x);
cout<<x; //5 is displayed
}

7

## • The same program can be rewritten using pass by address.

All 3 programs display the same value: 5
C) A Pass by Address Program

## void sum(int x, int y, int *result)

{
*result= x+y;
}

void main()
{
int x;
sum(2,3,&x);
cout<<x; //5 is displayed
}

8

Example (2)

## A) A function to swap the values of two variables using pass by value

#include<iostream.h>
void exchange(int, int);
void exchange(int x, int y)
{
int temp=x;
x=y;
y=temp;
cout<<"In the exchange function, x= "<<x<<" and y=
"<<y<<endl;//3 2
}

void main()
{
int a=2, b=3;
cout<<"Originally in main a= "<<a<<" and b=
"<<b<<endl;//2 3
exchange(a,b);
cout<<"Later in main a= "<<a<<" and b= "<<b<<endl;//2 3
9

## void exhange(int *x, int *y)

{
int temp=*x;
*x=*y; Pass arguments by
function to swap
} values at memory
locations.
void main()
{
int a=2, b=3;
exchange(&a,&b);
cout<<a<<" "<<b; //3 2
}

10

## C) Example: A function to swap the values of two variables using

pass by reference

## void exhange(int &x, int &y)

{
int temp=x;
x=y;
y=temp;
}

void main()
{
int a=2, b=3;
exchange(a,b);
cout<<a<<" "<<b; //3 2
}
11

Example (3)

## A) Cubing a Number Passing By Value

#include <iostream.h>
int cubeByValue( int );

void main()
{
int number = 5;
cout<<number<<endl; //5 is displayed
number = cubeByValue( number );
cout<<number; //125 is displayed

## int cubeByValue( int n )

return n * n * n;
}
12

## • When the cubeByValue(number) function is

called, values are copied in the

number n

5 5

13
B) Cubing a Number Passing By Address

#include <iostream.h>

void main()
{
int number = 5;
cout<<number<<endl; //5 is displayed
cout<<number; //125 is displayed

}

## *nPtr = *nPtr * *nPtr * *nPtr;

}
14

function is called, a link is established in the
follows:

number

5
nPtr

15
C) Cubing a Number Passing By Reference

#include <iostream.h>
void cubeByreference( int & );

void main()
{
int number = 5;
cout<<number<<endl; //5 is displayed
cubeByreference( number );
cout<<number //125 is displayed
}

n = n * n * n;
}
16

## • When the cubeByReference (number)

function is called, a link is established in the
void cubeByReference(int &n) as
follows:

number &n=0x009878

5 5