Accessing of Functions in C++

A function can be accessed (i.e., called) by specifying its name, followed by a list of arguments enclosed in parentheses and separated by commas as shown below.

function_name(int a, int b);

If the function call does not require any arguments, an empty pair of parentheses must follow the name of the function as shown below.

function_name();

The function call may be a part of a single expression (such as an assignment statement), or it may be one of the operands within the more complex expression as shown below.

int returnedvalue = function_name();
int c = 3 * function_name() + 5;

The arguments appearing in the function call are referred to as actual arguments, in contrast to the formal arguments that appear in the first line of the function definition.

In normal function call, there will be one actual argument for each formal argument. The actual arguments may be expressed as constants, single variables or more complex expressions. However, each actual arguments must be of the same data type as its corresponding formal argument.

Consider the below function definition:

int addnumbers(int a, int b)
{
//body of the function enclosed in curly braces
int c;
c = a + b;
return c;
}

We can access the above defined function as shown below:

int returnedvalue = addnumbers(2, 4);

In the above function call, values “2” and “3” are called actual arguments.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *