Lesson video

In progress...

Loading...

Hello, my name is Dr.

Dass.

Welcome to Computing.

I am so pleased that you have decided to join me for the lesson today.

In this lesson, we will be looking at functions.

Welcome to today's lesson from the unit: programming subroutines.

This lesson is called functions and by the end of today's lesson, you should be able to create a programme that uses a function to return values.

These are some of the key words that we will be using throughout today's lesson.

Function, function: a subroutine that returns a value.

Return value, return value: a value that is returned by a function.

Parameter, parameter: used in a subroutine to allow values to be passed into them.

Argument, argument: values held in the brackets of a subroutine called which are passed into a subroutine via the parameters.

Today's lesson is split into two parts: arguments and return values in functions and use functions to return values in programmes.

Let's start by looking at the first section, arguments and return values in functions.

A subroutine can be classified as a function or a procedure as shown in the diagram below.

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 as shown in the image below: def calculate a, b followed by a colon defines a function, calculate, that takes two parameters, a and b.

def is used to define the function.

calculate is the identifier that is used to give the function a meaningful name.

And 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 within the function.

A function can have one, more, or no parameters.

Right, let's do a quick recap.

True or false? A function can only have one parameter.

Why don't you pause the video here and have a quick think? Did you come up with the answer false? You were right.

Multiple parameters can be used to pass values into a function, well done.

Let's look at another question.

What code needs to be added to line 7 to call the function? The programme is shown in the image below.

The function being defined is calculate which takes the parameters a and b.

Pause the video here and have a think.

I am sure you'll come up with the right answer.

Well done, B was the correct answer: calculate within brackets num1, num2.

That will be the function call to call the function calculate.

The function call contains arguments contained in brackets which are passed into the function via the parameters.

So as you can see in the image below, we have added on line number 7, the function call: calculate, bracket open, num1, num2, bracket closed.

a and b within the function are its parameters while num1 and num2 are called arguments.

In this programme, num1 is a and num2 is b when we call the function calculate.

That means a has the value 10 and b has the value 50.

In Python, the syntax for returning value is return, followed by the value you are returning.

As you can see on line 2 in our programme, return a + b in the function add_numbers will return the sum of the values a and b.

If you look further down on line number 4, we are actually calling the function add_numbers with the arguments 5 and 3.

So the return value of the function would be 5 and 3, 8.

So when the programme runs, line number 5 will print 8 on the screen.

It is important to note that when the return line is executed, the function will terminate.

So if you look at the programme below, in the function add_numbers, as soon as the statement return answer is executed, the next line on line number 4, the statement print answer will not execute, the function terminates at line 3.

Let's do a quick recap.

What will be displayed when this programme is run? Pause the video here and have a quick think.

You are doing really well.

Did you come up with option B? That's right, the answer is nothing as line 4 in the programme will not run.

And the reason is that on line 4, the print statement, it has been placed after the return answer statement.

As soon as the return answer statement is executed, the function will terminate.

Right, so this brings us to the end of the first section.

You are doing really well.

But before we move on, let's just do a quick task just to make sure that you have understood what we covered in this section.

Now this task has four parts.

The first part asks you what is the return value from the function call find_highest, which has the parameters 12 and 9.

And the example code snippet is given in the image below.

Part two asks you to explain exactly what will happen when this particular code snippet is run.

Again, the example code snippet is given in the image below.

The third part of the task asks you to open the programme at the given link, run the code and see if your predictions are correct.

Did anything surprise you? Finally, the fourth part asks you to explain how values num1 and num2 are passed to the function.

Why don't you take a few minutes, pause the video here, have a think, and try and work these parts out.

I'm sure you'll be able to work them out.

Well done, pause the video here.

Right, the answer to the first part of task A, what is the return value from the function called find_highest but with the parameters 12, 9? The answer is 12.

Did you get that right? I'm sure you did, well done.

So if you look at the code example, the function find_highest, once it has the parameters 12 and 9, within the function, there is a check if a > b, then return a else: return b.

In our case, the if statement is true because 12 is greater than 9.

So the return a statement gets executed and the return value is 12.

Right, now let's look at the next part of the task.

The programme that is given here will take the values held by num1 and num2 and will add them together.

It'll then display the result 10 + 15 = 25.

Are you able to work that out? Well done.

The third part, did the link work as expected? Maybe the code didn't surprise you.

If it worked as you expected, then probably the code didn't surprise you.

Finally, the fourth part, 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 7 as arguments.

Really well done, we will now move on to our next section.

In this section, we will look at using functions to return values in programmes.

What do you think will happen when this code is run if the user types in 2 for the first number and 3 for the second number? The code snippet is given in the image below.

Sam says, "I think it'll display 2 to the power of 3 is 8." Jun disagrees, "I'm not sure that's right, Sam! I think the programme may have an error." Why don't you pause the video and have a quick think? Jun is correct.

The programme will return an error.

When you run this programme on the screen, you should get the text output NameError: name 'answer' is not defined on line 11 of main.

py.

So I'm going to click on this live coding video to investigate the error in the code and 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 2 for the first number and 3 for the second number, we have an error that is returned.

So the error says NameError: 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 4, 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 6, we then display another print message to ask the user to enter a second number.

And on line 7, we are storing that as the variable num2, again, as an integer.

On line 9, we are calling the subroutine, so to_the_power and we are passing the values num1 and num2 to the subroutine.

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

Now this is converting this 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: 2 for the first number, 3 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 num1, num2, which we're passing into the subroutine.

So if we run this again now with the same numbers, so 2 and 3, it's working correctly.

So we've got 2 to the power of 3 is 8, 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 2 followed by 3, you can see that it's still working and that 2 to the power of 3 is 8.

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.

Right, let's do a quick recap.

Attempt this true or false question.

A variable inside the function cannot have the same name as a variable outside the function.

What do you think? Why don't you pause the video here and have a go at this? That's right, this statement 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.

Well done, let's move on.

What Python key word do you think is used to share a value from a function to the rest of the programme? You've got three choices.

Pause the video here and have a think.

I'm sure you know the answer.

You've been doing so well.

The correct answer is A, return.

Well done on getting that right.

Right, this brings us to the end of this section.

Well done on getting this far.

To end this section, you will complete a task.

Task B is split into three parts.

The first part asks you to open the starter programme that you can find on the link that's given here.

This programme asks the user which operation they want to perform.

The programme then takes in the two numbers the user wants to perform the operation on.

The text output on running the programme is given here on the slide in the image.

Part two asks you to create a function for each operation.

So the four operations, add, subtract, multiply, and divide are given as a menu.

Part two is asking you to create a function for each of these operations and the function should return the result of the operation.

As you can see in the example image, the first one has been done for you.

The third part then asks you to create a selection statement that checks the operation the user wants to perform, and then calls the correct function.

Why don't you pause the video, have a look through these parts again, look at the starter programme, and try and work these out? You have been doing so well.

I'm sure you'll be able to work through this and write the final programme.

Pause the video here.

Well done on getting this far.

So this here shows you what a possible solution could be.

The link below will take you to the solution itself, but let's just quickly go through the solution on the screen here.

So we start off by defining the four functions: add, subtract, multiply, and divide.

Each of these functions takes two parameters, in this case x and y.

The result after performing the appropriate operation is returned back at the end of each function.

So for example, your add function takes the parameters x and y and returns the sum, returns x + y as the result of that function.

Similarly, subtract function takes x and y, performs the subtraction, and returns x - y as a result of that operation.

The third function, multiplication, takes x and y, and then returns the product x times y as a result of that operation.

Finally, division, the divide function, takes the parameters x and y and returns x / y.

Carrying on to the main programme, we can now see the starter programme that you started off with.

So the first few lines, so your lines 17 to 29 is the starter programme that you started off with.

So it displays the menu and takes the user in.

As you can see on line number 24, the user is asked to enter choice 1/2/3/4 based on the different menu options.

Line 25 reads the choice and stores it into the variable choice.

And then, the users ask to input the two numbers, which are then stored in num1 and num2 respectively.

Line 33 starts off the selection statements and you check if the choice is one, then the result will be to call the add function with the arguments.

Note that because it's a function call, you are passing, you are calling the function with the required number of arguments, in this case 2.

So num1 and num2 are your arguments that you are calling the add function with.

The functions called the result is stored in the variable result, and then a print statement prints out num1 + num2 equals the sum, the result.

elif choice == 2.

So, else if the choice is equal to 2, the result would be to call the subtract function with the arguments num1 and num2 and the value of the result is stored in the variable result.

This is followed by a print statement that prints num1 takeaway num2 equals result.

This is followed by another elif statement, so else if choice is 3.

At this time, the multiply function is called with the arguments num1 and num2 and the value is stored in result.

This is followed by a print statement where you are printing num1 times num2 equals result.

Finally, an else statement will catch the final choice and call the divide function with the arguments num1 and num2, and the result is stored at the variable result.

This is printed to say num1 divided by num2 equals result.

And that is your programme.

Really well done for attempting it.

Really well done for completing both sections of this lesson.

Let's now move on and summarise what we have covered in this lesson.

A function is a named block of code that is called to perform a specific task and will return a value.

Arguments are values that are passed or provided to a subroutine at the time it is called.

Subroutine parameters are values that are used by the subroutine that are passed from the arguments when it is called.