Hey, let's run a few code experiments now for a better understanding of functions.
Specifically, you want to understand the difference between function definitions and function calls.
And that is what the first experiment is about, the difference between function calls and definitions.
So if I remove all these calls here and I just leave the function there and I run the codes, nothing
will be returned as an output.
Well, you could say there is no print function here.
That's why we didn't get any outputs.
But I can provide a print function here.
Let's say, Hey, hey.
And I run the code again.
No outputs.
So why is that?
Well, that's because this is just a recipe of the process.
If we wanted to use our coffee making example again.
So when you go to order a coffee in a bar and you go to the bartender and you ask them, I want a coffee.
They have a recipe in mind.
Maybe in their mind, maybe they have written it somewhere.
So the recipe is the function definition.
That's the function definition.
The recipe itself has to be executed by someone.
The bartender.
So the function also has to be executed in order to get the output and we execute the function like
this.
So greet.
So that's the variable and that's the value.
So at this point this will be equal to Halo with first letter capitalized.
But if we run this again, what we get is, hey, hey.
So the function was called and that proves it.
We get hey, hey.
So the function was called because this was executed.
That's how we know the function was called.
We don't get Halo printed out because we simply got a low and we stored it in greetings.
So if we want hello to be print out, we say print greeting.
You run again the code and then you get say hey.
And also hello.
So it's very important to realize that the function call is equal to a value.
The value of that is always whatever is returned in here.
So it could be a string, it could be a number.
It could be any object that the function returns.
So I hope that helps you understand the difference between the function definition and the function
call.
Now, the next experiment, it is about the return statement.
So try to remove that line and print out the code.
This is what we get this time.
Hey.
And none.
So again, the interpreter goes and executes the blueprints.
It doesn't do anything.
It just looks at the recipe.
And then you execute the recipe here.
But the problem is that this time the recipe has not instructed what to return.
So the coffee was prepared, but the recipe didn't say, Oh, give that coffee to.
The client.
And when that doesn't happen implicitly, the function definition will return a non object.
So just like we have integer strings, we also have this special object called None.
None is basically to represent nothing.
So in Python, you can actually in the console you can just say none.
So that's recognized as a value.
So it's not a string, it doesn't have quotes.
So this would be different from that.
That is a string.
That is a special object.
So that's here is equivalent of having return none.
If we run this, we're going to get the same error.
Hey, sorry, the same output.
It's not an error.
The same.
Hey.
And none.
So you normally want to return something here in this case is new message.
Some people what they will do, especially beginners, they will confuse print with return.
So instead of returning something, they will say a print new message and let's see what we get this
time.
So.
This is what happens.
The interpreter reads the recipe and then it executes the recipe.
So messages associated to the hello string and the new message is associated to hello with the first
letter capitalized.
So far, so good than print.
Hey, so this is printed out in here then Print message.
Message is printed out.
Hello.
You see with the letter cabinet lights.
So, so far, so good.
And then none is printed out.
Why is not printed out?
Well, because I just told you that when you don't have that return statement, the function will always
return none.
So implicitly, the function again is doing return none.
That's why we get none here.
So in other words, grits will be equal to none to that object.
And that's why.
When it prints.
Here we are printing the non object, the object that the function greets returns.
So you don't want to confuse print with return.
Always try to return the output instead of printing it out.
So if I run the script now, we get the correct expected output.
Hey, hey, print it out by this and hello by that.
So why is it important to return and not print?
Well, because perhaps you want to do something to that.
Value, which is Hello.
Perhaps you want to print out the length of Hello.
So the length would be Len greeting you run this and you get five.
But if you had print here.
See what we get now.
It's an error because you're trying to apply Len to non to the non type object, and that gives an error
because the non object doesn't have a length, doesn't have characters like a string has or at least
has.
So again, I cannot stress it enough that you should return instead of printing.
So this was about functions for this code experiments for today.
Thanks a lot.
See you.