Types of function in C Language | Function types in C Language
A function is categorized into two types:
1. Library Functions in C Language
These are predefined functions that exist in header files of C Language. These are meant for input/output, complex mathematical functions and various other tasks.
2. User Defined Function in C Language
User defined functions are created by programmers basically for those operations which are repeatedly needed in a program.
Types of a user defined function in C Language
User defined function can be categorized into four types:
i. Function without parameters and without return value in C Language
This type of function has no parameters. It doesn’t receive any values from the calling function.
Program for function without parameters and without return value in C Language. |
#include<stdio.h> void add() { int x,y,c; printf(“\nEnter two values to be added:=”); scanf(“%d%d”,&x,&y); c=x+y; printf(“\nAddition=%d”,c); } int main() { add(); return(0); } |
Output |
Enter two values to be added:=4 15 Addition=19 |
ii. Function with parameters and without return value in C Language
In this function, we specify parameters within the parenthesis given with function name.
The parameters passed while calling the function are called actual arguments whereas parameters given with function body are called formal Parameters. Formal parameters get values from actual parameters.
Program for function with parameters and without return value in C Language. |
#include<stdio.h> void multiply(int a, int b) //a and b are arguments/parameters { int c; c=a*b; printf(“\nmultiplication is %d”,c); } int main() { multiply(10,5); //10, 5 are actual parameters. return(0); } |
Output |
Multiplication=50 |
iii. Function with parameters and with return value in C Language
In this function, we need to specify return type as well as parameters within the parenthesis given with function name.
Input is generally received from calling function and after calculations or manipulations inside function body, result is returned back to calling function.
Program to demonstrate the use of function with arguments and without return value in C Language. |
#include<stdio.h> float sub(float a, float b) { float c; c=a-b; return(c); } int main() { float z=sub(10,3); printf(“Subtracti0n=%f”,z); return(0); } |
Output |
Subtraction=7 |
iv. Function without parameters and with return value in C Language
In this type of function, we specify return type but no parameters. This function performs some calculations and manipulation and result is returned back to the calling function.
Program for function without parameters and with return value in C Language. |
#include<stdio.h> int sum() { int a=5,b=3,c; c=a+b; return(c); } int main() { int c; c=sum(); printf(“Sum=%d”,s); return(0); } |
Output |
sum=8 |