Introduction to Functions in C language

“Don’t Stop when you’re tired, Stop when you’re done”
Good morning everyone!!!
Today I present a very important topic , Introduction to functions in C language.
1) It’s Definition
2) Types
3) Standard library function in detail with example.
4) Advantages of using C library functions.
Let’s start quickly 🙂
•Definition of Function:-
1) A function is a group of statements that together perform a task. Every C program has at least one function, which is main( ) function and all the most lengthy programs can define additional functions.
2) You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.
•Types of Functions:- There are mainly two types of functions.
1)Standard Library Functions
2)User Defined Functions
Today we will discuss Standard Library function in detail.
•Standard Library Functions:-
1) C Standard library functions or simply C Library functions are inbuilt functions in C programs.
2) The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program.
3) For example, If you want to use the printf() function, the header file <stdio.h> should be included.
•Example: Square root using sqrt() Function.
#include<stdio.h>
#include<math.h>
main()
{
float num, root;
printf(“Enter a number: “);
scanf(“%f”, &num);
// Computes the square root of num and stores in root.
root = sqrt(num);
printf(“Square root of %.2f = %.2f”, num, root);
}
Output:-
Enter a number: 169
Square root of 169.00 = 13.00
•Advantages of Using C library Functions:-
1) Use of functions enhances the readability of a program. A big code is always difficult to read. Breaking the code in smaller Functions keeps the program organized, easy to understand and makes it reusable.
2) The C compiler follows top-to-down execution, so the control flow can be easily managed in case of functions. The control will always come back to the main( ) function.
3) It reduces the complexity of a program and gives it a modular structure.
4) In case we need to test only a particular part of the program we will have to run the whole program and figure out the errors which can be quite a complex process. Another advantage here is that functions can be individually tested which is more convenient than the above mentioned process.
5) A function can be used to create our own header file which can be used in any number of programs i.e. the reusability.
•List of some Standard Library Functions in Ctype.h

Example:- Write a C program to check whether the alphabet is uppercase or lowercase.
#include<stdio.h>
int main()
{
char ch = ‘A’;
if (isupper(ch))
printf(“\n Entered character is uppercase character”);
else
printf(“\n Entered character is not a uppercase character”);
}
Output:
Entered character is uppercase character
Like wise we can write many more programs using C library functions.
Soo this is all about standard library function.
We will see User Defined function in detail in the upcoming article.
Till then goodbye……….. Take care.
Have a fantastic day ahead 🙂
See you all in next article.
And please give your precious comment 🙂
You must log in to post a comment.