A function is a set of instructions in the C language. Which is called a program."
When we write big programs, then many times there is a problem that for the same work, some statement has to be written again and again, but in simple terms, let us understand this thing through an example, suppose we get the sum of total marks obtained in the class of 5 students. To calculate the sum of the marks of every student, we have to write the program code 5 times.
By writing a program in this way, the length of the program will increase and the program will become very difficult and complicated. To avoid this problem, we write a rotten program separately and whenever we have to do any calculation, we use that sub-program. In this way, the code of a particular low is not written again and again in the program, it is written separately and is used according to the need. This is called a sub-program.
Using a function, a program can be divided into many small parts, that is, we can also say that a function is a group of program codes that are created for a specific task. The function works like a black box, it takes data from any other function and returns the value according to the arrangement. The codes written inside the function remain invisible, in the main() program, no one knows what is happening in a function. There are many benefits to creating and using the function as needed.
some of which are as follows: --
- In a function, a complete group of repeating statements is given and whenever the main() program needs that statement group, then that function is called in the main() program, in which the length of the main() program is reduced and the possibility of mistakes is reduced.
- Functions are easy to understand, if there is a mistake in any function, then we do not have to check the entire program, but only that function has to be debugged.
- Once the function is created, we do not have to write that function back in any other program by saving it in a separate source file.
A function is a set of instructions in the C language. Which are called by some program.
In C language, the function is divided into two parts.
1- Library function
2- user-defined function
1. Library function: -- This is the default function used in the C language. Which have been defined earlier.
Ex:- printf ( )
Scanf( )
2. User-defined function:-- The program is made something else and reusable by using the function. There are three steps to create the function and to use it in the program.
You may also like: - High demand skills in 2023?
(A) Function prototype:- It provides information to the compiler about the name of the function, the type of argument, and its return value.
Ex :- void sum(int x ,int y)
(B) Definition of function:- In this, the compiler is given information about what is the function of that function.
Ex:-
# include <stdio.h>
Void (int x ,int y)
{
Int z;
Z= x+ y
Printf (z);
}
(C) Calling of function: - To call the function, the name of the function is its argument and that line is terminated inside the bracket.
# include < stdio.h >
Void ( )
{
Int a=10; , b=20;
Sum(a, b);
}
There are two ways of calling the function.
(i) Call by value:- In this method copy is done on the actual perimeter. And the function then uses that as an argument. In this type of call, the variable can be protected. Because the variable name cannot be changed inside the function.
(ii) Call by reference:- In this method, the reference of the original variable is called. So that in the called function the copy of the original value is not made. But the variable will be called with a different name which will be the reference to it. In the call-by-reference method, memory is used through the reference pointer.
Note:- If this article is useful for you then do comment below for the feedback .it will be very beneficial for us & also share it with your friends and classmates, or if you have any questions related to any topics and subjects, then you can do comment below or can write a mail to us. Thanks.