Wednesday, March 22, 2017

C++ Part 42

Review Part V
Local Variables
Variables that are declared within the body of a function definition are
said to be local to that function or to have that function as their scope.
Variables that are declared within the main part of the program are said to
be local to the main part of the program or to have the main part of the
program as their scope. When we say that a variable is a local variable
without any mention of a function and without any mention of the main
part of the program, we mean that the variable is local to some function
definition. If a variable is local to a function, then you can have another
variable with the same name that is declared in the main part of the
program or in another function definition, and these will be two different
variables, even though they have the same name.

Overloading a Function Name
If you have two or more function definitions for the same function name,
that is called overloading. When you overload a function name, the
function definitions must have different numbers of formal parameters or
some formal parameters of different types. When there is a function call,
the compiler uses the function definition whose number of formal
parameters and types of formal parameters match the arguments in the
function call.

Call-by-Reference
To make a formal parameter a call-by-reference parameter, append the
ampersand sign & to its type name. The corresponding argument in a call
to the function should then be a variable, not a constant or other
expression.
When the function is called, the corresponding variable argument
(not its value) will be substituted for the formal parameter. Any
change made to the formal parameter in the function body will be made
to the argument variable when the function is called. The exact details of
the substitution mechanisms are given in the text of this chapter.
EXAMPLE
(OF CALL-BY-REFERENCE PARAMETERS IN A FUNCTION
DECLARATION):
void get_data(int& first_in, double& second_in);

Both void functions and functions that return a value can have return
statements. In the case of a function that returns a value, the return statement
specifies the value returned. In the case of a void function, the return
statement simply ends the function call.


Post a Comment