FUNCTION
POINTER
CALL BY VALUE
CALL BY REFERENCE
RECURSION
FUNCTION
A function in C is defined as a block of statements that performs a
specific task.
A function takes some input, do some specific computation on it and
produces output.
Input Computation Output
W H AT I S N E E D O F F U N C T I O N ?
To group common code related to a task together.
Instead of writing same code again and again for different inputs,
write code once in a function, then call it repeatedly.
TYPES OF FUNCTIONS Library Functions
There are two types of functions in C.
User defined Functions
L I B R A RY F U N C T I O N :
Standard built-in functions in C programming.
f o r ex a m p l e , p r i n t f ( ) , s c a n f ( ) , s q r t ( ) , p o w ( ) , s t r c p y ( ) , cl r s c r ( ) e t c .
stdio.h math.h string.h conio.h
These functions are defined and stored in header files.
USER DEFINED FUNCTION:
Functions cr eated by the user as per need.
f o r ex a m p l e , s u m ( ) , a r e a ( ) , s a l a r y ( ) , f a c t o r i a l ( ) , l a r g e s t ( ) e t c .
#include<stdio.h>
USER DEFINED FUNCTION Function Declaration
#include<conio.h>
There are three steps for a user defined function in C Program. int sumFunction
(int, int, int);
Definition
void main()
data_type fun_name (args); Function Call
Function Declaration: {
int sum (int, int, int); int a, b, c, s;
data_type fun_name (formal args) printf(“Enter three numbers: ”);
scanf(“%d%d%d”, &a, &b,&c);
Function Definition: {
s = sum(a, b, c); //Actual Arguments
Output: statement 1;
printf(“ Sum is %d”, s);
statement 2;
Enter three numbers: 1 3 7 getch();
statement 3;
Sum is 11 } //Formal Arguments
int sum ( int m, int n, int p)
statement n;
{
}
int k;
Function Call: fun_name (actual args); k = m + n + p;
return (k);
}
POINTERS void main() Pointer to Pointer to Pointer
Pointer is a special variable in C which stores {the address of another
a b c int a = 10; char b = ‘R’; float c = 4.5;
variable. 10 R 4.5 int *p; int **m; int ***n;
28004 32408 40004 p = &a; m = &p; n = &m;
p k t char *k; Address Pointer to Pointer
Of
28004 32408 40004 k = &b; Operator Unsigned
Value at
82880 20248 34528 float *t; Address
Operator
m t = &c;
Output: 82880 printf(“Address of ap is %u”, p);
m);
Address of pa is 82880
28004
25004 *p);
printf(“\nValue of a is %d”, **m);
Value of a is 1010 n printf(“\nAddress of b misis%u”,
%u”,k);
n);
Address of m b isis 32408
25004 25004
printf(“\nValue of ab is %d”,
%c”, *k);
***n);
Value of ab is 10
R 40448
printf(“\nAddress of ca is %u”, t);
&a);
Address of
Address of ac is
is 28004
40004
printf(“\nValue of ac is %f”,
%d”,*t);
*(&a));
Value of
Value of ac is
is 10
4.5 }
temp
CALL BY VALUE 55
//Swap Values Using Call by Value
#include<stdio.h> 2008 8004 1018 4284
#include<conio.h> 5 10 5 10
void swapv(int, int); a b a b
void main() Output:
{ After Swapping:
int a=5, b=10;
temp = a = 5 a=10 and b=5
swapv(a, b);
a = b = 10 Value of a and b in main
printf(“Value of a and b in main ” );
b = temp = 5 a = 5 and b = 10
printf(“a = %d and b = %d”, a, b);
getch();
}
void swapv(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf(“After swapping: ” );
printf(“a = %d and b = %d”, a, b);
}
CALL BY REFERENCE
//Swap Values Using Call by Reference a b
#include<stdio.h> 5 10
#include<conio.h> 2004 3008
void swapr(int*, int*);
void main() temp Output:
{ 5 After Swapping:
int a=5, b=10; a=10 and b=5
swapr(&a, &b); 2004 3008 Value of a and b in main
printf(“Value of a and b in main ” ); x y a = 10 and b = 5
printf(“a = %d and b = %d”, a, b);
getch();
}
temp = *x temp = *(&a) = 5
void swapr(int *x, int *y)
*x = *y *(&a) = *(&b) = 10
{
*y = temp *(&b) = temp = 5
int temp;
temp = *x;
*x = *y;
*y = temp;
printf(“After swapping: ” );
printf(“a = %d and b = %d”, *x, *y);
}
#include <stdio.h>
RECURSION int natural(int);
A function that calls itself in its definition is known as recursive function
void main()
Program to find sum of n natural numbers using recursion
and this technique is known as recursion. {
int number, sum;
return 5 + natural(4)
return 5 + 4 + natural(3) printf("Enter a positive integer: ");
return 5 + 4 + 3 + natural(2) scanf("%d", &number);
return 5 + 4 + 3 + 2 + natural(1) sum = natural(number);
return 5 + 4 + 3 + 2 + 1 + natural(0) printf(“Sum = %d", sum);
return 5 + 4 + 3 + 2 + 1 + 0 }
return 15
int natural(int n)
{
Output: if (n == 0) //base Condition
Enter a positive integer: 5 return 0; // natural() function calls itself
Sum = 15 else
return n + natural(n-1);
}
#include <stdio.h>
FACTORIAL USING RECURSION int fact(int n);
// Without Recursion // Without Recursion
void main()
return 5 * fact(4) for(i= n; i>0; i--)
return 5 * 4 * fact(3) f= f*i; {
return 5 * 4 * 3 * factl(2) = 1*5 int num, factorial ;
return 5 * 4 * 3 * 2 * fact(1) = 1*5*4 printf("Enter a number: ");
return 5 * 4 * 3 * 2 * 1 * fact(0) = 1*5*4*3 scanf("%d", &num);
return 5 * 4 * 3 * 2 * 1 * 1 = 1*5*4*3*2
return 120 = 1*5*4*3*2*1 factorial = fact(num);
= 120 printf(“Factorial = %d", factorial);
return 120 getch();
Output:
// Without Recursion } // With Recursion
Enter a number: 5
int fact(int
fact(intn)n)
Factorial = 120
{ int f=1, i;
for(i
if(n ===n;0)
i > //base
0; i--) condition
{
return 1;
f=f*i;
else
}
return n*fact(n-1);
return f; }
}
FIBONACCI SERIES
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 .…..
A series in which each number is the sum of the two preceding terms, starting
from 0 and 1, is known as Fibonacci Series.
Fibo(5) 3
Fibo(1)= 0, Fibo(2)=1
Fibo=(3)= Fibo(2) + Fibo(1) =1 + 0 = 1
Fibo=(4)= Fibo(3) + Fibo(2) =1 + 1 = 2 2 Fibo(4) Fibo(3) 1
Fibo=(5)= Fibo(4) + Fibo(3) =2 + 1 = 3
Fibo=(6)= Fibo(5) + Fibo(4) =3 + 2 = 5
Fibo=(7)= Fibo(6) + Fibo(5) =5 + 3 = 8 1 Fibo(3) Fibo(2) Fibo(2) Fibo(1)
… 1 1 0
5
…
… Fibo(2) Fibo(1)
Fibo(n)= Fibo(n-1) + Fibo(n-2) 1 0
Recursion leads to indefinite run if you don’t write code for
ending it (i.e. base condition). So to prevent infinite recursion you
must use if-else statement to end recursive call.
Recursion makes program code more compact as compared to
iterative code.
Recursion provides a clean and simple way to write code as well
as to understand and debug later.
Recursion is slower as compared to code written using
iterations. So to achieve performance, use loops instead of recursion.
Recursion consumes much memory than iterative code.
Lets do it in Compiler.
Write a program to find area of a circle using function.
Write a program to find area of triangle using function.
Write a program to calculate cube of a number using
function.