Types of function in C Language

Preview
You must first complete Introduction to function in C Language before viewing this Lesson

Types of function in C Language

A function is categorized into two types:

1. Library Functions

These are predefined functions existing in header files.  These functions are basically meant for basic input/output, complex mathematical calculations and several other tasks.

2.  User Defined Function

This type of function is created by programmers. They are mainly created for those operations which are repeatedly needed . 



Types of a user defined function

User defined function can be categorized into four types:

i. Function without parameters and without return value

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.
#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 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.
#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 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.
#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 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.
#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



 

Best Books of C





Lesson tags: actual arguments in c, formal arguments in c, library function in c, parameter passing in c, types of function in c, types of user defined functions in c
Back to: C Programming Language