Loading...
Hello, my name is Mrs. Holborow, and welcome to Computing.
I'm so pleased you've decided to join me for the lesson today.
In this lesson, we'll be exploring the difference between a procedure and a function.
And we'll look at how we can create and call a procedure in a programme.
Welcome to today's lesson from the unit "Programming: subprograms." This lesson is called "Procedures in programmes." And by the end of today's lesson, you'll be able to create and call a procedure in a programme.
Shall we make a start? We will be exploring these keywords in today's lesson.
Call, call, an expression that passes control and arguments, if any, to a subprogram.
Procedure, procedure, a subprogram that executes a block of code when called but does not return a value.
Function, function, a subprogram that returns a value.
Return value, return value, a value that is returned by a function.
Look out for these keywords in today's lesson.
Today's lesson is broken down into two parts.
We'll start by describing differences of procedures and functions, and then we'll move on to create and call a procedure.
Let's start by describing differences of procedures and functions.
A subprogram is a section of code that is designed to perform one specific task clearly and effectively.
Subprograms can be called upon whenever they're required by the main programme.
So here we have a subprogram which is called total_charge, and that takes in a parameter which is called total_walks.
This subprogram has been designed to calculate the total cost of some dog walks.
A subprogram can be classified as a procedure or a function.
The distinction between a procedure and a function can be confusing as not all programming languages distinguish between the two.
In Visual Basic, the difference between a procedure and function is more obvious as you use the word Function to define a function.
So here, the top block of code is a procedure in Visual Basic.
The bottom section of code is a function.
In Python, the difference between the two is less obvious.
So you can see, they're both defined using the keyword def.
However, you will notice that both functions have a return value.
So you can see in Visual Basic, we have Return area, and then same in Python on line 3, we have return area.
Just a slightly different syntax.
Jacob says, "So a function must return at least one value." Jun says, "And a procedure does not return a value." That's great.
Well done, Jacob and Jun.
Time to check your understanding.
Which of the two blocks of code is a procedure? Is it a or b? Pause the video whilst you have a look carefully at the code.
Did you select a? Well done, well spotted.
The code in b has a return value, so it's not a procedure.
A common procedure that you have used is print().
You may not fully understand how the print() procedure works, but you will have used it to make text, numbers, or symbols appear on a screen.
You can also create your own procedures.
This procedure has been developed to display a menu on the screen.
The procedure will output the menu options.
So we have def display_menu(): and then we have three print lines, (1: Log in"), ("2: Sign up"), ("9: Quit").
What is an advantage of having the menu options in a procedure? Maybe pause the video whilst you have a think.
The advantage of having the menu options in a procedure is that it can be called multiple times, and if the programmer wants to change the menu options, then they only have to do it once.
Time to check your understanding.
What statement is used to create a procedure or function in Python? Is it a, def, b, sub, or c, call? Pause the video whilst you have a think.
Did you select a? Well done.
I knew you'd get that right.
Okay, we're now moving on to our first set of tasks for today's lesson, and you're doing a fantastic job so far, so well done.
I'd like you to start by describing the difference between a procedure and a function.
Pause the video whilst you answer the question.
For part 2, I'd like you to identify which of the following would be suitable as procedures.
So the first one is, a section of code that adds together sales over a week and returns the total sales to the main programme.
The next one, a section of code that takes a user's name and prints out a greeting message.
The third one, a section of code that displays the admission prices for a theme park.
And then the last one, a section of code that calculates a 10% discount and returns the new total to the main programme.
Pause the video whilst you complete the task.
How did you get on? For part 1, you were asked to describe the difference between a procedure and a function.
Here's a sample answer.
A procedure executes a list of commands when called.
A function will execute a list of commands, but it will also return a value.
For part 2, you are asked to identify which of the following would be suitable procedures.
Hopefully, you've ticked the two that are on the screen.
So, a section of code that takes a user's name and prints a greeting message, and a section of code that displays the admission price for a theme park.
They would be both suitable as procedures.
The other two aren't because they are required to return values to the main programme.
Okay, we're now moving on to our second part of today's lesson, where we're going to create and call a procedure.
In Python, the syntax for defining a procedure is, def is used to define the procedure, calculate is the identifier that is used to give the procedure a meaningful name.
So this can be called whatever you want it to be.
And then here, a and b are the parameters in this procedure.
Parameters define the data that must be passed to a procedure when it is called.
The parameters are treated as local variables inside the procedure.
A procedure can have one, two, or no parameters.
Time to check your understanding.
What are num1 and num2 in this line of code? Are they a, identifiers, b, functions, or c, parameters? Pause the video whilst you have a think.
Did you select c, parameters? Well done.
Defining a procedure will not run the procedure.
So this section of code will not run unless we call it.
To run the procedure, you need to call it in the main programme.
So you can see on line 6, I'm calling the procedure calculate.
Lines 4 and 5 set the values of the variables num1 and num2.
Line 6 is the procedure call.
Time to check your understanding.
What will happen when this code is run? Pause the video whilst you have a think.
That's right, nothing will happen.
The procedure has been defined, but it has not been called, so it won't run.
In Python, the syntax for a procedure call is the name of the procedure followed by the arguments that we're going to pass to it.
So calculate is the name of the procedure being called.
num1 and num2 are the arguments.
Values held in the arguments are passed to the parameters of the procedure.
You'll notice that the parameters and arguments do not have the same name.
So at the top, the parameters are called a and b, and in the procedure call, the arguments are called num1 and num2.
They do not have to be called the same thing, but there does need to be the same number of arguments as parameters.
In this case, num1 becomes a and num2 becomes b.
Okay, we're moving on to our second 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 creating a new Python programme.
I'd then like you to define a procedure called average_value.
The average_value procedure should accept three parameters, a, b, and c.
The purpose of the average_value procedure is to calculate the average of three numbers and output them for the user.
Create a variable called average and assign it to an expression that will calculate the average of the three values passed through the parameters.
After the average has been calculated, the programme should display the output: The average value is and then the value held by the variable average.
Pause the video whilst you complete your programme.
For part 4, test your procedure by calling it.
You can call your procedure by adding this line of code, average_value, and then you can pass it the arguments 6, 8, and 10.
If your programme is working correctly, it should display the following when it is executed: The average value is 8.
0.
Pause the video whilst you test your procedure.
For part 5, delete the test line of code you added for part 4.
Create three user prompts that ask for the three numbers.
The numbers should be held in three variables, which are called num1, num2, and num3.
Create a procedure call that will pass the arguments num1, num2, and num3 to the average_value subprogram.
Finally, for part 6, use the round() function to round the average to two decimal places.
Pause the video whilst you complete the tasks.
Let's have a look at a solution together.
If you want to go and look at the full working solution, you can go to oak.
link/average-procedure-solution.
So on line 1, I'm defining the procedure average_value, and the parameters are a, b, and c.
Inside the procedure, I'm calculating the average.
So average = (a+b+c), in brackets, /3 because there's three numbers.
On line 3, average = round(average, 2).
That's rounding the value held in average to two decimal places.
On line 4, I'm printing "The average value is" and then the value held by the variable average.
On line 6 to 11, I'm printing the statements to the user and storing their numbers as integers.
So, for example, on line 6, print(f"Enter the first number:").
On line 7, num1 = int(input()).
I just repeat that for num2 and num3.
And then finally on line 13, I'm calling the procedure.
So I have average_value, and I'm passing the arguments num1, num2, and num3 to the procedure.
Remember, if you need to make any corrections to your code, you can pause the video and do that now.
Now let's test your procedures using the data below.
So, I've given you three different sets of values for num1, num2, and num3, and you should be able to calculate the average value using your procedure.
Have a go now.
How did you get on? If your procedure was working correctly, then the average value for the first set of numbers, so 10, 20, and 30, should be 20.
The second set of numbers, 25, 30, and 40, the average should be 31.
67.
And then the final set of numbers, 40, 90, and 55, the average should be 61.
67.
Did your procedures calculate those correctly? Great work.
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 subprogram can be classified as a procedure or a function.
A procedure executes a list of commands when called.
A function executes a list of commands when called, but it also returns a value.
To run a procedure, it must be called by the main programme.
I hope you've enjoyed today's lesson, and I hope you'll join me again soon, bye.