Table of Contents
Introduction
From declaring a variable to printing something, to decision making, to iterations using loops, we have learned many basic concepts of C language till now. This and the next few chapters are going to be new but related to our basics. In this article, we will learn about Functions in C.
A function in C is a set of instructions/statements grouped as code with a particular name. It can be called by its name anytime and anywhere in the program whenever needed.
For example, a user wants to maintain the record of the candidates based on their voting eligibility. So, he will make a function with a code that asks for necessary details like the candidate’s age, etc., and gives output. Thus, whenever a candidate arrives, there is no need to write the whole code again, just call the function and proceed accordingly.
Why functions in C?
The above example gives a relevant answer to this question. Functions reduce the workload of programmers and save time. In short, rewriting the same code can be avoided by using functions.
Moreover, functions make our code neat and easily readable.
Benefits of functions in C
- Splits up a bigger task into smaller sub-tasks, and every sub-task is also a function.
- Better memory utilization.
- Easy to read and modify.
Types of functions in C
There are two types of functions:
- Library functions
- User-defined functions.
Library functions in C
Library functions are t functions that are pre-defined in the C library and are used by the programmers whenever needed. ‘printf()’ and ‘scanf()’ are predefined functions that have already been used in our programs.
Moreover, using such library functions has many advantages like they work very well and are easy to use. These predefined functions save users time. Printing anything on the screen, taking the output from the user, and many more are already defined. This enables the programmers to work efficiently.
User-defined functions in C
While writing a program, we make many functions according to our needs so that our code runs well. All such functions defined by the user are user-defined functions.
User-defined function mainly consists of 3 parts:
- Function declaration
- Function definition
- Function call
How to declare a function in C?
A function is declared as follows:
Syntax:
return_type function_name (parameters);
Example:
To calculate the area of a rectangle having two dimensions length and breadth. The ‘area’ function is declared as
float area (float length, float breadth);
Here the function named ‘area’ is declared, taking ‘length’ and ‘breadth’ of float type as input and returning the area value of type ‘float’.
float average (int a1, int a2);
The function named ‘average’ is declared, which will return the average value of type ‘float’ after calculating it from two inputs ‘a1’, ‘a2’ of type ‘int’.
Defining functions in C
Syntax:
return_type function_name (parameters)
{
//code
}
Let’s define the ‘area’ function declared above.
float area (float length, float breadth)
{
float A;
A=(length*breadth);
return A;
}
‘A’ is a variable of type ‘float’ declared inside the ‘area’ function. It is only valid inside the function and not outside it. Such variables are called ‘local variables’. Thus, ‘A’ is a local variable.
Note: Specifying the parameter type along with the parameters is necessary while defining the function.
The calculated value of ‘length*breadth’ will be assigned to ‘A’.
return A; this function will give us the value of ‘A’, which type ‘float’.
Calling a function in C
Till now, we declared a function and defined it according to our use. The function can be called in the program once or more according to the user’s need.
When a call is made, the control will move to that function, and the code written in its definition will be executed. After that, the control passes to the main program.
Syntax:
function_name (parameters);
‘area’ function can be called as
area (length, breadth);
So, now that we are ready with functions, let’s write a program to find the area of a rectangle.
#include<stdio.h> float area(float length,float breadth);// declaration int main() { float l, b; float a; printf("enter the value of length:\n"); scanf("%f", &l); printf("enter the value of breadth:\n"); scanf("%f", &b); a=area (l, b); // function call printf ("Area is %f\n", a); return 0; } float area (float length, float breadth) // definition { float A; A=(length*breadth); return A; }
Output:
float area(float length, float breadth); The function is declared first even before the ‘main’ function so that when the ‘main’ function encounters the ‘area’ function during the execution of code, it must know that there exists a function named ‘area’. It will search for the function definition.
The function named ‘area’ declared with ‘float’ type with 2 arguments of ‘float’. It means that while calling, we must parameters pass two ‘float’ inputs, and we will get a ‘float’ output in return.
Now come to the ‘function call’ statement in the ‘main’ function.
a=area (l, b); The function call is made by passing two parameters of type ‘float’. Since ‘a’ was also declared as float type, ’80.500000’ gets stored in ‘a’.
When the compiler reaches the function call, it searches for the ‘function definition’, written after the ‘main’ function in the above function.
‘Function definition’ can also be made after declaration.
#include<stdio.h> float area(float length,float breadth) { float A; A=(length*breadth); return A; } int main() { float l,b; float a; printf("enter the value of length:\n"); scanf("%f",&l); printf("enter the value of breadth:\n"); scanf("%f",&b); a=area(l,b); printf("Area is %.3f\n",a); return 0; }
Output:
The function definition is made with its declaration. So in this case, while executing ‘main’ the compiler will know that there is a function named ‘area’ because it is defined above from where it is called.
One more example of a function is given below:
#include<stdio.h> void show (int a) { printf("number is %d\n",a); } int main() { int x; printf("enter a number:\n"); scanf("%d",&x); show(x); return 0; }
Output:
void show (int a), In this statement, the ‘show’ function has return type ‘void’, which means that the ‘show’ function is not returning anything. The rest of the program is quite simple. The only concern to write the above program is to tell about the ‘void’ return type.
The function definition is also possible without passing any argument. See an example of this.
#include<stdio.h> void display () { printf("function with no argument\n"); } int main() { display(); return 0; }
Output:
void display ( ), This function has empty ‘( )’ brackets, which means that we are passing no arguments in the function ‘display( )’.
Points to remember
- The function is an operation/perform task, which once defined, can be used many times/ can be called many times.
- One function in the program must be ‘main( )’.
- A program may have any number of functions.
- No function is defined in another function.
- Every function has its unique name.
- Every program’s execution starts with the ‘main( )’ function.
- Since ‘main( )’ is not a user-defined function, the operating system does the function call for ‘main( )’.
0 Comments