Until now we wrote programs in which all the code was written in main ( ). If we require to write large programs, it is always easy if the program can be broken down into small modules. This can be done using functions. The main ( ) itself is a function. Designing, writing and maintenance of programs can be done with simplicity and clarity by using different function instead of putting whole code in main function.
We have refered to library function in earlier chapters. While studying goto statements, we have said that they can pass control anywhere within the same function.
In this chapter we will see what functions are, how to define a function, call a function & different methods of passing parameters. We will also learn about recursive functions
- What is the function
- Calling a function.
- Types of function.
- Why function is required.
- Function prototype.
A ‘C’ program can be considered as being a collection of functions. Here we will see what do we mean by a function and basically why do we need to write a function when we are already writing programs without it.
What is a function?
In order to know what a function is, let us write the program of adding two numbers and printing their sum, using functions.
Here we have broken down the program into two modules.
Adding two numbers.
Printing the result.
We wrote a function for each module, ‘sum’ and ‘display’ respectively. Any function definition has the form:
return-type function-name (argument declaration)
declaration and statements
Function definition is the actual code of the function within braces. Whereas function declaration is a statement which tells the compiler that a function (e.g. sum), with these type of argument (e.g. two integers) and these type of return value(e.g. int) is used in this program.
Functions are self-contained block of statements. All variables used in a function are declared at the beginning of the function. In sum variable s is used and hence declared at the beginning of function.
Calling a function
Let us consider the concept of calling a function. When a function calls another function, control passes to the called function and the calling function waits for the control to return to it. In Example (1), main ( ) is the calling function and ‘sum’ and ‘display’ are the called functions.
main( ) calls the function sum(). Control passes to sum and statements in sum are executed. When ‘return’ statement is encountered, control is returned to function main ( ). Further statement in main() start to execute. The main( ) calls function display().
Any function can call any other function any number of times. Compiler begins program execution with the function main ( ). Communication between function is made possible by passing argument to called function and argument by called functions. Minimum requirement to recognize a function is a name with opening and closing round brackets.
Empty round brackets after function name indicate that, no argument is passed to the called function.
Types of functions
There are two types of functions;
- Library functions.
- User-defined functions.
We have mentioned library function in the first chapter in relation to input-output functions. Library functions are built-in functions that are provided with ‘C’ compiler. There are library functions in ‘C’ which perform basic needful tasks. I/O functions like printf ( ), scanf ( ), string manipulation functions like strlen ( )and so on. For using library functions programmer need not worry about how it is done. Knowing only what is done is sufficient.
User-defined functions are functions the programmer writes as per his requirement. Programmer has to plan all the ins and outs of the function.
Why function is required ?
Imagine you have to add two integers number of times in your program. If you don’t use the function sum(), it would keep repeating the same code that many times. Functions avoid repetition. Since ‘sum’ is a small function you might not mind repetition, but when you perform complicated operations, lengthy operations, avoiding repetition by using function is a plus point.
Also breaking into different tasks makes things simple then writing a whole single program. Functions once written can be used many times while writing other programs.
int sum(int x, int y)
just before main() in our previous example tells that sum is a function, used in main(). It accepts two integer arguments and returns an integer value. This declaration is called as function prototype.
By default, return value of a function is an integer. A function returning values other than integers (i.e. float, double etc.) must be explicitly mentioned. Functions returning no value have to be declared with return type ‘void’ as in function display().
Function declaration and definition must match otherwise compiler will give an error. Prototype is placed at the beginning of program, after any preprocessor commands. Parameter names in definition and declarations need not be same. Hence declaration of sum () has names ‘x’ and ’y’ while definition has names ‘a’ and ‘b’ Rather, names are optional in prototype.