C - Function

<<Previous

Next >>





Usually, we want our computer programs only 20 to 30 lines but in real world the computer programs are much longer it can run several thousand or million lines of code. If you put an entire program in main(), you would need to spend lot of time to find any specific task. Also, the same code might need to be used at several points of the program and this cause repetitive code. That's why, we break our program into sections that makes us to write and maintain the program easier. These sections are called functions in C. In this section we are going to see function definition, function declaration and their types.

What is function?

A long C program breaks into many small routines that is known as functions. Each function contains block of statements enclosed by "{ }" which performs one specific task. One of the program's function is named main() which is always required and listed first. Therefore, a C program is not just one long program, it is made up of many small functions. Function is also known as procedure or method or subroutine. The function can be called many times.

Advantages of function

  • Easy to write and maintain the programs.
  • Provides code reusability and modularity.
  • Easy to track and debug the errors.
  • Called many times and from any place in a program.
  • Used to improve understanding of programs.
  • Used to achieve functionality.

Terms used in function

  • Function declaration  :  It signals the compiler about function name, return-type and parameter lists.
  • Function definition    :  It provides the actual body of the function.
  • Formal parameter      :  These are the names used inside the function to refer to its arguments.
  • Actual arguments      :  These are the values that are passed as arguments when the function is actually called.

Notice that the terms parameters or arguments are used interchangeable.

Function declaration

A function must be declared before it is used. A function declaration or prototype is an interface specification. It consists of function's name, return type and parameter lists. The syntax of the function declaration is given below:

return-type functionName(data-type parameter1, data-type parameter2,...); //parameter1 and 2 are the formal parameters

For Example:

int add(int a,int b);

A function is declared without parameter name but data-type must be required. This is also a valid declaration. So the above function declaration can also be written as

int add(int, int);

Notice that the function declarations are usually placed in the top of source file.

Function Definition

A function definition provides the actual workings of the function. It contains return type, function name which is used to refer to it in a program, list of parameters or arguments which are passed from outside and body of the function which enclosed by curly braces "{ }". The following is the form of function definition

return-type functionName(data-type parameter1, data-type parameter2..){

//function body

}

The return type can be any data type. Actually,the functions may or may not return the value. If the functions don't return any value, then use the void for the return type.

For example:
This function adds two int parameters a and b and return the addition of two number
int add(int a, int b)
{
   int result;
   result = a + b;
  return(result);
}

Calling a function in C

A C function can be called from the main() function. It can be called by using function name and with or without arguments. When our program(calling function) calls the function, then the control is transferred to that particular function(called function). A called function can perform the specific task and then return the control to the calling function.

Syntax:
      variable-name = functionName(arguments list);//arguments list are the actual parameter.

Here the variable-name is optional. If the function returns a value, then that value will be stored into the given variable-name. Suppose, if the function return type is void, then the variable is not required.

While calling, we need to pass the required arguments to the called function. In general, the arguments are passed in two ways that are:

  • call by value
  • call by reference

Both of these methods pass the variable to the called function from calling function. There is also a way to return a value from called function back to the calling function by using "return" statement.

Difference between call-by-value and call-by-reference

call by valuecall by reference
The copy of actual value is passed along with function call An Address of actual value is passed to the function call
Different memory location is allocated for actual and formal parameter Both actual and formal parameter use the same memory location
Any modification of parameter's value inside the called function is not affected the original value of the parametersChanges made on the parameter's value inside the called function is reflected the original value of the parameters

<< Previous

Next >>