The Function in C programming language is the basic building block which is used to implement modularity.

What is Function in C?

It is a block of a statement which performs some operation.
Function definition: Function is a set of a statement in the block which is used to perform specific tasks.

Why function used in c?

Suppose we want to perform the same task more than once. For this, we have two choices
1. Rewrite the same task repeatedly
2. Reuse the written task repeatedly.
Obviously the second option is a better choice. Function in c is the block of code with a specific task which we can write once and can call any time as required.

In c First, we have to declare the function after that we define the function then we call the function to execute function.

Function structure in c:
Syntax:

<Return type> function _name (parameters)
{
   //Block of code
}

Return type: return type in function means value return by the function, we write type of value which function returns that is int, float, etc. If function does not return anything we mention void as a return type of function.

Function name: it’s the name of the function.

Parameter list: The parameters are the values which we want to pass to the function. The function accepts any number of parameters. If we do not want to pass a value to function we mention the void as the function parameters.

Body of code: It is a number of statements which function perform.

Implementing functions involves three steps which are as follow:

  1. Function declaration
  2. Function Definition
  3. Function Call

Function Declaration

Before using the function we declare the function. The declaration of function involves specifying the prototype.
Function declaration as follow:
Syntax:

<return type> function_name (parameters list);

Example:
int function1(void);

Function Definition

After declaration, we define the function. The definition is the body of the function. Syntax:

<return type> function_name (parameters list);

Function example:
int function1(void){
printf(“function”);
}

Function call

To execute the code of function we have to call to the function. We call the function as follow:
The syntax for function call

function_name(parameters);

Example :
function1(void);

In C programming, Function is a built-in function or user-defined functions.

Types of functions in c

Function in C
Function in C

1. Predefined functions

The functions which are already defined in c header files such as puts(), gets(), getch(), etc are predefined functions. Whenever we need them we just call predefined functions.

2. User-defined functions

The function which we create in a program is a user-defined function.

Program for function in c

//Implicit declaration of function

#include <stdio.h>
//function declaration.
int add(int,int); //function with parameter and return type
void display(void); //function with no parameter and no return type

int main()
{
int a,b,c;
a=10;
b=10;

printf("calling function:\n");
c=add(a,b); //function call

printf("after function call:\n");
printf("Addition = %d",c);

display(); //function call

return 0;

}

//Defining the implicit function .

int add(int a,int b){
int c=a+b;
return c;
}
void display(void)
{
printf("\nfunction with no parameter and no return type");
}

Output:

Function in cImplicit declaration of Function

In c, before using the function we have to declare the function.
Declaring the function before using the function in the program is known as an implicit declaration of function.
We have to declare the prototype of the function before main function.
If we do not specify the function declaration then the compiler will show you an error as an implicit declaration of function with the function name.
Either for this we declare the function before the main or we directly write a definition for the function.

Example :
int fun(); // Implicit declaration of function.
void main()
{

// code
}

C program to implicit declaration of function

//Implicit declaration of function

#include <stdio.h>
void function1(){

printf("\n Function 1 definition \n "); //directly defining function.

}

void function2();
int main()
{
printf("\n Main function");

printf("\n Calling function 1 -----");

function1();

//calling implicit function .

printf("\n Calling function 2 -------");

function2();

return 0;

}

//Defining the implicit function .

void function2()

{

printf("\n Function 2 definition ");

}

Output :

Implicit declaration of Function

Advantages of functions

1. It improves the readability of code.
2. Improves reusability of code.
3. It makes debugging of code easier.
4. Reduces the length of code.

Write A Comment