Join date: May 10, 2022


Function in C++

A self-contained block of statements that carry out a specific task is called Function. A functions is a subprogram which acts on data. main() function one of these functions. Execution of program always begins with main() function.


Declaration of function :

return_type function_name(arguments) {

Function Body

} Function prototype declaration, function definition, and function call are three important parts of function usage.

Passing arguments to a function :

According to homework answers websites arguments of a function are the data received by it when it is called. It is not compulsory to pass arguments in a function. In the above example the function ‘measage()’ does not contain any arguments.

• Default arguments –

C++ allows assigning default values to the arguments. According to computer science homework help expert the default values are declared at the time of function declaration. This default value is used when a matching argument is not passed when the function is called. In the above example we have set the default argument ‘rate = 0.10’. At the time of function call the value of rate is not mentioned at that time it will take value from the default argument i.e ‘rate = 0.10’.

• Constant arguments

A function can have constant arguments which cannot be changed. To make an argument constant keyword ‘const’ is used

For example –

void sum(const float x, const float y, const float z);

Here, the keyword ‘const’ tells the compiler that the arguments cannot be modified.

Calling a Function:

• Call by value :

If we call a function by passing values in it then it is called call by value.

For example –

In the above example program

c = sum(a,b); is a call by value method.

• Call by reference

If we call a function by passing address of variable in it then it is called call by reference. Pointers are passed as function arguments..

Inline Function :

Inline Functions speed up the program execution. In this function call is replaced by function code. In normal function execution the compiler has to jump in another location for execution of Function and the control is returned back after execution of function. Therefore the execution time required is more. There is memory penalty in inline Function case.

Declaration of inline function :

inline return_type function_name(arguments){ body of the function

The inline function does not work for the following situations -

• Functions returning values and having or a switch a loop or a goto statement.

• Functions which do not return value and if they are having a return statement.

• Functions which are having static variables.

The benefits of inline functions are -

• They are better than a macro. • Function call overheads are eliminated. • Program is executed more efficiently.

More resources:

С++ Training: Inheritance

С++ Training: Constructor and Destructor

С++ Training: Class

Data types in C++

Tokens in C++

Willie Miller

More actions