Chapter 3

Functions

 

A function is a set of program statements. The program statements can be expressions, commands, comments or calls to functions. A function is declared in C & C++ as follows

<return type> function name (parameter list){

…..statements

return value;}

A return type of a function is a value type that will be returned by the function. The rule in C & C++ states that all functions return a value must be declared as "void". A function return type can be any of the basic data-types, pointer or objects. A function name can be any string with no space between letters. This name is used in invoking the function. The parameter list consists of values passed to the function. And within the braces, the function is defined i.e. program statements are written within the block between the braces which forms the body of the function.

 

The main( ) function

 

The main function is the first function to be invoked when your program is first loaded into memory by the operating system. When your program is loaded into memory. The main function, as the name suggests, is the main function in a program and there can be only one main function in a C & C++ program. The main function, as all functions, return a value to the operating system or if declared "void" does not return any value. A large function/program is difficult to maintain and therefore the tasks of the program are broken down into smaller programs to facilitate ease of use. A simple ‘C’ program may look like the following piece of code

 

void main( )

{

display_string( );

}

void display_string( )

{

printf("%s","Hello!");

}

 

A function is typically enumerated as per the task and its name should be indicative of the task that the function is designed for.

 

Parameterized functions

 

As we know all functions can take and return values. Let us see an example program. A small program which takes two integer values and returns the resultant value. Let us call it a "calculator program".

 

#include <stdio.h>

 

void main( )

{

int x,y,result; // Declaration of variables

x=6; // Initialization of variable

y=7; // Initialization of variable

result=calculator(x,y); // Call to a function

printf("%d",result);

}

int calculator(int a, int b)

{

int r; // Declaration of variables

r = a+b; // Expression

return r; // The return statement which returns a value of type integer

// which is also the return type of the function.

}

 

There are two functions "main( )" and "calculator(.…)". One is of type "void", the other is of type "int". The "int" type function also takes two integer variables. Notice how the function has been declared and defined. The call to the function passes x & y and the function calculator receives them into int a & int b. When the statement result=calculator(x,y) is executed, the control of the program branches to the calculator function and x’s value is assigned to a and y’s value is assigned to b and the first statement of the program declares a variable "r" which will contain the result of the arithmetic expression "a + b" which is subsequently returned to the calling function (main) by the last statement – "return r". This statement returns a value of "r" to result when the statement result=calculator(x,y); is executed. The last statement prints the value on to the screen/console. Printf is a pre-defined function whose prototype is present in the header file "stdio.h". The very first statement in the program includes a reference to the pre-defined function so that the compiler does not generate a "Prototype of "printf" not found". This means that the "stdio.h" has a prototype of the "printf" function through which the compiler seeks out the actual definition of the function in the appropriate library. A prototype is an exact replica of the actual. The prototype of our calculator function will look as follows

 

int calculator(int a, int b); // Prototype of a function

 

In C++, it is necessary to declare prototypes of all functions in a program; in C, it is not although some C compilers may demand it.

 

There are numerous library functions available with the ‘C’ compiler and each’s respective header file has to be #include-ed before using them.

 

More on parameterized functions

 

When we pass parameters to a function, we are actually passing a copy of the value contained within the variable. This is called "Call by value". "Call by value" means that the original value of a variable is never going to be modified even if the copy value of the variable passed to a function is modified. In the "calculator" function, the values of "x & y" remain unchanged. That is, the original values in the variables remain intact. There is another type of passing parameters which is called "Call by reference". A "Call by reference" does not make a copy of a variable but simply passes a reference of the variable i.e. the address where the variable’s value resides and thus, all modifications to the original value is also modified in the original place.

Result=calculator(&x, &y);

 

The & sign is usually used for referring to addresses.

 

Since a C program consists of many functions, it becomes necessary to fully understand everything about functions. And since all our example programs will consist of different types of functions, we shall discuss, as well as fully understand, the importance, technique and functionality of functions in C & C++.