Computer Programming Lecture 4.0 & Practicals
Computer Programming Lecture 4.0 & Practicals
Computer Programming Lecture 4.0 & Practicals
•What we did in this case was to call to function addition passing the values of x and y, i.e. 5 and
3 respectively, but not the variables x and y themselves.
Passing arguments by value &
reference
•This way, when the function addition is called, the value of its local variables a and b become 5
and 3 respectively.
•Any modification to either a or b within the function addition will not have any effect in the
values of x and y outside it, because variables x and y were not themselves passed to the
function, but only copies of their values at the moment the function was called.
•However, there might be some cases where you need to manipulate from inside a function the
value of an external variable.
•For this purpose your function must allow passing values by reference rather than by value.
Passing arguments by value &
reference
•Consider the example below:
• The first thing that should call your attention is that in
the declaration of duplicate the type of each parameter
was followed by an ampersand sign (&).
• This ampersand is what specifies that their
corresponding arguments are to be passed by reference
instead of by value.
• When a variable is passed by reference we are not
passing a copy of its value, but we are somehow passing
the variable itself to the function.
• Any modification that we do to the local variables will
have an effect in their counterpart variables passed as
arguments in the call to the function.
Passing arguments by value &
reference
•Suppose we created the function parameters without the ampersand signs (&), we would have
not passed the variables by reference, but a copy of their values instead.
•The output on screen of our program would have been the values of x, y and z without having
been modified.
Default values in parameters
•When declaring a function we can specify a default value for each of the last parameters. This
value will be used if the corresponding argument is left blank when calling to the function.
• To do that, we simply have to use the assignment operator and a value for the arguments in the
function declaration.
•If a value for that parameter is not passed when the function is called, the default value is used,
but if a value is specified this default value is ignored and the passed value is used instead.
•Consider the example on the following slide:
Default values in parameters
Overloaded functions
•In C++ two different functions can have the same name if their parameter types or number are
different.
•That means that you can give the same name to more than one function if they have either a
different number of parameters or different types in their parameters.
•Consider the example on the next slide:
Overloaded functions
•Take note:
• A function cannot be overloaded only by its return type.
• At least one of its parameters must have a different type.
Recursion
•A function that calls itself is said to be recursive.
•This process can also be performed indirectly if the function first calls another function or
multiple functions before it is called once more.
•Recursion is the property that functions have to be called by themselves.
•Recursion has may applications when creating computer programs.
•Consider how you mathematically obtain the factorial of a number (n!) by the formula:
n! = n * (n-1) * (n-2) * (n-3) ... * 1
Recursion