Skip to main content

Functions and Arguments in C Language


• A Programs could be broken down into functions that perform single cohesive task, that is relatively independent of the rest of the code.

• Thus a function is a self-contained block of statements that perform a coherent task of some kind.

• These cohesive modules may be called from the main module which wants to achieve this task in order to complete a complex task.

• Creating functions help in Reducing complexity and Avoiding duplicate code and Simplifying complicated boolean tests and obiviously Improves re-usability .

• Declaration and definition of functions are two different things.

• Declaration is the prototype of the function, that includes the return type, name and argument list to the function and is ended with a semicolon.

• Function definition is the actual function code.

void display(); //Function Declaration 
int main() 
 display();  //Function Call 
return 0; 
 void display()     // Function Header 
 //Body of the function 
printf(“\n Hello World”); 

Note : 

  • Here // sign denotes the Comments Part for a single line .

Function definition

Basically , A function definition has two principal components:

  • function header 
  • body of the function  

The function header is the data type of return value followed by function name and  (optionally) a set of arguments separated by commas and enclosed in parenthesis.

Body of the function includes the statements to get executed in response to the function call.

Function Header
• The general form of a function header is     

return_type  function_name (type1 arg1,type2 arg2,..,typen argn)  

Note :

  •  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,...,typen represent the data type of the arguments arg1,..,argn.

Example :
Following function returns the product of two integers.

int multiply(int i,int j) 

int product= i*j;        //Body of the function return product;        

Note :

  • In order to calculate the product, we must pass two values to the function and that is done by using arguments. Here i and j are the arguments which represents the multiplicand and the multiplier. 
  • Arguments are sometimes called as "parameters".

Returning Value from functions :

  • A function may or may not return a value. 
  •  Return type may be int, float, char or any other valid data type.
  • The keyword return is used to return some value from the function. 
  • It does two things,returns some value to the calling program and also exits from the function.
  • There may be some functions which do not return any value. For such functions, the type of return value is void

Example : 

void Display(int); //This function do not return any value. 
int Sum(int,int);    //This function  can return any integer value.

Calling Functions

  • A function can be invoked whenever it is needed. 
  • It can be accessed by specifying its name followed by a list of arguments enclosed in parenthesis and separated by commas.

Example :

multiply(9,10) ;

Note :

  • In order to calculate the product, we must pass two values to the function and that is done by using arguments.
  • Here 9 and 10 are the arguments/parameters which represents the multiplicand and the multiplier. .

Passing data to Functions

  •  Functions may have to accept data that is required for processing, from other functions from where it is called. 
  • Such data that is passed into a function from where it is called is known as parameter.

In C, data can be passed to functions by two ways

  1. Pass by value 
  2. Pass by reference

1. Pass by value - In pass by value a copy of the argument is made and then passed to the function.
2. Pass by reference( Memory Address of value ) - In pass by reference a function receives a reference to a variable. In this way the value of the argument in the calling function can be modified by the called function. This is done by using pointers.

Recursive Functions

  •  A recursive function is one which calls itself. 
  • Recursive function must have at least one exit condition that can be satisfied. 
  • Recursive function is closely related to definitions of functions in mathematics. So we can solve factorial problems using recursive function. 

Note : 

  • Formula to find factorial of a number is N! = N*(N-1)*(N-2)…2*1. 
  • Or in a recursive way we can define it as N*(N-1)! 

Example : Lets see how can we implement this recursive concept in C to find the factorial.

int factorial(int n)  
 if(n<= 1)  
return 1; 
 else n*factorial(n-1);

 int main() 
 printf("factorial of 9 is %d",factorial(9)); 
 return 0;