In C Programming Language function is a very useful and important concept. It is a small block of logics that is written to performs some specific tasks. Along with performing some specific task functions also helps to make our code length small which is easy to understand what operation is performed by that function.

Every function has three common parts which are the return type of the function, name of the function, and parameter/argument of a function. In some cases, the function may be without arguments.

Syntax of function

return_type function_name(argument)
{
//statements
//statements
}

Types of C Function

We have two types of c Function:-

  • Library function
  • User define function

Library function

A library function is the in-built functions that come with C programming.
Some of the example of Library functions in C are main(),printf(), scanf(),puts(), gets(), pow() etc.

Examples:

  • main()
  • printf()
  • scanf()
  • puts()
  • gets() etc.

Main()

the main function is a part of every program in c language from where the execution of the program starts. The compiler always starts to check our code from the main.
So, the main function is a very essential part of our program. Thus the main function is always the first line of code executed.

printf()

This function is used to print any output to the console. The output may be anything like some hardcoded text or some calculated values after performing some operations.

scanf()

This function is used to get value from the user as an input.

For example: if you want to get some integer value from user so for that you can use scanf function like

int i;
scanf(“%d”, &i);

User-defined function

C language also provides us the capability to write your our functions to perform some specific task as per requirement.
A user-defined function always require a prototype before we
start using it which is also known as function initialization or function declaration.

This steps should be follow by you when you want to write you own function in C Programming.

1). Function initialization/declaration:- In these steps we initialize our function which has only function name, its return type, and argument.

Suppose we want to write a function which will add two number and return its sum.

So We can initialize it as

int sum(int a, int b);

We can explain above as, int is a return type for function sum, the sum is the name of the above function and a and b are two arguments which will be taken by the sum function.

2). Function body/implementation: This is the main part of the function where all logic is written to perform the operation. In our case, we want a function that will sum two digits.

So body/implementation could be

int sum(int a, int b){
int sum = a+b;
return sum;
}

or

if we optimize our above code we can write as

int sum(int a, int b){
return a+b;
}

3). Function call: Function call means calling the function to get some computational output for which we have written that function.

Here we have written function sum which will return output after adding two numbers. So for that, we need to call sum function from somewhere to bypassing the required parameter. In our case, the required parameter is two numbers.

we can call sum function like

int result = sum(2,3);

here sum function is taking two numbers 2 and 3 as an input and after computation, it will return output 5 which will be stored in the result variable. Here result variable is of int type because sum will return int value.

Lets see the complete program using function so that you can get the idea of how to use function in C

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include<conio.h>
int sum(int a, int b);
int main() {
    int result = sum(2,3);
    printf("sum= %d", result);
    getch();
}
int sum(int a, int b){
    return a+b;
}

Output:

Advantage of user-defined function:-

  • the function provides reusability and breaks the large code of segment into small code.it helps the user to easily understand, maintain.
  • We can divide our workload using a function.