Overview of functions in C

The functions can be designed, developed and implemented independently by different programmers. The use of function offers flexibility in the design, development, and implementation of the program to solve complex problems. The following benefits can be achieved by the use of function.


  • • it makes programs significantly easier to understand and maintain
  • • well written functions may be reused in multiple programs. The C standard library is an example of the reuse of functions.
  • • different programmers working on the large project can divide the workload by writing different functions
  • • easier to debug
  • • reduction of work and time
  • • function can be considered as “black box”, because arguments will be passed as input and provides result in the form of return value

The following program shows how a function program looks like.

Program – Addition of three numbers

#include <stdio.h>
void main ()
{   
    float a, b, c, sum;
    float addition (float, float, float);
    printf (“Enter the first number:”);  
    scanf (“%f”, &a);
    printf (“Enter the second number:”);  
    scanf (“%f”, &b);
    printf (“Enter the third number:”);  
    scanf (“%f”, &c);
    sum = addition (a, b, c);
    printf (“\nResult = %f”, sum);
}

float addition (float x, float y, float z)
{
    float s;
    s = x + y + z; 
    return s;
} 

Output
Enter the first number: 3.5
Enter the second number: 5.5
Enter the third number: 6.4
Result = 15.400000


Advantages of using function

There are two reasons of using functions, instead of writing the entire code in one function, main ().

  • 1) The advantages of using functions avoids rewritings the same code over and over again. Suppose we have a section of code that calculates the area of a rectangle. If later in the program it is required to calculate the area of a different rectangle then the same code can be used. It is required to jump back to the place from where it was accessed. This section of code is known as function.
  • 2) By using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided into separates activities, and each activity placed in a different function, then each could be written and checked independently. Separating the code into modular functions also makes the program easier to design and understand.


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext