Lecture 11 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;
end

'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 the function.

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 they.

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 outside world.
If I call the function outside, and assigne the results (returned value) to a variable, then I can visualize this variable by just calling it.

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.

2 An example of calculating the distance between two points in the coordinate
Suppose you want to find the distance between two points, given by the coordinates (x1; y1) and (x2; y2). By the Pythagorean theorem, the distance is:

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.

3 Composition
You can call one function from within another. This ability is called composition:

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.

4 Boolean functions

5 Recursion
Here is one example that shows the function is calling itself. This may be a little bit consusing:
So 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:

Since 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...
Since 0
is 0, we take the first branch and return 1 without making any more recursive calls.

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* recurse
return result'
was executed for 3 times?
Everytime 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 layer is:
'result = n (2 now)* recurse (1 now)
return result'

The second outer layer is:
'result = n (3 now)* recurse (2 now)
return result'

So the result is 1*2*3