Lecture 8 – Functions
Pass by Value and Pass by Reference
Differences between Value Parameters and
Reference Parameters
MAIN PROGRAM MEMORY
4000
25
age
If you pass only a copy of 25 to a
function, it is called “pass-by-value”
and the function will not be able to
change the contents of age. It is still
25 when you return.
MAIN PROGRAM MEMORY
4000 4000
25 40
age age
BUT, if you pass 4000, the address of
age to a function, it is called “pass-
by-reference” and the function will be
able to change the contents of age. It
could be any number (for example,
40) when you return.
Argument in Calling Block
4000
25
age
Value Parameter Reference Parameter
The value (25) of the argument The memory address (4000) of the
is passed to the function when argument is passed to the function when
it is called. it is called.
reference parameters should be used
when you want your function to give a
value to, or change the value of, a
variable in the calling block.
Return more than one variables.
int main()
/* Swap the values of two variables. {
*/
int a = 3, b = 5;
void badSwap ( int a, int b )
printf("%d %d\n", a, b);
{
badSwap ( a, b );
int temp;
printf("%d %d\n", a, b);
temp = a;
return 0;
a = b;
}
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
11
int main()
/* Swap the values of two variables. {
*/
int a = 3, b = 5;
void badSwap ( int a, int b )
printf("%d %d\n", a, b);
{
badSwap ( a, b );
int temp;
printf("%d %d\n", a, b);
temp = a;
return 0;
a = b;
}
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
12
int main()
/* Swap the values of two variables. {
*/
int a = 3, b = 5;
void badSwap ( int a, int b )
printf("%d %d\n", a, b);
{
badSwap ( a, b );
int temp;
printf("%d %d\n", a, b);
temp = a;
return 0;
a = b;
}
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
3 5
13
int main()
/* Swap the values of two variables. */ {
int a = 3, b = 5;
void badSwap ( int a, int b )
{ printf("%d %d\n", a, b);
int temp; badSwap ( a, b );
printf("%d %d\n", a, b);
temp = a;
a = b; return 0;
b = temp; }
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
3 5
14
int main()
/* Swap the values of two variables. */ {
int a = 3, b = 5;
void Swap ( int *a, int *b )
{ printf("%d %d\n", a, b);
int temp; Swap ( &a, &b );
printf("%d %d\n", a, b);
temp = *a;
*a = *b; return 0;
}
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
15
int main()
/* Swap the values of two variables. {
*/
int a = 3, b = 5;
void Swap ( int *a, int *b )
printf("%d %d\n", a, b);
{
Swap ( &a, &b );
int temp;
printf("%d %d\n", a, b);
temp = *a;
return 0;
*a = *b;
}
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
5 3
16
int main()
/* Swap the values of two variables. {
*/
int a = 3, b = 5;
void Swap ( int *a, int *b )
printf("%d %d\n", a, b);
{
Swap ( &a, &b );
int temp;
printf("%d %d\n", a, b);
temp = *a;
return 0;
*a = *b;
}
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
5 3
5 3
17
int main()
/* Swap the values of two variables. {
*/
int a = 3, b = 5;
void Swap ( int *a, int *b )
printf("%d %d\n", a, b);
{
Swap ( &a, &b );
int temp;
printf("%d %d\n", a, b);
temp = *a;
return 0;
*a = *b;
}
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
5 3
5 3
18
We want to find 2 real roots for a quadratic
equation with coefficients a,b,c. Write a
prototype for a void function named
GetRoots( ) with 5 parameters. The first 3
parameters are type float. The last 2 are
reference parameters of type float.
quadratic equation:
2+ +
2x 2 +4x−6=0
Thus: a = 2, b = 4, c = -6
#include <stdio.h>
#include <math.h>
void GetRoots(float, float, float, float*,float*);
//Last 2 parameters: the two roots (the answers) — the function must calculate
and send them back (output).
void main ( )
{
float a, b, c, first, second;
printf(“ Enter a b c : “);
scanf(“%f %f %f”, &a , &b, &c );
GetRoots(a, b, c, &first, &second);
printf(“ Roots are %.2f %.2f \n “ , first ,
second );
}
Function Definition
In C programming, a single array element or an
entire array can be passed to a function.
Also, both one-dimensional and multi-
dimensional array can be passed to function as
argument.
#include <stdio.h>
void display(int a)
{ printf("%d",a);}
int main()
{int c[]={2,3,4};
display(c[2]); //Passing array element c[2] only.
return 0;}
Output : 4
While passing arrays to the argument, the name
of the array is passed as an argument (i.e.,
starting address of memory area is passed as
argument).
Write a C program to pass an array containing
age of 6 persons to a function. This function
should find average age and display the
average age in main function.
#include <stdio.h>
float average(float a[]);
int main()
{
float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};
avg=average(c);/* Only name of array is passed as argument. the
address of the first element of the array! */
printf("Average age=%.2f",avg);
return 0;
}
float average(float a[])
{ int i;
float avg, sum=0.0;
for(i=0;i<6;i++)
{sum+=a[i];}
avg =(sum/6);
return avg;
}
#include <stdio.h>
int* test(int size, int x);
int main()
{
int n = 3, a = 10, i;
int *z;
z = test(n, a);
for (i=0; i<n;i++)
printf("%d", z[i]);
return (0);
}
int* test(int size, int x) //The test function will create an array of 3
integers, fill it with values, and return a pointer to of the array.
{
int* factorFunction = malloc(sizeof(int) * size);
factorFunction[0] = 5 + x;
factorFunction[1] = 7 + x;
factorFunction[2] = 9 + x;
return (factorFunction);
}
Step 1 –
Starting main()Variables initialized:n = 3 a = 10 z is a pointer to int
Step 2 - Calling test(n, a) Call: z = test(3, 10); Goal: Create an array of 3
integers, initialize based on x = 10
Step 3 –
Inside test()Memory allocated:malloc(sizeof(int) * size) -> 12 bytes (if int =
4 bytes) Initialize elements :
factorFunction[0] = 15
FactorFunction[1] = 17
factorFunction[2] = 19
Step 4 –
Returning to main()
z now points to array: {15, 17, 19}
Access through z[i]
Step 5 - Printing the ElementsLoop through z and
print:Output: 151719 (no :
Final OutputOutput: 151719Note: All numbers printed continuously
To pass two-dimensional array to a function as
an argument, starting address of memory area
reserved is passed as in one dimensional array.
#include <stdio.h>
void Function(int c[2][2]);
int main()
{int c[2][2],i,j;
printf("Enter 4 numbers:\n");
for(i=0;i<2;++i)for(j=0;j<2;++j)
{scanf("%d",&c[i][j]);}
Function(c); /*passing multi-dimensional array to function */
//decays into a pointer to the first row.
return 0;
}
void Function(int c[2][2])
{/* Instead to above line, void Function(int c[][2]){ is also valid */
int i,j;
printf("Displaying:\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
printf("%d\n",c[i][j]);
}