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 can be categorized into two categories:

  • Library Function
  • User Defined Function

1. Library Function

It is a predefined function provided by C Language programming language. Library functions exist in header files i.e. their definitions lie in header files.

These functions are used to perform basic input/output operations, complex mathematical operations and many other operations which can’t be performed by any user defined function.

Library functions can be called any program in which the header file for a specific function is included. The most commonly used library functions include getch(), clrscr() etc.

2.  User Defined Function (UDF)

It is a function defined by a programmer depending upon his own requirement. User defined functions are mainly defined for those operations which are repeatedly required in a program. 

A user defined function is a self contained group of statements which can perform a specific task. Once a user defined function has been defined, it can be called within another function in the same program or another program.

It can also take values from other functions and can also return some value. The internal details of a function are hidden from rest of the program.


Types of a user defined function

User defined function can be categorized into four types:

  • Function without arguments and without return value.
  • Function with arguments and without return value.
  • Function with arguments and with return value.
  • Function without arguments and with return value.

i. Function without arguments and without return value

In this type of function, the function has no arguments so it doesn’t receive any values from the calling function.

In this type of function, we don’t specify anything within the parenthesis written after the function name. This type of function doesn’t return any value. So we write void at the place of return type.

Features

  • Function contains no arguments and return type of function is void.
  • Calling function does not receive any value from the function.
  • There is simple passing of control from calling function to called function and vice versa but there is no passing of values between them.
  • Everything is performed within the body of function.

Syntax:

void function(
{
:
}
main()
{
function();   //function() has been called inside main()
}

Program to demonstrate the use of function without arguments and without return value.
#include<stdio.h>
void sum()  /*Called function*/
{
int a,b,c;
printf(“\nEnter two values:=”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“\nsum=%d”,c);
}
int main()
{
sum(); /*Calling function*/
return(0);
}
Output
Enter two values:=4
5
sum=9
Description
we have defined a UDF named sum() which reads values of two  variables, adds them and shows their addition. This function sum() has been called within function main().



ii. Function with arguments and without return value

 In this category of function, we need to specify arguments within the parenthesis written after the function name. Arguments are variables declared individually within the parenthesis. While defining a function, arguments within the parenthesis of function are known as formal arguments.

The arguments within the parenthesis while calling the user defined function are known as actual arguments. The formal arguments receive values from actual arguments.

The number, type and order of actual arguments and formal arguments must be same.

The values of actual arguments are assigned to formal arguments on one to one basis. This function doesn’t return any value so we should write void at the place of return type.

Features:

  • Function contains arguments and return type of function is void.
  • Calling function does not receive any value from the called function.
  • There is passing of values from calling function to user defined function.
  • Input is received from calling function and only manipulation is performed within the user defined function.

Syntax:

void function(int x,int y) /* x and y are formal arguments.*/
{
:
}
main()
{
function(10 ,  20);  /*10 and 20 are actual arguments, 10 goes to x, 20 goes to y */
}

Program to demonstrate the use of function with arguments and without return value.
#include<stdio.h>
void sum(int a, int b) //a and b are arguments/parameters
{
int c;
c=a+b;
printf(“\nsum=%d”,c);
}
int main()
{
int x,y;
printf(“\nEnter two values whose sum is required:=”);
scanf(“%d%d”,&x,&y);
sum(x,y);
sum(10,3);
return(0);
}
Output
Enter two values whose sum is required:=3
4
sum=7
sum=13
Description
In the above program, we have defined a UDF named sum() takes values of two  variables through arguments, adds them and shows their addition.



iii. Function with arguments and with return value

In this category of function, we need to specify the return type before the function name as well as arguments within the parenthesis written after the function name.

Arguments are variables declared individually within the parenthesis. The arguments within the parenthesis while defining function body are known as formal arguments. The arguments within the parenthesis while calling the user defined function are known as actual arguments. The formal arguments receive values from actual arguments.

The number and type of actual arguments and formal arguments must be same. In this type of function, there is passing of values from one function to another function.

In this type of function, input is received from calling function and some manipulation is performed within the user defined function and result of manipulation is returned back to the calling function.

While calling such type of function, we need to assign function calling statement to some variable of data type same as return type of function or we can directly write function calling statement within the printf() function.

Features

  • Function contains arguments and return type of function is some valid data type of C Language like int, float, char etc. It should not be void.
  • There is a two-way communication of values between calling function as well as called function i.e. values are transferred from calling function to called function and again some value is returned back to the calling function.
  • At a time only one value can be returned by this type of function.
  • Input is received from calling function and after manipulation within the user defined function, result is returned back to the calling function.

Syntax:

int function(int x,int y) /*x,y are formal arguments/parameters*/
{
return(x+y);  /*Value of x+y i.e. 30 will be returned and stored in z*/

}
main()
{
int z =  function(10 ,  20); /* 10 and 20 are actual arguments, 10 goes to x, 20 goes to y*/

}

Program to demonstrate the use of function with arguments and without return value.
#include<stdio.h>
int sum(int a, int b) //a and b are arguments/parameters
{
int c;
c=a+b;
return(c);
}
int main()
{
int x,y,z;
printf(“\nEnter two values whose sum is required:=”);
scanf(“%d%d”,&x,&y);
z=sum(x,y);
printf(“Sum=%d”,z);
return(0);
}
Output
Enter two values whose sum is required:=3
4
sum=7
Description
In the above program, we have defined a UDF named sum() takes values of two  variables through arguments, adds them and returns their sum.



iv. Function without arguments and with return value

In this category of function, we need to specify the return type before the function name. In this category of function, function doesn’t contain any argument. In this type of function, some manipulation is performed within the user defined function and result of manipulation is returned back to the calling function.

In this type of function, there is one way communication i.e. value is transferred from called function to calling function. Only one value can be returned by this type of function.

While calling such type of function, we need to assign function calling statement to some variable of data type same as return type of function or we can directly write function calling statement within the printf() function.

Features

  • Function doesn’t contain arguments and return type of function is some valid data type of C Language like int, float, char etc. It should not be void.
  • There is a one way communication of values between called function and calling function i.e. values are transferred from called function to calling function.
  • At a time only one value can be returned by this type of function.
  • All manipulations are performed within the user defined function and result is returned back to the calling function.

Syntax:

int function(
{
return(10);
}
main()
{
int z =  function();
}

Program to demonstrate the use of function without arguments and with return value.
#include<stdio.h>
int sum()
{
int a,b,c;
printf(“\nEnter two values:=”);
scanf(“%d%d”,&a,&b);
c=a+b;

return(c);
}
int main()
{
int s;
s=sum(x,y);
printf(“Sum=%d”,s);
return(0);
}
Output
Enter two values whose sum is required:=3
4
sum=7
Description
In the above program, we have defined a UDF named sum() which reads values of two  variables through adds them and returns their sum..



Tips and Tricks

  • If we don’t specify any return type of a function, it is automatically assumed to be int.
  • If a program contain only one function, it must be main().
  • If a program contain more than one function then these can be called in main().
  • There is no limit on number of functions that might be present in a C program.
  • Each function in a program is called sequence specified by the function calls in main().
  • A function can be called from other function but cannot be defined in the other function.
  • Before using a function we should declare the function so the compiler has information of function to check and use it correctly. The function implementation has to match the function declaration for all part return data type, function name and parameter list. When you pass the parameter to function, they have to match data type, the order of parameter.
  • An argument is an entity used to pass the data from calling function to the called function. Formal arguments are the arguments available in the function definition. They are preceded by their own data types. Actual arguments are available in the function call.



 Test Your Knowledge

Write C program for following:

  1. Program using a function to find largest among two numbers.
  2. Program using a function with arguments and without return type to print table of any number.
  3. Program using a function with arguments and with return value to find factorial of a number.
  4. Program using a function with arguments and with return value to return largest among three numbers.

Write the output of the following programs:

1.
int main( )
{
display();
return(0);
}
void display()
{
printf(“Welcome to C”);
}

2.
int main( )
{
printf(“Welcome to C”);
main();
return(0);
}

  1. int main( )

{
checkvalue(10);
return(0);
}
void checkvalue(int a)
{
if(a>0)
printf(“\nValue is positive”);
else
printf(“\nValue is not positive”);
}

3.
int main( )
{
char ch=’D’;
view(ch);
ch- -;
view(ch);
return(0);
}
void view(char c)
{
printf(“\n%c”,c);
}

4.
int x;
int modify()
{
return(x+=10);
}
int change(int x)
{
return(x+=1);
}
int main()
{
int x=10;
x++;
change(x);
x++;
modify();
printf(“First output:%d\n”,x);
x++;
change(x);
printf(“Second output:%d\n”,x);
modify();
printf(“Third output:%d\n”,x);
return(0);
}

5.
void fun();
int i = 10;
int main()
{
int i =20;
printf(“%d”,i);
i = 30;
fun();
return(0);
}
void fun()
{
printf(“%d”,i);
}

6.

void Test(int a)
{
if(a>0)
{
Test(–a);
printf(“%d,  ”,a);
}
}
int main()
{
Test(5);
return(0);
}

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