Functions in C++

category: 

"A function is a separate subprogram unit that groups the program statements into a single unit with a unique name which follows the rules of a variable". The design of different functions is important in structured programming whereas in Object Oriented Orogramming, the concentration is totally on classes. One of the major advantages is that it reduces the program source code. When a specific task is performed more than once, a function is used because its code is placed in one the memory. Unlike macros, it does not substitute text.

A function can be executed many times but the same code will be performed. C and C++ functions act like functions and procedures in Pascal. All those facilities which can be used in two different subprogram units in Pascal have been added in single C and C++ functions.

We can communicate with a function through parameters as well as get one value returned from it. On the other hand, in some cases return value is not needed. Functions can be either declared with or without parameters. In some cases, functions are used without parameters.

A special task can be defined separately into a function which allows the programmer to avoid unwanted side effects. The extensive use of global variables may create some bugs. To overcome this problem, local variables are declared where the life time of that variable is local to that function. In many cases, the use of local variables and parameters, a function can work properly without creating side effects in other parts of the program. Large programs can be combined and included and easily be managed.

At this point, both C and C++ are almost the same. As we know C language provides the basis of structured programming techniques. The use of local variables and the design of different functions with different task performed by their corresponding functions form the basis of structured programming. A function has three parts when using and creating it.

  • Function prototype
  • Function definition
  • Function invocation

Function prototype

The C++ compiler work top-down and should know the details about a function before calling it. In this part, the type of parameters is specified (if any) when declaring and defining a function. It must state the return-type (void or predefined data types), the name of the function and the data types of the variables, which will be passed as parameters.

For example:

int largest (int, int y);

This should be placed before the declaration of main () followed by a semicolon. The declaration of variables with the data type enclosed in parentheses is optional.

For example:

int largest (int, int);

Personally, I would prefer the first use of prototype because it is more readable and understandable. But one thing to be noted here, the use variables is only for clarity, the ‘C++’, compiler ignores the variable names. If there is no return value from the function, then it should be replaced by void. For example,

Void largest (int x, int y);

It can also be written as:

Void largest (void);

When the compiler is invoked, it checks the prototype just to make sure that the exact number of parameters with the specified data types are passed in and the returned value is also checked, which is depending on the return data type either voids or others predefined data types. If there is any mismatch, the compiler will lead to an error.

There is another way C++ declares parameters that is the empty parameters-list. In this case, the function can be declared as:

int largest (void)

Or

int largest ()

The presence or absence of a void in place of parameter-list is simply telling the compiler that the function does not have any parameters.

Function definition

It is the function itself. As I have said earlier, use variable names with data types in the prototype. Because a function definition is just the copy of prototype. Otherwise, you will include the variable names here. It is important that the order and the type of parameters must match the function prototype. An example of function definition such as:

int largest (int x, int y)

It is to be noted that in the declaration of ordinary variable, a data type can be associated with more than one variable.

For example

Int  x, y, z;

But in case of function definition, it is not permissible.

For example:

Int largest (int x, y)

The type of each parameter or argument must be associated separately.  

Function invocation

A function invocation or call activates the function specified by the function name. as mentioned earlier that a function must have a valid user-defined name. the function invocation must have a list of actual parameters if the corresponding function declaration contains a list of formal parameters. A function invocation means execution of function.

For example,

large = largest(x, y);

Actually when it encounters the function name largest, it transfers control to the function and upon completion, the transfers back to the statement following the invocation of the function.