Sie sind auf Seite 1von 11

CSC099: FOUNDATION

COMPUTING II (PART 2)
Chapter 6 : Function and
Programme Structure
(Part 2)
1
2
Objectives :
To understand the difference between
pass by value and pass by reference

Passing variables to a Function
3
In memory location, a variable has both a value and a
unique address
In passing a variable to function, you can pass either the
variables value (pass by value) or its address (pass by
reference) to the receiving function
Passing by value
In a pass by value, the computer passes the contents of
the variable to the receiving function
Receiving function is not given access to the variable
in memory
It cannot change value stored inside variable
By default, variables are passed by value in C

4
Example : Pass by value
5
/*prototype declaration */
void passByValue (int x);

int main (void)
{

/*local definition */
int a = 5;

/*statement*/
passByValue (a);
printf(%d\n, a);
return 0;
}
void passByValue (int x)
{
/*statement*/
x = x + 3;
return x;
}
a
x
values copied
prints 5
Example Pass by value
6
#include<stdio.h>

void passByValue(int k);

void main ( )
{
int i=0;
printf (" The value of i before call %d \n", i);
passByValue (i);
printf (" The value of i after call %d \n", i);
}

void passByValue(int k)
{
k = k + 10;
}
The value of i before call 0
The value of i after call 0
Pass by reference
Passing a variables address is referred to as passing by
reference
Receiving function has access to passed variable
Use ONLY when you want receiving function to
change the contents of variable
To pass a variable by reference in C
Include dereference operator (*) after the type of
formal parameter in function prototype and function
header
Include an address-of operator(&) before the
variable of actual parameter in function call
7
Example 1: Pass by reference
8
/*prototype declaration */
void passByRef(int *x);

int main (void)
{
/*local definition */
int a = 5;

/*statement*/
passByRef (&a);
printf(%d\n, a);
return 0;
}
void passByRef (int *x)
{
/*statement*/
*x = *x + 3;
return;
}


a
Prints 8
x
Dereference
Address
(pointer)
After the type of parameter,
include dereference operator (*)
Use address operator (&) before
the variable to be passed
Require
dereference
operator (*)
Example 2: Pass by reference
9
#include<stdio.h>

void passByRef(int *k) ;

int main ( )
{
int i=0;

printf (" The value of i before call %d \n", i);
passByRef(&i);
printf (" The value of i after call %d \n", i);
}
void passByRef(int *k)
{
*k = *k + 10;
}
The value of i before call 0
The value of i after call 10
Pass by value vs. Pass by reference
Pass by value
a copy of data is created and placed in a local
variable in the called function
ensure that regardless of how the data is manipulated
and changed in the called function, the original data
in the calling function are safe and unchanged
Pass by reference
sends the address of a variable to the called function
use the address operator (&) in the parameter of the
called function
anytime we refer to the parameter, therefore we
actually referring to the original variable
if the data is manipulated and changed in the called
function, the original data in the calling function are
changed
10
Pass by value vs. Pass by reference
11
#include <stdio.h>

void passByValue(int a, int b);
void passByReference(int *a, int b);

int main(){

int a=3;
passByValue(a,4);
printf("\nThe value of a after passByValue() is %d", a);


a=3;
passByReference(&a,4);
printf("\nThe value of a after passByReference() >> %d", a);

}

void passByValue(int x, int y){
int a=6;
x +=a;
a+=2*y;
}

void passByReference(int* x, int y){
int a=6;
*x +=a;
a+=2*y;
}
The value of a after passByValue() is 3
The value of a after passByReference() is 9

Das könnte Ihnen auch gefallen