C Functions
C Functions
C Functions
Introduction 2
What is function?
Example programs.
Introduction 3
Modular Programming 4
Predefined
Userdefined
Predefined functions 12
If the functionality of a function is defined by the compiler
those functions are called predefined functions.
NOTE
Users are free to define their own code for the user-defined
functions. Every user-defined function must have the
following….
Declaring User-defined functions 15
Syntax
Example
NOT
E
Example
Calling User-defined functions 17
Syntax
Example
Working of Functions
18
Example Program
void main() 19
{
int a, b, c;
void addition(int , int);
printf(“We are in main…..\n”);
printf(“Enter any two numbers: ”);
scanf(“%d%d”, &a, &b);
addition(a, b);
printf(“We are again in main…..”);
}
void addition(int x, int y)
{
printf(“SUM = %d\n”, x+y);
}
Important terms in functions
20
void add( )
Control {
int a,b,c;
printf(Enter any two numbers: );
scanf(“%d%d”, &a, &b);
No return value
c = a + b;
printf(“Result = %d”, c);
}
With Parameters and without Return value
31
In this type, there is data transfer from calling to called
function, but not from called to calling function.
B Sudhakar
}
void add(int a, int b)
{
int c;
c = a + b;
printf(“Result = %d”, c);
}
10/22/2019 32
33
void main( )
{
void add( int, int ); 10 & 20 as input
printf(“We are in main….\n”);
add( 10, 20 );
printf(“We are again in main….\n”); Control
}
B Sudhakar
printf(“Result = %d\n”, c);
}
int add( )
{
int a, b;
printf(“Enter any two numbers:”);
scanf(“%d%d”, &a, &b);
return (a + b);
10/22/2019
} 35
void main( ) 36
{
int c;
int add( ); No input
printf(“We are in main….\n”);
c = add( );
printf(“Result = %d\n”, c); Control
}
void add( )
{
Control int a,b;
printf(Enter any two numbers: );
30 as return value scanf(“%d%d”, &a, &b);
return (a + b);
}
void main()
{
int c;
int add( int, int );
printf(“We are in main….\n”);
B Sudhakar
c = add( 10, 20 );
printf(“Result = %d\n”, c);
}
int add( int a, int b)
{
return (a + b);
}
10/22/2019 38
void main( ) 39
{
int c;
int add( int, int );
printf(“We are in main….\n”); 10 & 20 as input
c = add( 10, 20 );
printf(“Result = %d\n”, c);
} Control
Control
void add( int a, int b)
{
30 as return value return (a + b);
}
Different ways to make a function call
40
There are THREE ways of making function call…
1. From main function 41
return add(a,b);
} Control
void function2()
{
body of the
function;
}
3. From a user-defined to same 44
user-defined function (recursive)
We can also make a function call from same function. That
means function calls itself
When a function calls itself until the last call is invoked till
that time the first call also remains open.
B Sudhakar
Output:
This is example of Recursion!!!
This is example of Recursion!!!
This is example of Recursion!!!
This is example of Recursion!!!
…….
10/22/2019 45
Example Program:
46
int factorial( int );
void main( )
{
int fact, n;
printf(“Enter any positive integer: ”);
scanf(“%d”, &n);
fact = factorial( n );
printf(“Factorial of %d is %d”, n, fact);
}
int factorial( int n )
{
int temp;
if( n == o)
return 1;
else
temp = n * factorial( n-1 );
return temp;
}
int factorial( int );
void main( ) 47
{
int fact, n;
printf(“Enter any positive integer: ”);
scanf(“%d”, &n);
fact = factorial( n );
printf(“Factorial of %d is %d”, n,
fact);
}
else if(n<=2)
return 1;
else febnum (2) febnum (1)
return (febnum(n-1)+febnum(n-2));
}
febnum (1) febnum (0)
4. Mutual Recursion or Indirect 53
Recursion
Calling two or more functions mutual is called
mutual recursion. Say for example, if function A is
calling B and function B is calling A recursively then it
is said that, they are in mutual recursion.
int isOddNumber(int n) int isEvenNumber(int n)
{ {
if (0 == n) if(0 == n)
return 0; return 1;
else else
return isEvenNumber(n - 1); return isOddNumber(n - 1);
} }
Here in the following example isEvenNumber() is calling
isOddNumber() and isOddNumber() is calling isEvenNumber()
5. Nested Recursion 54
When a recursive method has a parameter defined in
terms of itself then it is called nested recursion.
Implementation of classical mathematical function,
Ackermann’s, is good example of nested function.
int ackerman(int m, int n)
{
if(m==0)
return (n+1);
else if(n==0)
return ackerman(m-1,1);
else
return (ackerman(m-1,ackerman(m,n-1)));
}
In nested recursion a function call have recursive int that
recursive call contain another recursive call or a function call
Example for nested recursive 55
#include<stdio.h>
int main()
{
int a,b;
printf("Enter four integers:\n");
scanf("%d%d",&a,&b);
s=ackerman(a,b);
printf(“The result= %d",s);
return 0;
}
int ackerman(int m, int n)
{
if(m==0)
return (n+1);
else if(n==0)
return ackerman(m-1,1);
else
return (ackerman(m-1,ackerman(m,n-1)));
}
Example for mutual Recursion 56
int iseven(int num)
{
if(n<=num)
{
Output: 2 1 4 3 6 5 8 7 10 9
printf("%d\t",n-1);
n++;
return isodd(num);
}
#include<stdio.h>
}
int n=1
int isodd(int num)
void main()
{
{
if(n<=10)
int a;
{
printf("Enter an number");
printf("%d\t",n+1);
scanf("%d",&a);
n++;
isodd(a);
return iseven(num);
}
}
}