Introduction to C
Data Types in C
Operators in C
Hello World Program
Loops in C
Single Dimensional Array
Two Dimesnional Arrays
Array Example Programs
Definition:- A function is a self-contained block of statements that perform a coherent task of some kind.
Explanation and more detail with example
A function groups a number of program statements into a unit and gives it a name. With this, the function can be called in the program which in result, will execute the code written in the function. So, it helps in reducing the code. Or we can say that we need not to repeat the same code again and again. Suppose, you have written some code which is required many times in the program. If you don’t function then you have to write the same code again and again in the program. But, with the help of function, you can execute the same code by just calling the function in one line only. So, function is much useful in reusability.
For example, suppose you hire some person to do a particular task. You just have to tell him to do the task, You don’t need to give him instructions how to do the task. The person knows how to perform it. Also, you don’t mean how the task has been done by the person. Only you need to complete the task. In the same way, you just call a function in C and execute the code written in it. You may or may not be aware of the content in the function. But your work will be done.
Example of function with a simple program in C
Explanation for the above program
In the above program, there are two functions namely myfunc() and main(). Main() is the part from which the execution of program starts as u know. Lets focus on myfunc() function. This has been used three times across the program. We shall understand these one by one.
First one is function prototype declaration which is written as void myfunc();
myfunc() is the name of the function and 'void' is the return type for the function which tells that we are not going to return anything from the function.
Second use of the function myfunc() is
printf(“My First Function Output. \n”);
Here, it is the function definition. It is the part in which we write what we have to execute from the function. We can perform any task from the function and we can use other things like loop, switch etc.
Third use of function is:
It is the function call. It will execute the code in the function and gives its output. One thing to be noted here is main() function calls the myfunc() function here in the program. It means when the myfunc() is called, the activity of main() is temporarily suspended. When the myfunc() runs out of statements to execute, the control then returns to main() function. Thus, main is the calling function and myfunc() is the called function.
Few Important points to be remembered for functions in C
1. A function can be called from any other function. It is applicable for main() also. It can also be called from any other function
2. A function can be called any number of times.
3. It is not necessary to define the functions in the same order as the functions are called.
4. A function can call itself. It is called recursion.
5. It is possible to call a function from another function but its definition can’t be provided in the other function.