Welcome back.
One type of expression that you make right in Python is what's called the function
call expression.
So you can think of functions as boxes that
contain some machinery inside of them.
So I'm going to visually represent them here just using this gray box.
And you can think of a function as something that takes in some inputs.
So here, the inputs feed into the top here and
then these inputs go inside of this machinery.
This machinery does something with the inputs, and
then after it's done its work with the inputs, it spits out an output.
So visually again, you can think of it as working something like this.
So here we have a function, it takes in three inputs or arguments and
then it does work, and then it spits out an output or return value.
So let's watch that again.
So again, we have the function, and it takes in three inputs here or arguments,
then it does work, and then it spits out an output or a return value.
Now, let's translate this visual metaphor into actual working Python code.
Here's an example of how we would actually write a function call expression in
Python.
Suppose that we have a function named square and
our square function takes in one argument which is going to be a number.
And then it returns, or outputs, whatever that argument is squared.
So for example, if we say square and then open parenthesis and
we put our argument in between the parentheses here, so
this is called calling the square function with 4 as an argument.
So this is how we actually write that in Python again, we say square, or
whatever the name of the function is.
Then inside of parentheses immediately after the name of the function,
we pass in our arguments.
So the arguments go here.
And then the value of this overall expression, so
whatever the value of this expression is, is going to be the return value.
And so, in the case of square, if square squares whatever input or
arguments you give it, then the return value is going to be 16.
And this is what it looks like when we translate this Python
code into that visual representation that I just mentioned.
So we have the square function, again represented as a box, and the square
function in this case we suppose that it takes in one input or argument.
And if we pass in 4, it does some work, and it spits out an output or
return value of 16.
To watch that again, we pass in 4, the square function does some work,
and it computes an output or return value, in this case of 16.
And again, this is how we write that in Python code.
We say the name of the function, square, then open parentheses to say that we
want to call that function and then in between parentheses,
then we pass in whatever arguments we want our functions to take.
Now, Python functions can take one, two or any number of arguments or
inputs, but there's always exactly one output.
So, for example, here this function takes in three inputs, or arguments, but
it spits out one return value.
So again, functions always take any number of arguments, but
there's always one value of that function call.
So, let's look at a little bit more Python code and calling functions.
So, here on the first line we're actually calling two functions technically because
here print is the function that we're calling and
we're passing in as an argument to print, an expression which just so
happens to be another function call.
And this function call uses the square function that I just described.
So, again, the square function that I am describing,
this isn't a built-in with Python.
This is something that we defined for the purpose of the explaining function calls.
But we defined it in a way that it takes in one argument,
in this case square(3), and then it spits out that number squared.
So if we say, print out the value of square(3),
then this is going to print out the integer 9.
So let's see that in action.
So I'm going to comment out the rest of these lines and run my code.
And I can see that the value of this expression, square(3) is 9.
If we call a function, but we don't have a print statement, so let's suppose
that I say square(5), the value of this expression is going to be 10.
But you'll notice when I save and
run my code, it's not going to actually affect the output.
And that's because we never actually printed the value of square(5).
We just computed the value of square(5).
Excuse me, it's not 10, it's 25 of course.
So here the value of this is 25 but we never print out its value.
Now, let's suppose that we have another function, called sub.
So sub takes in two arguments.
So I can represent that visually like this.
So it takes in one and two arguments.
And just like every other function, it's going to spit out one returned value.
Now, let's suppose that sub subtracts the second argument from the first argument.
So, in other words, suppose if I passed in 6,
If I passed in 6 and 4,
Then my sub function is going to spit
out 2 because 6 minus 4 is 2.
So we would call the sub function by saying print out and then sub.
And then here you'll notice that we have two arguments and
we separate those arguments by a comma.
So you print out the value of sub(6, 4).
And we see that the value of this expression, sub(6, 4) is 2.
If we call it again, so if we call sub with arguments 5 and
9, then this is going to give us -4 because 5- 9 is -4.
We can also combine function calls with other operators including function calls.
So here if we print out the value of square(3) + 2.
Then Python is going to first compute the value of this overall expression by
evaluating the value of square(3) and then it's going to add the result of that to 2.
So Python is going to compute the value of square(3) and
it's going to get 9 and then it's going to compute the value of this expression,
which has a value, 2, because it's a literal expression.
And then it's going to add these numbers together to give us 11.
Now this expression on line 2 is much more complicated.
So here we can see that we're calling the sub function.
We're calling it with two arguments.
So the first argument is square(3), so this is arg 1,
the second argument is square(1 + 1), so this is arg 2.
Now, whenever Python calls a function call expression,
it needs to figure out what the values of the arguments are.
So Python kind of computes the values of a function call expression
from the inside out.
So it's going to go from the value of 1 + 1.
To the value of square(1+1) and then the value of square(3), and
then after it has the values for square(3) and square(1+1),
it's going to subtract those two values by calling the sub function.
So again, we're kind of going from the inside out to the next layer.
And then out to the next layer beyond that.
So when Python computes this,
it's going to first compute the value of square(1+1), or 2.
So it's going to compute the value of square(2), and that's going to give us 4.
It's going to compute the value of square(3), and that's going to give us 9.
And then it's going to compute the value of sub when called on 9 and
4, and that's going to give us 5.
So if I run my code, you'll see that we get
11 from line 1 and 5 from line 2.
Just to run through the exact steps that Python takes when computing the code on
line 2, Python first computes, what's the value of square(3)?
It gets 9, then it computes what's 1 + 1, and it gets 2.
Then it computes what's square(2), and it gets 4..
Then it subtracts 9 and 4 and gets 5 and that's what it ultimately prints out.
Now, one thing to note is that functions are objects in Python.
So if I print out what's the value of square,
then Python is going to tell me that square is a function.
So if I run my code, you can see that line 1 prints out function square.
In order to actually call the function or run that machinery,
we need to have the name of the function, and then open parenthesis and
pass in our arguments inside of those parentheses.
So in other words, we need to have open parentheses right after the name of
the function in order to say that we want to call the function,
we don't want to just reference that function object itself.
That's all for now, until next time.