More Functions (Fruitful Functions)
1. Return values
Not only can you pass a parameter value into a function, a function can
also produce a value. You have already seen this in some previous
functions that you have used. For example, len takes a list or string
as a parameter value and returns a number, the length of that list or
string. range takes an integer as a parameter value and returns a list
containing all the numbers from 0 up to that parameter value. Functions
that return values are sometimes called fruitful functions. In many
other languages, a function that doesn’t return a value is called a
procedure, but we will stick here with the Python way of also calling
it a function, or if we want to stress it, a non-fruitful function.
If the 'return temp' is not included in the function, then you will get
nothing out of the function. So the 'returned' value is like the output
of the function.
In Matlab, in the independent function module, we do this:
function result = usrFunction(x)
result = x^2;
'result' is the returned value from this function in Matlab. However,
in Python, we don't put the returned value on the top, but we use
'return something' to deliver the results.
In the example aboeve, if we don't have 'return temp', and leave that
line empty, you will get this:
ALSO, the variable 'temp' is a 'local' variable, you cannot see it or
use it outside of the function module. See the example below, after I
run the function, and then I tried to call the variable 'temp' but I
got nothing out of it. It is a local variable only being used inside of
THIS IS DIFFERENT FROM MATLAB.
Since the value
in 'temp' was retuned, it doesn't mean you can use
'temp' outside of the function (since it is a local variable). Keep in
mind that the function is like a factory. Raw materials go into the
factory and the product come out from the factory. The inputs are raw
materials or variables, the output is the product or the returned
values. There migh be many things or byproduct being produced in the
facotry at the same time, but as long as the factory doesn't sell these
things, they are internal things and other people won't know what are
So you may only
care about the INPUT and the OUTPUT of the function,
and the output must be returned to be able to become visible to the
If I call the
function outside, and assigne the results (returned
value) to a variable, then I can visualize this variable by just
As the example
above, in order to take out the returned value and
display it, you need to assign the function to a variable outside of
the function itself.
of calculating the distance between two points in the
Suppose you want
to find the distance between two points, given by the
coordinates (x1; y1) and (x2; y2). By the Pythagorean theorem, the
As the example
above shows, the name of the variable outside of the
function doesn't have to be the same as the one inside the function.
However, when you call the function, definitely the name you call must
be the same name as the function.
You can call one
function from within another. This ability is called
All the arrows
show you the flow of how these parameters are
bing passed throughout the functions when you call one of
these functions and put the parameters in the function.
Here is one
example that shows the function is calling itself. This may be a little
3! is 3 times 2!, which is 2 times 1!, which is 1 times 0!. Putting it
all together, 3! equals 3 times 2 times 1 times 1, which is 6.
The result is
correct. Now let's see how this works out:
3 is not 0, we take the second branch and calculate the factorial of n-1...
Since 2 is not 0, we take the second
branch and calculate the factorial of n-1...
Since 1 is not 0, we
take the second branch and calculate the factorial of n-1...
0 is 0,
we take the first branch and return 1 without making any more
The return value (1) is multiplied by n,
which is 1, and the result is returned.
The return value (1) is multiplied by n,
which is 2, and the result is returned.
The return value (2) is multiplied by n,
which is 3, and the result, 6, becomes the return value of the function
call that started the whole process.
** You may
wonder why the part of
'result = n*
was executed for
it enties 'else', the function immediately jumps out of the 'else' part
and starts from the top again until you have 'return 1' executed. After
'return 1' executed, '1' is returned to 'recurse' and now, n is '1' as
well. Then you have the 'result' to be 1*1 to be returned to 'recurse'.
Then the program
start running the leftover at the outer layers that didn't get a chance
to be executed:
The first outer
'result = n (2
now)* recurse (1 now)
The second outer
'result = n (3
now)* recurse (2 now)
So the result is
Define a function that carries the radius (r) and height (h) of a
cylinder. This function calles two sub-functions which calculate the
area of the cross-sectional circle and the volume of the cylinder
The input from the user has three parameters: 'W' is the width of the
rectangle, 'L' is the length of the rectangle, 'R' is the radius of the
homogenious circles to he aligned one-by-one inside the rectangle.
Design a function that calculates/returns how many circles can be
physically fit into a rectangle with given 'W' and 'L'. (Must use sub-functions
to calculate how many circles width-wise and how many circles
length-wise and use another function to calculte the entire number of
another function to
calculate/return the empty area not occupied by the circles in the
rectangle. Imagine that you can magically take out these areas and make
them into circles with the given 'R'. Use this function to return how
many circles you can make and what is the leftover area after this?