Variable: Stores data.
Function: Performs operation on data.
Function takes input, performs operations and returns output
Syntax | Example |
int identity(arguments) { -> statements; } | int add(int a, int b) { int res = a+b ; return res ; } |
C functions classified into:
- Built In Functions
- User Defined Functions
Built in Functions:
- C library is a set of header files
- Header file is a collection of pre-defined functions.
stdio.h | conio.h | string.h | graphics.h |
printf() scanf() feof() … | getch() clrscr() getche() …. | strlen() strrev() strcat() ….. | line() circle() bar() … |
Custom Functions: Programmed defined functions based on application requirement
Calculator.c | Mobile.c | Account.c |
add() subtract() multiply() divide() | call() message() store() ….. | deposit() withdraw() …. |
Every function consists
- Prototype:
- Prototype is called function declaration. Every Custom function must be specified before main().
- Definition:
- Definition is a block of instructions contains function logic. Function executes when we call that function.
- Function call:
- It is a statement and it is used to execute the function logic.

No arguments and No return values function:
#include<stdio.h> void sayHi(void); int main(void) { sayHi(); return 0 ; } void sayHi(void) { printf(“Hi to all \n”); } |
With arguments and No return values function: (Even Number Program)
#include<stdio.h> void isEven(int); int main(void) { int n; printf(“Enter number : “); scanf(“%d”, &n); isEven(n); return 0 ; } void isEven(int num){ if(num%2==0) printf(“%d is Even \n”, num); else printf(“%d is not Even \n”, num); } |
With arguments and With return values function: (addition of 2 numbers)
#include <stdio.h> int add(int, int); int main() { int a, b, res; printf(“Enter two numbers : \n”); scanf(“%d%d”, &a, &b); res = add(a,b); printf(“%d + %d = %d\n”, a, b, res); return 0; } int add(int x, int y) { int z=x+y ; return z; } |
No arguments and With return values function:
float getPI(void); void main(void) { float pi; fpi = getPI(); printf(“PI value is : %f\n”, pi); } float getPI(void){ return 3.142; } |
Arithmetic Operations Menu Driven program – Using functions
#include <stdio.h> float add(float,float); float subtract(float,float); float multiply(float,float); float divide(float,float); int main() { int choice; float a,b,c; while(1) { printf(“1.Add\n”); printf(“2.Subtract\n”); printf(“3.Multiply\n”); printf(“4.divide\n”); printf(“5.Exit\n”); printf(“Enter your choice : “); scanf(“%d” , &choice); if(choice>=1 && choice<=4) { printf(“Enter 2 numbers :\n”); scanf(“%f%f”, &a, &b); } switch(choice) { case 1 : c = add(a,b); printf(“Result : %f\n\n”,c); break ; case 2 : c = subtract(a,b); printf(“Result : %f\n\n”,c); break ; case 3 : c = multiply(a,b); printf(“Result : %f\n\n”,c); break ; case 4 : c = divide(a,b); printf(“Result : %f\n\n”,c); break ; case 5 : exit(1); default : printf(“Invalid choice \n\n”); } } return 0; } float add(float x, float y) { return x+y ; } float subtract(float x, float y) { return x-y ; } float multiply(float x, float y) { return x*y ; } float divide(float x, float y) { return x/y ; } |
Recursion:
- Function calling itself.
- Calling the function from the definition of same function
#include<stdio.h> void recur(); int main() { recur(); return 0; } void recur() { printf(“Start\n”); recur(); printf(“End\n”); } |

We need to take care of function execution completion in recursive call.
#include<stdio.h> void abc(int); int main() { abc(3); return 0; } void abc(int x) { printf(“x : %d\n”,x); if(x>0) abc(x-1); } |

#include<stdio.h> void abc(int); int main() { abc(3); return 0; } void abc(int x){ printf(“x : %d\n”,x); if(x>0) abc(x-1); printf(“x : %d\n”,x); } |

Find the factorial using recursion:
#include<stdio.h> int fact(int); int main() { int res; res = fact(4); printf(“Factorial : %d \n”, res); return 0; } int fact(int n) { if(n==0) return 1; else return n*fact(n-1); } |

Program to find sum of first N numbers using Recursion:
#include<stdio.h> int sum(int); void main(void) { int n, s; printf(“Enter n value : “); scanf(“%d”, &n); s = sum(n); printf(“Sum of %d nums : %d\n”, n, s); } int sum(int n) { if(n==0) return n; else return n+sum(n-1); } |