In this tutorial, you will learn about functions in c programming and the types of functions in c programming.

functions in c programming

C programming functions

A function is a single comprehensive unit (self-contained block) containing a block of code that performs a specific task.

This means function performs the same task when called which avoids the need of rewriting the same code again and again.

Types of functions in C programming

C functions are classified into two categories:

  1. Standard Library functions
  2. User defined functions

Standard Library functions

Library functions are built-in standard function to perform a certain task. These functions are defined in the header file which needs to be included in the program.

Click here to learn about standard library math function.

For examples: printf(), scanf(), gets(), puts() etc… are standard library functions.

User defined functions

The user defined functions are written by a programmer at the time of writing the program. When the function is called, the execution of the program is shifted to the first statement of called function.

Good Programming Practice
Each user-defined function should perform a specific task and function name should express that task which promotes reusability.



Example: C program to calculate the area of square using function

/* program to calculate the area of square */
#include <stdio.h>
void area();   //function prototype
int main()     //function main begins program execution
  area();    //function call
  return 0;
}           // end main
void area()  //called function
  int square_area,square_side;
  printf("Enter the side of square :");
  square_area = square_side * square_side;
  printf("Area of Square = %d",square_area);
}          //end function area


In the above program, function area is invoked or called in the main function. The execution of program now shifts to called function area which calculates the area of square. The void in funtion prototype indicates that this function does not return a value.

Syntax of a function definition

return_value_type function_name (parameter_list)

The function_name is an identifier.

How does function work?

c function

Good Programming Practice
To avoid ambiguity, we should not use the same name for functions arguments and the corresponding parameters in the function definition.


Common Programming Errors
Function definition inside another function results in a syntax error. Remember semicolon at the end of function prototype.