INTRODUCTION
In the earlier lessons we have already seen that C supports the use of library functions, which are used to carry out a number of commonly used operations or calculations. C also allows programmers to define their own functions for carrying out various individual tasks. In this lesson we will cover the creation and utilization of such user defined functions.
- The use of user-defined functions allows a large program to be down into a number of smaller, self-contained components, each of which has some unique, identifiable purpose. Thus a C program can be modularized through the intelligent use of such functions.
- There are several advantages to this modular approach to program development. For example many programs require a particular group of instructions to be accessed repeatedly from several different places within a program.
- The repeated instruction can be placed within a single function, which can then be accessed whenever it is needed.
- Moreover, a different set of data can be transferred to the function each time it is accessed.
- Thus, the use of a function avoids the need for redundant (repeating) programming of the same instructions.
Before using any function it must be defined in the program. Function definition has three principal components: the first line, the parameter declarations and the body of the functions. The first line of a function definition contains the data type of the information return by the function, followed by function name, and a set of arguments or parameters, separated by commas and enclosed in parentheses. The set of arguments may be skipped over. The data type can be omitted if the function returns an integer or a character. An empty pair of parentheses must follow the function name if the function definition does not include any argument or parameters.
Function
A function is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling program.
Every function has a unique name. This name is used to call function from “main()” function. A function can be called from within another function.
- The calling portion of a program must contain a function declaration if a function returns a non-integer value and the function call precedes the function definition.
- Function declaration may be included in the calling portion of a program even if it is not necessary.
- It is possible to include the data types of the arguments within the function declaration.
- The compiler will then convert the value of each actual argument to the declared data type and then compare each actual data type with its corresponding formal argument.
- Compilation error will result if the data types do not agree. We had already been use, data types of the arguments within the function declaration.
- When the argument data types are specified in a function declaration, the general form of the function declaration can be written as
data-type function name (argument type1, argument type2, … argument type n);
- Where data-type is the data type of the quantity returned by the function, function name is the name of function, and argument type/refer to the data types of the first argument and so on.
- Argument data types can be omitted, even if situations require a function declaration.
- Most C compilers support the use of the keyword void in function definitions, as a return data type indicating that the function does not return anything.
- Function declarations may also include void for the same purpose.
- In addition, void may appear in an argument list, in both function definitions and function declarations, to indicate that a function does not require arguments.
// Function prototype
<return_type><function_name>([<parameter_list>]);
// Function Call
void main()
{
<function_name>([<arguments>]);
}
// Function definition
<return_type><function_name>([<parameter list>])
{
<function_body>;
}
Where:
- <return_type> is the data type specifier of the data returned by the function.
- <function_name> is the identifier by which it will be possible to call the function.
- [<parameter_list>] (as many as needed): Each argument consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow passing arguments to the function when it is called. The different parameters are separated by commas.
- Statements are the function’s body. It is a block of statements surrounded by braces { }.
Function Header
In the first line of the above code
int sum(int x, int y)
It has three main parts
- The name of the function i.e. sum
- The parameters of the function enclosed in parenthesis
- Return value type i.e. int
Function Body
- The prototype of a function provides the basic information about a function which tells the
compiler that the function is used correctly or not. - It contains the same information as the function header contains.
- The prototype of the function in the above example would be like
int sum (int x, int y);
Note: The only difference between the header and the prototype is the semicolon; there must be a semicolon at the end of the prototype.
Function Prototype and Function Definition Example
- Argument is a parameter which is used to pass into function during function call
- Actual argument is a parameter which is available in calling function
- Formal argument is a parameter which is available in called function/function definition
- Both actual and formal argument must be same which is declared before main
A function parameter is a variable declared in the prototype or declaration of a function:
void fun(int x); // prototype — x is a parameter
void fun(int x) {} // declaration — x is a parameter
An argument is the value that is passed to the function in place of a parameter:
fun(6); // 6 is the argument passed to parameter x
fun(y+1); // the value of y+1 is the argument passed to parameter x
Why we use Functions?
Two reasons
- Writing functions avoids rewriting the same code over and over. Suppose that there is a section of code in a program that calculates area of a triangle. If, later in the program we want to calculate the area of a different triangle we won’t like to write the same instructions all over again. Instead we would prefer to jump to a “section of code” that calculates area and then jump back to the place from where you left off. This section of code is nothing but a function.
- Using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code in to modular functions also makes the pro-gram easier to design and understand.