Professional Documents
Culture Documents
1
HERNANDEZ MORALES EFREN DANIEL UNIVERSIDAD TECNOLOGICA DE ALTAMIRA
TASK 1: OBJECT ORIENTED LANGUAGE PROGRAMMING
24/03/2024
You must keep two important things in mind with the return statement:
1.- Any statement found after the execution of return will NOT be executed. It is
common to find functions with multiple return statements inside conditionals, but once
the code executes a return statement, everything from there on down will not be
executed.
2.- The type of the value returned in a function must match that of the type declared to
the function, that is, if int is declared, the returned value must be an integer.
Example 1:
As you can see, it is a simple example, if you execute this, the function will return the
sum value which is 10 (5+5). The subsequent lines will never be executed, although they
do not generate any errors, they are of no use. You can notice that in this case it is the
same to have written return sum as to write return 5+5. Both lines work equivalently.
Example 2:
Here we made use of multiple return statements and took advantage of the characteristic
that when executed they immediately terminate the execution of the remaining part of
the function. In this way we can ensure that the function will return 'a' only when the
value of parameter n is zero and will return an 'x' when said value is not zero.
Example 3:
Here we already have a function that receives two parameters, one of them is used in the
conditional and the other to display its value on the screen with cout, this time we return
boolean values 0 and 1, it could be true or false as well.