Lecture 5 – Functions
Pass by Value and Pass by Reference
Recursion
Differences between Value Parameters and
Reference Parameters
Recursion
MAIN PROGRAM MEMORY
25
4000
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
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 23 or 90 when you return.
25
4000
age
Value Parameter Reference Parameter
The value (25) of the
argument
is passed
to the function when
it is called.
The memory address (4000)
of the argument
is passed
to the function when
it is called.
25
4000
age
Argument
in Calling Block
reference parameters should be used
when you want your function to give a
value to, or change the value of, a
variable from the calling block.
Return more than one variables.
CALLING
BLOCK FUNCTION
CALLED
“incoming”
value of
argument
CALLING
BLOCK FUNCTION
CALLED
“incoming”
original value of
argument
“outgoing”
changed value of
argument
OR,
CALLING
BLOCK FUNCTION
CALLED
argument
has no value yet
when call occurs
“outgoing”
new value of
argument
13
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
badSwap ( a, b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void badSwap ( int a, int b )
{
int temp;
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
14
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
badSwap ( a, b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void badSwap ( int a, int b )
{
int temp;
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
15
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
badSwap ( a, b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void badSwap ( int a, int b )
{
int temp;
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
3 5
16
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
badSwap ( a, b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables. */
void badSwap ( int a, int b )
{
int temp;
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
3 5
17
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
Swap ( &a, &b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables. */
void Swap ( int *a, int *b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
18
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
Swap ( &a, &b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void Swap ( int *a, int *b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
5 3
19
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
Swap ( &a, &b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void Swap ( int *a, int *b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
5 3
5 3
20
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
Swap ( &a, &b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void Swap ( int *a, int *b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
5 3
5 3
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.
#include <stdio.h>
#include <math.h> void GetRoots(float, float, float, float*, float*);
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 GetRoots goes here
……..
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. */ 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; }
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 */
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]);
}
What is recursion?
When one function calls ITSELF directly or
indirectly.
A function is a recursive if a statement in the
body of the function calls the function that
contains it.
Why learn recursion?
New mode of thinking.
Powerful programming tool.
Divide-and-conquer paradigm.
Many computations are naturally self- referential
A recursive definition is made up of two parts. ◦ Base case that tells us directly what the answer is.
◦ Recursive case that defines the answer in terms of the answer to a subproblem.
For example, in factorial: ◦ Base case is factorial(0)=1.
◦ Recursive case is factorial(n) = n * factorial(n-1).
Recursive Function Multiply
int multiply ( int m, int n)
{
int ans;
If (n==1)
ans=m;
else
ans= m + multiply (m, n-1);
return (ans);
}
m is 6
n is 3
3 == 1 is false
ans is 6 + multiply (6,2)
return (ans)
m is 6
n is 2
2 == 1 is false
ans is 6 + multiply (6,1)
return (ans)
m is 6
n is 1
1 == 1 is true
ans is 6
return (ans)
6
12
18
multiply (6,3) int multiply ( int m, int n) { int ans; If (n==1) ans=m; else ans= m + multiply (m, n-
1); return (ans); }
When a function calls itself, new local variables
are allocated storage on the stack, and the
function code is executed with these new
variables from the beginning .
A recursive call does not make a new copy of
the function. Only the arguments are new.
#include <stdio.h>
int sum(int n)
{ if(n==0)
return n;
else return n+sum(n-1);
}
int main()
{ int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
#include <stdio.h>
int sum(int n);
int main()
{ int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n)
{ if(n==0)
return n;
else return n+sum(n-1);
}
sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15
Functions are very often defined recursively. The classic example is the factorial function. ◦ factorial(0) = 1 ◦ factorial(n) = n * factorial(n-1) [for n>0]
Let's compute factorial(3). ◦ factorial(3) = 3 * factorial(2) = 3 * 2 * factorial(1) = 3 * 2 * 1 * factorial(0) = 3 * 2 * 1 * 1 = 6
int factorial(int n)
{
if (n == 0) return 1 ;
else return n * factorial(n-1) ;
}
Base Cases: fib(0) = 0, fib(1) = 1
Recursive Case (two recursive calls): fib(n) = fib(n-1) + fib(n-2) [for n>1]
fib(4)= fib(3) + fib(2)
= fib(2) + fib(1) + fib(1) + fib(0)
= fib(1) + fib (0) + 1 + 1 + 0
= 1 +0 + 1 + 1 + 0
= 3
int fib(int n)
{
if (n == 0) return 0;
if (n == 1) return 1;
else return (fib(n-1) + fib(n-2));
}
43
F(X) = 2 *X + F ( X – 2) ;
F(0) = F(1) = 0;