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

2.passing Parameters To Functions

Uploaded by

fadi lamo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

2.passing Parameters To Functions

Uploaded by

fadi lamo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

1

Passing parameters to functions

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
(pass by address)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
3

Calling Functions by Reference


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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
4

• return can return one value from function

• Arguments passed to function using reference


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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5

Passing Parameters to Functions


Example (1)

• The following pass by value program can be written using


pass by reference instead.
A) A Pass by Value Program

int sum(int x, int y)


{
return x+y;
}

void main()
{
cout<<sum(2,3); //5 is displayed
}
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
}

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
}

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
} © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9

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


pass by address

void exhange(int *x, int *y)


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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
}
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
}
12

• When the cubeByValue(number) function is


called, values are copied in the
int cubeByAddress(int n) as follows:

number n

5 5

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
13
B) Cubing a Number Passing By Address

#include <iostream.h>
void cubeByaddress( int *b );

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

}
void cubeByaddress( int *nPtr )

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


}
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
14

• When the cubeByAddress(&number)


function is called, a link is established in the
void cubeByAddress(int *nPtr) as
follows:

number

5
nPtr

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
}

void cubeByReference( int &n )

n = n * n * n;
}
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

You might also like