Functions Overview in C Language part-1

Functions Overview



Function is a basic building block of a C program. Any C program can be assumed as a collection of several functions. Function is a block of code which has some name for identification.


Key Point of Function

Function is a block of code, which has some name for identification
Any C program can have any number of functions. At least one function should be there in the program
Function names must be unique. No two functions can share same names
No keyword is a functions, so do not misinterpreted by the syntax of if, while, switch, return(), sizeof(), etc, they are not functions
Function cannot be defined inside body of another function
Function call, function definition and function declaration are three different terminologies with different meanings, so never used them interchangeably
You can call a function from a function any number of times, but can define only once.
Function is a way to achieve modularization.
Splitting up of a bigger task into several smaller sub tasks is known as modularization
Functions are of two types, Predefined and user defined
printf, scanf are examples of predefined function
main is an example of user defined function
Functions can be defined in any sequence in the program, without affecting the flow of the program
Function execution depends on the call of a function. Function can never execute in the life of program if it is not called from anywhere in the program
Function declaration is also known as function prototype
Function declaration for the predefined functions resides in the header files
Function definitions for all predefined functions resides in the library file
Programmer has to provide declaration of the user defined function
Execution of program begins with main function
Operating system calls main
Any function can call main
Any function can call itself, known as recursion

Benefits of functions


Easy to read
Easy to modify
Easy to debug
Avoids rewriting of code
Better memory utilization


Following example can be used to understand the flow of the program, when it contains several functions.


int main()
{
printf(“\nI am in function main() “);
a();
printf(“\nI am in function main() “);
b();
printf(“\nI am in function main() “);
a();
printf(“\nI am in function main() “);
return(0);
}
a()
{
printf(“\nI am in function a() “);
}
b()
{
printf(“\nI am in function b() “);
a( );
}


Output:

I am in function main()

I am in function a()

I am in function main()

I am in function b()

I am in function a()

I am in function main()

I am in function a()
I am in function main()


Comments

Popular posts from this blog

Notepad, Wordpad and Paint

HOW TO PASS CCC EXAM WITH ONE ATTEMPT (NIELIT)

Inheritance in C++ Language(part 1)