Documente Academic
Documente Profesional
Documente Cultură
CSC099:
COMPUTING
ENGINEERING II
Chapter 6 : Function and Programme
Structure
(Part 2)
Objective
Passing by value
/*prototype declaration */
void passByValue (int x);
values
copied
prints 5
x
#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
Pass by reference
/*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 (*)
Dereference
Address
(pointer)
#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
13
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);
}
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;
}
Modify previous code so that the program will display the following
output.
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
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
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);
}