Function in C.

Function in C

Function in C.

  • Also in other programming languages the function in C language is known as procedure or subroutine. We can build function to execute any task. Several times one function can be named. This offers the molecularity and re-usability of code.
  • One function is called a set of statements grouped into a single unit. 
  • Dividing the program into a different feature makes the program more readable and easier to maintain.
  • It is necessary to have a single function ‘main’ in every C program, along with other functions used/defined by the programmer.
  • A function description 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, because they represent the name of the data item that is transferred into the function from the calling portion of the program. 
  • The corresponding arguments in the function call are called actual arguments or actual parameters, since they define the data items that are actually transferred.
  • 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.
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.

Post a Comment