Functions in C programming language

In C, a function is a block of code that performs a specific task. The function provides reusable code. In large programs, debugging and editing tasks is easy with the use of functions.

Parts of Function :
  1. Function Declaration
  2. Function Definition
  3. Function Call
Function Declaration : A function declaration tells the compiler about a function's name, data type, and parameters.

Syntax :
data_type function_Name(argument_list)

Example :
int addition();

Function Definition : It contains the body of a function which are to be executed. A body of a function consists of statements which are going to perform a specific task. A function body consists of a single or a block of statements.

Syntax :
data_type function_Name(argument_list)
{
     //  body of the function
}

Example :
int addition();
{
       // body of the addition() function
}

Function Call : A function call means calling a function whenever it is required in the program.

Syntax :
function_Name(argument_list)

Example :
#include<stdio.h>
int addition(int a, int b); // function declaration
int main()
{
      int a,b,sum;
      sum = addition(2,4); // Calling of the function
      printf("Result is %d",sum);
}
int addition(int a, int b) // function definition
{
      return a+b;
}

Output :
Result is 6


Types of functions : There are two types of functions in C programming.

  • Library Functions : This are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.

  • User-defined functions : This are the functions which are created by the C programmer, so that they can use it many times. It reduces the complexity of a big program and optimizes the code.

0 Comments