What is fuction in C

What is fuction in C

First the definition:A function is a named, independent section of C code that performs a specific task and optionally returns a value to the calling program. Now take a look at the parts of this definition:

1) A function is named. Each function has a unique name. By using that name in another part of the program, you can execute the statements contained in the function. This is known as calling the function. A function can be called from within another function.

2) A function is independent. A function can perform its task without interference from or interfering with other parts of the program.

3) A function performs a specific task. This is the easy part of the definition. A task is a discrete job that your program must perform as part of its overall operation, such as sending a line of text to a printer, sorting an array into numerical order, or calculating a cube root.

4) A function can return a value to the calling program. When your program calls a function, the statements it contains are executed. If you want them to, these statements can pass information back to the calling program.

That’s all there is in regard to defining a function. Keep the previous definition in mind as you look at the next section.

Code Example

      float average(float age[])
          int i;
          float avg, sum = 0.0;
          for (i = 0; i < 6; ++i){
              sum = sum + age[i];
          avg = (sum / 6);
          return avg;
          int main ()
          float avg, age[] = { 23.4, 55, 22.6, 3, 40.5, 18};
          avg = average(age); /* Only name of array is passed as argument. */
          printf("Average age=%.2f", avg);
          return 0;


Average age=27.08    

What are the advantages of using functions in C?

  • Avoid repetition of codes.
  • Increases program readability.
  • Divide a complex problem into simpler ones.
  • Reduces chances of error.
  • Modifying a program becomes easier by using function.

Why main function is used in C?

The only difference is that the main function is “called” by the operating system when the user runs the program. The execution of a C program starts from the main() function. The function main( ) invokes other functions within it. … The main function is simply the entry point or the start point of your program.