Hi, here is one more lecture about functions and this one is in a more practical view
because what we're going to do is we're going to, save that, we're going to look at
the program that we built in the previous lecture, sorry in the previous section so
this simple program. Know that these are the real-life applications that this
course covers, so these are just a small snippet of code, don't confuse this
with the real-life applications. I will cover 10 real world applications later in
course. Until in this program will this did was it asked you for the age and
let me guess M that's not exactly my age but anyway 90, so this will print out
your age plus 50, so 90 in this case and what I want to do in this
lecture is I want to organize this code better by putting this inside a function
so this will help you understand functions, how they are used in a more
real-life situation. So the function basically had three components and that
was input, the body of the functions, so the operations that the function
performs, and then the output so in this case we are going to somehow get the
input from this, user prompt and then this will be a body of our function and
then we will output this one here. The new age variable so let's go ahead and
define a function here, define age foo. Let's keep it like that and we're going to
get some age input there and there goes this line, so the new age will be
equal to the input that the function will pass in here.
So we convert that input to float and then we add 50 to that and then we return
new age, so once you have defined a blueprint so to say,
once you define your function, then you want to prompt the user for a value, so
that you have a value already there to pass it to the function code later here.
Which will be like print age foo age goes there. Now don't confuse this age
variable with this one here, they just happen to be the same but these are not
the same because this is a local variable,
while this one in here is a local, global variable, sorry. Global variable, local
variable so what happens is when Python executes this script it goes line by
line, so it reads the first line, second, third and it knows, it sort of records
in the memory this kind of blueprint but anyway this is just a blueprint so
it will not output anything, and then it goes to the next line this is empty, okay go
to the next line. Age equals to input. We have an input function which creates a
user prompt, so it prompts the user this message and then the user
submits a value and that value is stored in the age variable.
So this is a global variable. It doesn't have to do with this one here and then what happens
is we call the function age foo and inside that function we pass the value stored in
this age global variable, so let's say the user passed 40. 40 will be passed in
here so 40 here which means this 40 will be passed to this age argument
so to this variable, so the local variable now we'll get a value of 40 and
that value will be put in here, so float 40 plus 50 equals 90 and that will
return 90, so that 90 will replace this thing in here and Python will print 90
point zero in the command prompt in the terminal here. Let me try that. Enter age.
40. 90 point zero. And yes basically that's how you integrate user
inputs with functions. That's about this lecture. I'll talk to you later.