CH 16
CH 16
Why FUNCTION is needed:- If we write our program using only main() function then it may
become large. Testing and debugging of such program may be become difficult for programmer. If program is
divided into small subprograms (functions) then it is much easier to understand, test and debug it.)
What is FUNCTION:-
Function is a group of statements that performs a specific task . The function is also knows as
method, sub-routine or procedure.
We are familiar with the use of main(), printf() and scanf(). They are examples of C functions. Every
executable C program has at least one main() function.
The use of function in program makes it modular. Modularity means partitioning a complex problem
into small sub-problems which are easy to understand and maintain.
◦ Once a problem is divided into small sub-problem, we can write a function for each sub-problem.
The problem is then solved by using all functions together in a program.
There are two categories of functions in C.
◦ Library functions or System defined functions
◦ User defined functions
Library Functions:
The C language standard library provides many built-in functions. They are also known as system
defined functions.
◦ For example printf(), scanf(), sqrt() and cos() are examples of library functions.
Library functions are not required to be written by normal user. These functions are available in
compiled form in C library.
Library functions are placed in various header files.
◦ For example, sqrt() and cos() are related to mathematics hence they are placed in <math.h> header
file.
To use any library function, we have to include its corresponding header file in our program
Advantages of Functions:-
Followings are the advantages of using functions in our program:
Dividing program into functions makes understanding and maintenance of the program easier.
Functions written once can be called many times in our program. This reduces the actual length of
program and thus saves memory.
Functions help us to avoid redundant programming of similar tasks.
Fnction written for one program may be used in other program.
Same function can be used with different values of arguments.
Function Definition:-
If we want to use user defined function in our program then we have to inform this to compiler .
◦ We can inform the compiler by writing function definition.
▪ A function cannot be used without its definition.
In C language. function can be defined using following syntax.
return_data_type function_name (arguments)
{
function statements,
}
Here return_data_type specifies the type of information return by the function.
If the return value from the function is integer value then return_data_type is int.
If function does not return any value then the return_data_type is void.
The function_name specifies the name of user defined function.
The rules for giving name to functions are same as rules of naming variable.
◦ We should give meaningful function name.
▪ For example add () for performing addition, avg() for performing average, etc.
The arguments show the input values provided to the function along with their data types.
◦ For more than one argument, all arguments are separated by commas in the function
definition.
Statements between opening and closing curly brace are known as function body.
◦ Based on requirements in a program, function body may contains local variable
declarations and return statements.
Note:-
That there is no need to define library function as they are already defined.
All user defined functions needs to be defined as function prototype before being used in a program.
Function prototype means declaration of a function before their use in a main () function.
Function Call:-
Every C program starts with function main().
From main() functions we are calling other library or user defined functions.
To call a function, use function name with required number of parameters.
Scope of Variables:-
By scope of variable we mean the part of program where the variable is accesible. In C scope of
variables are of two types:
◦ Global variables
◦ Local variables
Types/Categories of Functions:-
Depending on presence of arguments and retum value in a function, they fall under following categories
◦ Function with No Arguments and No Return Values:
▪ When function has no arguments and no return value, it falls under this category. The called
function does not receive any data value from calling function. Similarly due to no return value,
the calling function does not receive any data from called function. In short there is no data
transfer between calling and called functions.
◦ Function with Arguments and No Return Values
◦ Function with Arguments and Return Values
▪ i.e. two way communication between the calling function and the called function.