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
- return type of the function
- name of the function
- parameter/argument of a function.
In some cases, the function may be without arguments.
Syntax of function
[c]
return_type function_name(argument)
{
//statements
//statements
}
[/c]
Types of C Function
We have two types of c Function
- Library function
- User define function
1. 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.
1. 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.
2. 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.
3. 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
[c]
int i;
scanf(“%d”, &i);
[/c]
2. 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
[c]
int sum(int a, int b);
[/c]
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
[c]
int sum(int a, int b){
int sum = a+b;
return sum;
}
[/c]
or
if we optimize our above code we can write as
[c]
int sum(int a, int b){
return a+b;
}
[/c]
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
[c]
int result = sum(2,3);
[/c]
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.
Example 1: C Program to find the sum using Function
[c]
#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;
}
[/c]
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.