C++ Functions

Library Functions

C++ provides many built in functions that saves the programming time.

Mathematical Functions: Mathematical functions like sin ( ), cos ( ), etc. are defined in header file math.h

Character Functions: All the character functions require ctype.h header file

String Functions: The string functions are present in the string.h header file

Console I/O Functions: The header file for such functions is stdio.h

User Defined C++ Function

A C++ function is a grouping of program statements in a single unit. The main() is an essential function in all C++ programs.

Function Prototype: The function prototype informs the compiler about the functions to be used in a program, the argument they take and the type of value they return. Functions which do not return any value are known as void functions.

Arguments to a function: Sometimes the calling function supplies some values to the called function. These are known as parameters. The variables which supply the values to a calling function called actual parameters. The variable which receive the value from called statement are termed formal parameters.

Consider the following example that evaluates the area of a circle:

#include <iostream.h>
void area(float);
void main()
{
float radius;
cin>>radius;
area (radius);
}
void area (float r)
{
cout << “The area of the circle is ” << 3.1416*r*r << “\n”;
}

The statement area (radius), that invokes the function, may be written as area (6.0). This will evaluate and display the area of a circle with radius 6.0. Here, radius is called actual parameter and r is called formal parameter.

Return Type of a function

In the above program, area of a circle display in the function area() itself. If the area of a circle required in the calling function main(), the function area() may return the area evaluated to function main(). This is possible through the return statement.

The program may be written as:

#include <iostream. h>
float area(float);
void main()
{
float radius, y;
cin>>radius;
y = area(radius)
cout<< “The area of the circle is ” << y;
}
float area (float r)
{
return(3.1416*r*r);
}

In function protoype, void is replaced by float, this indicates that value returned by the function area to main function is float. In the calling function variable y is assigned the value returned by function area(). Thus, variable y will be assigned the area of a circle.

Global and Local variables

The variable declared in a program can be classified into following two types:

  1. Local variable
  2. Global variable

Local Variable

A variable declared within the body of a function will be evaluated only within the function. The portion of the program in which a variable is retained in memory is known as the scope of the variable. The scope of the local variable is a function where it is defined. A variable may be local to function or compound statement.

Global Variable

A variable that is declared outside any function is known as a global variable. The scope of such a variable extends till the end of the program. These variables are available to all functions which follow their declaration. So it should be defined at the beginning, before any function is defined.

If in a program, variable a is declared as local as well as global. Then,

  • a represents the local variable
  • ::a represents the global variable

The symbol :: is called scope resolution operator.

Variables and Storage Class

The storage class of a variable determines which parts of a program can access it and how long it stays in existence. The storage class can be classified as:

  1. Automatic
  2. Register
  3. Static
  4. External

Automatic Variable

All variables by default are auto , i.e., the declarations int a and auto int a area equivalent. Auto variables retain their scope till the end of the function in which they are defined. An automatic variable is not created until the function in which it is defined is called. When the function exits and control is returned to the calling
program, the variables are destroyed and their values are lost. The name automatic is used because the variables are automatically created when a function is called and automatically destroyed when it returns.

Register Variable

A register declaration is an auto declaration. A register variable has all the characteristics of an auto variable. The difference is that register variable provides fast access as they are stored inside CPU registers rather than in memory. The register and auto can be applied to local variable.

Static Variable

The variable may be static automatic variable or static external variable. The later one is meaningful only in multi-file programs. A static automatic variable has the visibility of a local variable but the lifetime of an external variable. Thus, it is visible only inside the function in which it is defined, but it remains in existence for the life of the program. 

External Variable

A large program may be written by a number of persons in different files. A variable declared global in one file will not be available to a function in another file. Such a variable, if required by functions in both the files, should be declared global in one file and at the same time declared external in the second file.

Calling of Function

The function can be called using either of the following methods:

  1. call-by-value
  2. call-by-reference

Call-by-value

Consider the following program which will swap the value of two variables using the method call-by-value:

#include <iostream.h>
void swap(int, int);
void main()
{
int a, b;
cin >> a >> b;
swap(a, b);
cout << a << b << “\n”;
}
void swap (int c, int d)
{
int t;
t = c;
c = d;
d = t;
cout << c << d << “\n”;
}

The variables a and b are called actual parameters. When calling the function swap() the value of a is passed onto c and value of b is passed onto d. Here c and d
are called formal parameters.

In the function swap(), the value are swapped but this change will not be implemented to actual parameters.

Call-by-reference

Consider the following program which will swap the value of two variables using call-by-reference method:

#include <iostream.h>
void swap(int &, int &);
void main()
{
int a, b;
cin >> a >> b;
swap(a, b);
cout << a << b << “\n”;
}
void swap(int &c, int &d)
{
int t;
t = c;
c = d;
d = t;
cout << c << d << “\n”;
}

In this method, when the function is called, the address is transferred from actual to formal parameters. It means a and c are sharing the same memory address and b and d are sharing the same memory address. So, any change in formal parameter will be implemented to actual parameter.

Inline Function

Functions save memory space because all the calls to the function cause the same code to be executed. The functions body need not be duplicated in memory. When the compiler sees a function call, it normally jumps to the function. At the end of the function, it jumps back to the statement following the call.

While the sequence of events may save memory space, it takes some extra time. To save execution time in short functions, inline function is used. Each time there is a function call, the actual code from the function is inserted instead of a jump to the function. The inline function is used only for shorter code.

  • Function is made inline by putting a word inline in the beginning.
  • Inline function should be declared before main() function.
  • It does not have function prototype.
  • Only shorter code is used in inline function.
  • If longer code is made inline then compiler ignores the request and it will be executed as normal function.

Function with Default Arguments

C++ allows to call a function without specifying all its arguments. In such cases, the function assigns a default value to a parameter which does not have a matching argument in the function call. Default value are specified when the function is declared. The compiler knows from the prototype how many arguments a function uses for calling.

Function Overloading

This means that some function name can be used to create functions that perform a variety of different tasks. This is known as function polymorphism in OOP. The function would perform different operations depending on the argument list in function call. The correct function to be invoked is determined by checking the number and type of arguments.

A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique.