In this article, you will learn-
C Functions
In this tutorial, you will be acquainted with functions (both client characterized and standard library functions) in C programming. Also, you will realize why functions are used in programming.
A function is a block of code that plays out a particular errand.
Assume, you have to make a program to make a circle and color it. You can make two capacities to take care of this issue:
create a circle function
create a color function
Dividing a complex problem into smaller chunks makes our program easy to understand and reuse.
Types of function
There are two types of function in C programming:
Standard library functions
User-defined functions
Standard library functions
The standard library capacities are worked in capacities in C programming.
These functions are characterized in the header files. For instance,
The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in the stdio.h header file.
Hence, to use the printf()function, we need to include the stdio.h header file using #include <stdio.h>.
The sqrt() function calculates the square root of a number. The function is defined in math.h header file.
User-defined function
You can also make functions according to your need. Such functions made by the user are known as user-defined functions.
How user-defined function works?
#include <stdio.h> void functionName() { ... .. ... ... .. ... } int main() { ... .. ... ... .. ... functionName(); ... .. ... ... .. ... }
The execution of a C program begins from the main() function.
When the compiler encounters functionName();, control of the program jumps to
void functionName()
And, the compiler starts executing the codes inside functionName().
The control of the program jumps back to the main() function once code inside the function definition is executed.
Advantages of user-defined function
The program will be easier to understand, maintain, and debug.
Reusable codes that can be used in other programs
A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.
Please feel free to give your comment if you face any difficulty here.