Loading...
Hello, my name's Mrs. Holborow and welcome to Computing.
I'm so pleased you've decided to join me for the lesson Today.
In today's lesson, we are going to be exploring how functions can be used in programmes and developing our own programmes which use functions to return values.
Welcome to today's lesson from the unit Programming Sub-programs. This lesson is called Functions in Programmes.
And by the end of today's lesson, you'll be able to create a programme that uses a function to return values.
Shall we make a start? We will be exploring these keywords in today's lesson.
Function.
Function, a subprogram that returns a value.
Return value.
Return value, a value that is returned by a function.
Parameter.
Parameter, used in a sub-program to allow values to be passed into them.
Argument.
Argument, values held in the brackets of a subprogram call, which are passed into a subprogram via the parameters.
Look out for these key words throughout today's lesson.
Today's lesson is split into two parts.
We'll start by looking at arguments and return values in functions and then we'll move on to use functions to return values in programmes.
Let's make a start by having a look at arguments and return values in functions.
A subprogram can be classified as a function or a procedure.
A function is different from a procedure because it returns a value or values to the main programme.
Python does not distinguish between procedures and functions.
The syntax for creating a function and a procedure is the same.
So here's an example for a sub-program called calculate.
Def is used to define the function.
Calculate is the identifier that is used to give the function a meaningful name.
A and B are the parameters in this function.
Parameters define the data values that must be passed to a function when it is called.
The parameters are treated as local variables inside the function.
A function can have one, more or no parameters.
True or false.
A function can only have one parameter.
Pause the video whilst you have a think.
Did you select false? Well done.
Multiple parameters can be used to pass values into a function.
What code needs to be added to line seven to call the function? Is it A, B, or C? Pause the video whilst you look carefully at the code.
Did you select B? Well done.
In order to call the function we need to write its name and then we need to pass the arguments to the function.
The function call contains arguments contained in brackets which are passed into the function via the parameters.
So here, A and B are the parameters and num1 and num2 are the arguments.
So the argument num1 is passed into the parameter, A.
And num2 is passed into B.
So here, A is equal to 10 and B is equal to 15.
In Python, the syntax for returning a value is return followed by the value you are returning.
It is important to note that when the return line is executed, the function will terminate.
So, this print statement on line four will not execute because it happens after the return line.
Time to check your understanding.
What will be displayed when this programme is run? Is it A, 15, B, nothing as line four will not run, or C, an error as the result is not defined? Pause the video whilst you have a think.
That's right.
Nothing will happen as line four will not run because line four is after the return statement in the function.
Okay, we are moving on to our first set of tasks for today's lesson and you're doing a fantastic job so far.
So well done.
For part one, what is the return value from the function call, find underscore highest with the arguments 12 and nine.
Pause the video whilst you have a think.
For part two, explain exactly what you think will happen when this code is run.
Pause the video whilst you look carefully at the code and make your prediction.
For part three, I'd like you to open the programme at oak.
link/calculate.
This is the same programme that was on the previous slide.
I'd like you to run the code and see if your predictions were correct.
Did anything surprise you? And then finally for part four, I'd like you to explain how the values num1 and num2 a passed to the function? Pause the video whilst you complete the tasks.
For part one you are asked what is the return value from the function Call find underscore highest with the arguments 12 and nine? The return value will be 12.
That's because in this case A holds the value of 12 and B holds the value nine.
A is greater than B, so it will return the value held by A which is 12.
For part two you are asked to make a prediction and explain exactly what you think will happen when this code is run.
Here's a sample answer.
The programme will take the values held by num1 and num2 and will add them together.
It will then display the result.
10 plus 15 is equal to 25.
For part three you were asked to explain if your prediction was correct or if anything surprised you.
In this sample answer, the code worked as I expected and the code did not surprise me.
For part four, you were asked to explain how the values num1 and num2 were passed to the function.
The values num1 and num2 are assigned values in the programme and are then passed to the parameters of the function in the call on line seven as arguments.
Remember, if you need to pause your video and make any corrections to your answers, you can do that now.
Okay, we are now moving on to the second part of today's lesson where we're going to use functions to return values in programmes.
What do you think will happen when this code is run, if the user types in two for the first number and three for the second number? Maybe pause the video here whilst you have a look carefully at the code.
Ah, Sam says I think it will display two to the power of three, which is eight.
Do you agree with Sam? Jun says, I'm not sure that's right Sam.
I think it may have an error.
Do you agree with Jun? Jun is correct.
The programme will return an error.
The programme will return a name error.
Name error name answer is not defined on line 11 of main.
py.
Let's go and see why this happens.
Watch the live coding video to investigate the error in the code and see how it can be corrected.
Okay, so here we have our subroutine, which we've defined as to the power and within inside that subroutine we have a variable called answer, which is evaluating the expression A to the power of B.
You've already had the opportunity to have a look carefully at this code and you've seen that when we run the code and for example, put in the value two for the first number and three for the second number, we have an error that is returned.
So the error says name error, name answer is not defined on line 11.
Let's take the time to have a look a little bit more carefully at the code.
So within the subroutine we have the variable answer, which as we've said has got the expression which evaluates to A to the power of B.
At the moment, this is just a procedure because it's not returning anything to our main programme.
On line four, we start the main code which prints the message to the user, enter a number.
It then stores that input as an integer under the variable num1.
On line six, we then display another print message to ask the user to enter a second number.
And on line seven we are storing that as the variable num2 again as an integer.
On line nine we are calling the subroutine so to the power and we are passing the values number one and numb two to the sub routine.
On line 11, we are then printing out the value of num1 to the power of num2 is answer.
I've got the variable answer and I've declared that inside the subroutine.
The issue is that that variable can only be seen inside the subroutine.
So down here when I am referring to answer, it's throwing an error because it cannot see the variable answer that's inside the subroutine.
Okay, so let's see how we can actually solve this problem.
So what we need to do first is we need to return the value of answer from the subroutine.
So I'm going to say return answer on line three.
Now this is converting this subroutine from a procedure to a function 'cause we are now returning the value.
Now you may think if we run this, it should hopefully work.
So let's try again with the same values.
Two for the first number, three for the second number.
Okay, we've still got the same error and that's because answer is still only available to the subroutine.
So we need to make sure that answer is available inside the main programme.
So what I'm gonna do is I'm gonna amend line 10 and I'm going to say the answer is equal to the power and then the number one num2, which we're passing into the sub routine.
So if we run this again now with the same numbers, so two and three, it's working correctly.
So we've got two to the power of three is eight, which is the correct answer.
Now this value here does not have to be answer.
Okay? And we can explore that in a second.
We can actually change that and prove that's not working.
And that's because this variable answer is a totally different variable to the variable answer that is inside the subroutine.
So what I'm going to do is I'm going to delete this and I'm gonna call it solution instead just to prove that.
But what I will need to do is I'll need to change the variable inside my print statement here to solution as well.
And this time if I run it and enter two followed by three, you can see that it's still working and that two to the power of three is eight.
So the important things to note here are that if we want to return a value from a subroutine, we must use this return line and this makes this a function.
And then secondly, we cannot expect the main programme to be able to see a variable held inside a subroutine.
True or false? A variable inside a function cannot have the same name as a variable outside a function.
Pause the video whilst you have a think.
This is false.
Variables inside the function are totally different from variables outside the function, so they can have the same name but still be different variables.
Although it's probably a good idea to avoid having the same names 'cause this can make testing and debugging quite difficult.
What Python keyword is used to share a value from a function to the rest of a programme? Is it A return, B call, or C print? Pause the video whilst you have a think.
Did you select A? Well done.
Remember, return is the key word, which is used to return a value from a function to the main programme.
Okay, we are now moving on to our final set of tasks for today's lesson, and you've done a fantastic job to get this far, so well done.
I'd like you to start by opening the starter programme at oak.
link/calculator.
The programme asks the user which operation they want to perform.
Addition, subtraction, multiply or divide? The programme then takes in two numbers the user wants to perform the operation on.
Create a function for each operation.
The function should return the result.
The first one has been done for you.
Here's a sample output from the programme.
So the operations are displayed to the user.
One add, two subtract, three multiply, four divide.
The user is asked to enter a choice, one, two, three, or four.
The user is then asked to enter the first number.
In this case they're entering two.
The user is then asked to enter the second number.
Again, they enter two.
The result is then displayed to the user.
So in this case, two plus two is equal to four.
Pause the video here whilst you complete the activity.
For part three, create a selection statement that checks the operation the user wants to perform and then calls the correct function.
How did you get on? Did you manage to create your functions? If you want to have a look at a sample answer, you can go to oak.
link/calculator-solution.
So on lines 10 to 15, I have my functions.
Remember, I have a function for each operation.
The first one was done for you.
So let's have a look at the subtract function.
So I'm defining the function by saying def and then I'm calling this function, subtract.
This function requires two parameters, which I'm calling X and Y, and then I'm returning the value X minus Y back to the main programme.
Let's have a look at the next part of the programme.
So here I have my codes from line 17 down to line 44.
So the top half of the programme displays the correct messages to the user and takes in the inputs of num1 and num2.
On line 33, I start my selection statements.
So if choice is equal to one, then the result is going to be equal to add num1, num2.
So I'm calling the add function and I'm passing the arguments num1 and num2.
I'm then printing out the result on line 35 and then I've repeated that for all of the other options.
Did you get your code working? I'm sure you did.
If you need to, you can always pause your video here, look carefully at the code and make any corrections.
Okay, we've come to the end of today's lesson and you've done a fantastic job.
So well done.
Let's summarise what we've learned together.
A function is a named block of code that is called to perform a specific task and will return a value.
Arguments are values provided to a subprogram at the time that it is called.
Subprogram parameters are values used by the subprogram that are passed from the arguments when it is called.
I hope you've enjoyed today's lesson and I hope you'll join me again soon.
Bye.