Functions in C Programming

Introduction

  • Programmers are allowed to break the program into different segments known as Functions.
  • They can be written independently of each other.
  • They are supposed to define a well-defined task. Hence, the code of one function is completely insulated from the other functions.

Need of a function

Following are the needs of the functions:
  • It divides the program into separate well-defined functions so that each function can be written and tested separately.
  • Understanding, coding and testing becomes very easy as they are separated.
  • Large programs affect the memory space. Also, in absence of functions, there would be countless number of code lines in the main() function.
  • A set of functions are available in the C library which helps to speed up the work of the programmers.
  • As the functions are divided the workload can also be divided among the programmers.
  • Custom functions can also be written by the programmer according to his needs.

Function Declaration, Definition, Call

Function Declaration

Before any function is used the compiler should know the number and the type of parameters that the function has to receive and the data type of the value that will be returned by the program.

Syntax:
return_data_type function_name (data_type variable1, data_type variable2, …..);

Where,

function_name - valid name for the function. It should be a meaningful name that will clarify what all tasks it will perform.
return_data_type - it is used for specifying the data type of the value that is returned to the calling function after the processing.
data_type variable1, data_type variable2, ….. - it is a list of variables with their specified data types. They are called as arguments or the parameters.

Some of the examples of how a function can be declared are as follows:
1. char convert_to_lowercase (char ch);
2. float bonus (int x, int y);
3. int_find_smallest ( int x, int y, int z);
4. double multiplication (float x, float y);
5. void swapping (int x, int y);
6. void print (void);

Function Definition

Whenever a function is defined, its space is allocated in the memory.

There are two parts for defining a function.
i) Function header
ii) Function body

Syntax:

return_data_type function_name (data_type variable1, data_type variable2, …..);
{
        …...................
        statements;
        …...................
        return (variable);
}


  • The arguments that are given in the declaration have to be the same in the definition also.
  • The statements written within the curly braces ({}) is the body of the function which contains the code to be performed.
  • A function can be defined either before or after the main() function.

Function Call

  • Whenever the function is invoked the compiler skips on to the called function for executing its statements.
  • Once the called function is executed the control of the program is passed back to the calling function.
Syntax:
function_name (variable1, variable2,....);

  • The function definitions are always placed in separate header files which can also be included in other source files of C from where they can be used easily.

Example: Write a program to add two integers by using a function.

#include <stdio.h>
int add (int x, int y);
void main()
{
        int n1=30, n2= 40, total = 0;
        total = add(n1,n2);
        printf ("\n Addition of two numbers = %d", total);
}
int add(int x, int y)
{
        int r;
        r = x + y;
        return r;
}


Output:
Addition of two numbers = 70