Sunteți pe pagina 1din 26

1

CSC099:
COMPUTING
ENGINEERING II
Chapter 6 : Function and Programme
Structure
(Part 2)

Objective

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

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

values
copied
prints 5
x

void passByValue (int x)


{
/*statement*/
x = x + 3;
return;
}

EXAMPLE PASS BY VALUE


6

#include<stdio.h>
void test(int a);
int main ( )
{
int x=50;
test(x);
printf(Value x now is %d, x);
}
void test(int a)
{
a += 50;
}
6

Example Pass by value


7
#include<stdio.h>
void passByValue(int k);
int 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

Example Pass by value


8
#include<stdio.h>
void changeMe(int num);
int main ( )
{
int num = 12;
printf (" In main function, number is %d, num);
changeMe (num);
printf (" Back in main again, number now is %d, num);
}
void changeMe(int num)
{
num = 0;
printf(In changeMe, the value is %d, num);
}

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

Example 1: Pass by reference


10

/*prototype declaration */
void passByRef(int *x);
After the type of parameter,
include dereference
int main (void)
operator (*)
{
/*local definition */
int a = 5;
Use address operator (&)
before the variable to be
/*statement*/
passed
passByRef (&a);
printf(%d\n, a);
return 0;
Prints 8
}
Require
dereference
operator (*)

void passByRef (int *x)


{
/*statement*/
*x = *x + 3;
return;
}

Dereference

Address
(pointer)

EXAMPLE PASS BY REFERENCE


11

#include<stdio.h>
void test(int *a);
int main ( )
{
int x=50;
test(&x);
printf(Value x now is %d, x);
}
void test(int *a)
{
*a += 50;
}

11

Example 2: Pass by reference


12
#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

Example Pass by REFERENCE


13
#include<stdio.h>
void changeMe(int *aValue);
int main ( )
{
int num = 12;
printf (" In main function, number is %d, num);
changeMe (&num);
printf (\nBack in main again, number now is %d,
num);
}
void changeMe(int *myValue)
{
*myValue = 0;
printf(\nIn changeMe, the value is %d, *myValue);
}

13

Pass by value vs. Pass by reference


14

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

#include <stdio.h>
void
15passByValue(int, int);
void passByReference(int *, int);
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\n", a);
}
void passByValue(int x, int y)
{
int t=6;
x +=t;
t+=2*y;
}
void passByReference(int *x, int y)
{
int t=6;
*x +=t;
t+=2*y;
}

16

Exercise 1

int main()
{
int num = 1;
printf(In main, num is %d\n, num);
anotherFunction();
printf(Back in main, num is %d\n, num);
return 0;
}
void anotherFunction()
{
int num = 20;
printf(In anotherFunction, num is %d\n, num);
}

void twin(int *);


17
int main()
{
int value = 4;
printf("In main, value is %d\n", value);
printf("Now calling twin...\n");
twin(&value);
printf("Now back in main, the value is %d\n", value);
return 0;
}
void twin(int *refVal)
{
if (*refVal >= 2)
*refVal *= 2;
else
*refVal -= 2;
}

18

Exercise

int a;
void f(int x, int y)
{
x += a;
a += 3*y;
}

int a;
void f(int *x, int y)
{
*x += a;
a += 3*y;
}

int main()
{
int a = 9;
f(a, 4);
printf("%d\n", a);
return 0;
}

int main()
{
int a = 9;
f(&a, 4);
printf("%d\n", a);
return 0;
}

void get_numbers(int *input1, int *input2);


void swap_values(int *var1, int *var2);
int main()
{
int first=7, second=5;
get_numbers(&first, &second);
swap_values(&first, &second);
printf("The reverse order of the two numbers is %d %d", first, second);
return 0;
}
void get_numbers(int *input1, int *input2)
{
printf("Enter two integers: ");
scanf("%d %d", &input1,&input2);
}
void swap_values(int *var1, int *var2)
{
int temp;
temp = var1;
var1 = var2;
var2 = temp;
19
}

Exercise 5 Modify code

Modify previous code so that the program will display the following
output.

Enter two integers: 1 2


The reverse order of the two numbers is 2 1

20

Exercise
a) Define the variable sumptr to be a pointer
to an object of type float.
b) Assign the address of variable total to
pointer sumptr.
c) Assign the value of pointer sumptr to
variable total2.
d) print the value of total2.
e) Write the function header for a function
called money that takes two pointers of
floating-point numbers p and q as
parameters and does not return a value.
21

22

Exercise 2

void anotherFunction(int *daisy, int rose);


int main(void)
{
int first, second;
first = 5;
second = 10;
anotherFunction(&second, first);
printf("%4d%4d\n", first, second);
return;
}
void anotherFunction(int *daisy, int rose)
{
int flower;
flower = 25;
rose = 5 + flower;
*daisy = flower * rose;
}

23

exercise

#include <stdio.h>

ANSWER??

A.
void do_something(int *thisp, int that); B.
C.
D.
int main(void)
{
int first, second;
first = 1;
second = 2;
do_something(&second, first);
printf("%4d%4d\n", first, second);
return (0);
}
void do_something(int *thisp, int that);
{
int the_other = 5;
that = 2 + the_other;
*thisp = the_other * that;
}

35
2
1 35
35
7
1 2

24

Exercise

void num(int *z);


int x, y;
int main()
{
x = 2; y = 3;
printf("%d\t%d\n", x, y);
num(&x);
printf("%d\t%d\n", x, y);
num(&y);
printf("%d\t%d\n", x, y);
return(0);
}
void num(int *z)
{
int y;
y = *z + 2;
*z = y * 3;
}

Exercise 3

#include <stdio.h>
25
void funOne (int a, int *b, char c);
int main()
{
int num1 = 10, num2 = 15;
char ch = 'A';
printf("Line 1: Inside main: num1 = %d, num2 = %d, and ch = %c\n", num1, num2, ch);
funOne(num1, &num2, ch);
printf("Line 3: After funOne: num1 = %d, num2 = %d, and ch = %c\n", num1, num2, ch);
return 0;
}
void funOne(int a, int *b, char c)
{
int one;
one = a;
a++;
b = *b * 2;
c = 'B';
printf("Line 2: Inside funOne: a = %d, b = %d, c = %c, and one = %d\n", a, b, c, one);
}

#include <stdio.h>
void funOne (int a, int *b, char c);
int main()
26
{
int num1 = 10, num2 = 15;
char ch = 'A';
printf("Line 1: Inside main: num1 = %d, num2 = %d, and ch =
%c\n", num1, num2, ch);
funOne(num1, &num2, ch);
printf("Line 3: After funOne: num1 = %d, num2 = %d, and ch =
%c\n", num1, num2, ch);
return 0;
}
void funOne(int a, int *b, char c)
{
int one;
one = a;
a++;
b = *b * 2;
c = 'B';
printf("Line 2: Inside funOne: a = %d, b = %d, c = %c, and one =
%d\n", a, b, c, one);
}

S-ar putea să vă placă și