Functions
1
23/11/22
Functions
2
A simple function
3
23/11/22
#include<stdio.h>
float max(float x, float y); main( )
{
float a,b,m;
scanf(“%f %f”, &a, &b); m = max(a,b); printf(“Max : %f\n”, m);
}
float max(float x, float y)
{
if(x > y) return x; else return y;
}
Function declaration
Function call
Function definition
One more example
4
23/11/22
/* function prototype */
#include <stdio.h> int square( int y); int main( )
{
int x;
for ( x = 1; x <= 10; x++ ) printf( "%d ", square( x ) );
printf( "\n" ); return 0;
}
int square( int y )
{
return y * y;
}
Main is also a function.
It returns an int
If unspecified, then by default a function returns int
What we should learn
5
23/11/22
Control Flow
6
23/11/22
i.
ii.
iii.
iv.
v.
vi.
vii.
exp1, exp2, … are evaluated
The values of exp1, exp2, … are passed to func( ) main is suspended temporarily
func( ) is started with the supplied values func( ) does the processing
func( ) returns a value which is assigned to z main( ) is resumed.
Function prototype
is also valid.
one is int and second one is float) and return a float.
7
23/11/22
void
8
23/11/22
void
9
23/11/22
{
nothing( );
}
void nothing(void)
{
printf(“when you need to do some fixed thing\n” “then you can use a func. like this \n”);
return;
}
Function definition
10
23/11/22
{
declarations statements
}
To find maximum of three ints
11
23/11/22
#include <stdio.h>
int maximum( int, int, int ); int main()
{
int a, b, c;
/* function prototype */
printf( "Enter three integers: " ); scanf( "%d%d%d", &a, &b, &c );
printf( "Maximum is: %d\n", maximum( a, b, c ) ); return 0;
}
int maximum( int x, int y, int z )
{
int max = x;
if ( y > max ) max = y; if ( z > max ) max = z;
return max;
}
You cannot use max in main( )
Declaration. When this function returns max disappears !!
max is called a local variable for this function.
Let us swap
12
23/11/22
void swap(int, int); main( )
{
int a, b;
a = 10, b = 20;
swap(a, b);
printf(“a = %d, b = %d\n”, a, b);
}
void swap(int x, int y)
{
int t;
t = x; x = y; y = t; return;
}
$./a.out
a = 10, b = 20
$
This does not work !
Why swap failed !?
13
Is there a way for the swap to correctly work?
How swap can correctly work
14
Tips and traps
15
23/11/22
Recursion
16
{
int t;
t =factorial(n-1); return ( n * t );
}
Recursion
17
{
int t;
if ( n < 0) return -1; /* an error code */ else if ( n == 0) return 1;
else {
t = factorial(n-1); return (n * t);
}
}
x = factorial(4);
Recursion
18
{
if(n == 0 || n ==1) return n; else return fib(n-1) + fib(n-2);
}
Recursion
19
23/11/22
{
if (n == 0) return 0; else return n + sum(n-1);
}
Recursion
20
Replacing recursion
21
{
int t = 0, j; for(j=1; j<=n; j++)
t = t + j; /* alternatively, t += j; */ return ( t );
}
Fibonacci without recursion
22
4. j = j+1;
Fibonacci without and with recursion
int fib( int n)
{
int a = 0, b = 1, c; int j = 1;
if (n == 0 || n == 1) return n;
while(j < n) {
c = a+b; a = b;
b = c; j ++;
}
return c;
}
23
int fib( int n)
{
if (n == 0 || n == 1) return n;
else
return fib(n-1) + fib(n-2);
}
Recursion
24
Functions
25
23/11/22