C Programming - Functions |
Functions
In C programming language, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide the reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedure or subroutine in other programming languages.
Advantage of functions in C
Here is the list of advantages of C functions.
- By using functions, we can avoid rewriting the same logic/code again and again in a program.
- We can call C functions any number of times in a program and from any place in a program.
- We can track a large C program easily when it is divided into multiple functions.
- Reusability is the main achievement of C functions.
- However, Function calling is always overhead in a C program.
Function Aspects
There are three aspects of a C function.
- Function declaration A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type.
- Function call Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.
- Function definition It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function.
Types of Functions
- Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
- User-defined functions: are the functions that are created by the C programmer, so that he/she can use them many times. It reduces the complexity of a big program and optimizes the code.
Return Value
A C function may or may not return a value from the function. If you don't have to return any value from the function, use void for the return type.
Let's see a simple example of a C function that doesn't return any value from the function.
Example without return value:
void hello(){
printf("hello c");
}
If you want to return any value from the function, you need to use any data type such as int, long, char, etc. The return type depends on the value to be returned from the function.
Let's see a simple example of a C function that returns the int value from the function.
Example with return value:
int get(){
return 10;
}
C Library Functions
Library functions are the inbuilt function in the C language that are grouped and placed at a commonplace called the library. Such functions are used to perform some specific operations. For example, printf is a library function used to print on the console or terminal. The library functions are created by the developers of compilers. All C standard library functions are defined inside the different header files saved with the extension .h. We need to include these header files in our program to make use of the library functions defined in such header files. If we took an example of library functions such as printf/scanf we need to include stdio.h in our program which is a header file that contains all the library functions regarding standard input/output.
A list of mostly used header files is given in the following table.
stdio.h
- This is a standard input/output header file. It contains all the library functions regarding standard input/output.
conio.h
- This is a console input/output header file.
string.h
- It contains all string related library functions like gets(), puts(),etc.
stdlib.h
- This header file contains all the general library functions like malloc(), calloc(), exit(), etc.
math.h
- This header file contains all the math operations-related functions like sqrt(), pow(), etc.
time.h
- This header file contains all the time-related functions.
C User-Defined Functions
User-Defined functions are built by users in C language that are used at commonplace user can modify and create according to their use Such functions are used to perform some specific operations. For example, If we create a function to add to numbers
int main() {
int num1, num2, res;
printf("\nEnter the two numbers : ");
scanf("%d %d", &num1, &num2);
//Call Function Sum With Two Parameters
res = sum(num1, num2);
printf("nAddition of two number is : ");
return (0);
}
int sum(int num1, int num2) {
int num3;
num3 = num1 + num2;
return (num3);
}
Output
Please comment your opinion here.