Functions in C

Functions in C

  • In other programming languages the function among C language is also known as process or subroutine.
  • To perform any task, we can create function. A function can be called many times. It provides modularity and code-re-usability.
  • A number of statements are called a function, grouped into a single logical unit. 
  • Using the function facilitates programming, since repeated statements can be grouped into functions. 
  • Splitting the software into a different feature makes it more readable and easier to maintain.
  • In each C program it is necessary to have a single' main' function, along with other functions that the programmer uses / defines.
  • A function definition consists of two main components: the function header and the function body. 
  • The function header is the return value type of data followed by the name of the function and (optionally) a set of arguments separated by commas and parenthesized. 
  • That argument is followed by associated form to which function accepts. Function header statement can be generally written as
return_type  function_name (type1 arg1,type2 arg2,..,typen argn)
  • where return_type represents the data type of the item that is returned by the function, function_name represents the name of the function, and type1,type2,…,typen represents the data type of the arguments arg1,arg2,..,argn.
  • Example: Following function returns the sum of two integers.

            int add(int p,int q)
            {
            return p+q;          //Body of the function
            }
  • Here p and q are arguments. 
  • The arguments are called formal arguments or formal parameters, since they represent the name of the data item that is transferred from the calling portion of the program into the function. 
  • The corresponding arguments in the function call are called actual arguments or actual parameters, because they define the actual transferred data items.
  • A feature may be invoked whenever needed

e.g., add(5,10);
The following condition must be satisfied for function call.
  • The number of arguments in the function calls and function declaration must be same.
  • The prototype of each of the argument in the function call should be same as the corresponding parameter in the function declaration statement.


Advantage of functions in C

There are many advantages of functions.

1) Code Reusability

By creating functions in C, you can call it many times. So we don't need to write the same code again and again.

2) Code optimization

It makes the code optimized, we don't need to write much code.
Suppose, you have to check 3 numbers (781, 883 and 531) whether it is prime number or not. Without using function, you need to write the prime number logic 3 times. So, there is repetition of code.
But if you use functions, you need to write the logic only once and you can reuse it several times.

Syntax to declare function in C

return_type function_name(data_type parameter...)
{  
//code to be executed  
}  

Syntax to call function in C

variable=function_name(arguments...);

1) variable: The variable is not mandatory. If function return type is void, you must not provide the variable because void functions doesn't return any value.
2) function_name: The function_name is name of the function to be called.
3) arguments: You need to provide same number of arguments as defined in the function at the time of declaration or definition.

Example of function in C


#include <stdio.h>      
#include <conio.h>    
//defining function    
int cube(int n){  
return n*n*n;  
}  
void main(){      
int result1=0,result2=0;    
clrscr();       
result1=cube(2);//calling function  
result2=cube(3);    
printf("%d \n",result1);  
printf("%d \n",result2);    
getch();      
}  

Output

8
27

More Example

Program to add two numbers using function
#include <stdio.h>
#include <conio.h>
int add(int p,int q);                       // function prototype
void display(int p, int q, int r); // function prototype
int main()
        {
        int a,b,c;
        clrscr();
        printf("Enter two numbers\n");
        scanf("%d%d",&a,&b);
        c=add(a,b);
        display(a,b,c);
        getch();
        return 0;
       }
int add(int p,int q)          //This function returns sum of a and b.
        {
        return(p+q);
        }
void display(int p, int q, int r). //This function returns nothing
        {
        printf("Sum of %d and %d is %d",p,q,r);
        }

Post a Comment

Previous Post Next Post