Loading...
Hello, my name is Dr.
Das.
Welcome to Computing.
I'm so pleased that you have decided to join me for the lesson today.
In this lesson, we will be learning about procedures.
Welcome to today's lesson from the unit Programming Subroutines.
This lesson is called Procedures and by the end of this lesson, you should be able to create and call a procedure in a programme.
Some of the key words that we will be using throughout today's lesson are call.
Call an expression that passes control and arguments, if any, to a sub routine.
Procedure.
Procedure, a subroutine that executes a block of code when called but does not return a value.
Function.
Function, a sub-routine that returns a value.
Return value.
Return value, a value that is returned by a function.
This lesson is split into two parts.
Describe differences of procedures and functions and create and call a procedure.
Let's start by looking at the differences of procedures and functions.
A subroutine is a section of code that is designed to perform one specific task clearly and effectively.
Subroutines can be called upon whenever they are required by the main programme.
The image below shows you an example of a subroutine total underscore charge being defined.
A subroutine can be classified as a procedure or a function, as you can see in the image below.
The distinction between a procedure and a function can be confusing as not all programming languages distinguish between the two.
For example, in visual basic, the difference between a procedure and function is more obvious as you use the word function to define a function.
You can see this in the image examples below.
The procedure calculate is defined using the word sub and the function calculate area is defined using the word function.
In Python on the other hand, the difference between the two is less obvious as you can see in the image examples below, both the procedure calculate and the function calculate underscore area have been defined using the word def, D-E-F.
However, you will notice that both the functions have a return value, both the visual basic function calculate area and the Python function calculate underscore area, return the value area.
Jacob says, so a function must return at least one value.
Jude says, and a procedure does not return a value.
That's right, both of you.
Let's do a quick recap.
Which of the two blocks of code is a procedure? Pause the video here and have a quick think.
Did you choose option A? You were right.
Well done.
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 to appear on a screen.
You can also write your own procedures.
This procedure has been developed to display a menu screen.
The procedure will output the menu options.
What is an advantage of having the menu options in a procedure? If you want, you could post the video here and have a quick 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, they only have to do it once.
Did you come up with something similar? I'm sure you did.
Well done.
Let's move on.
Another quick recap.
What statement is used to create a procedure or function in Python? Why don't you pause the video here and and work this out.
Did you choose option A? I'm sure you did.
You are doing so well.
Right, let's quickly work on this task so that you can make sure that you have understood what we covered in this part.
Task A is made up of two parts.
The first part is asking you to describe the difference between a procedure and a function, and the second part is asking you which of the following would be suitable procedures.
You have to choose options from the four given options.
Why don't you pause the video here and try and work this out.
You're doing really well and I'm sure you can work out the answers to the two questions.
For the first question, where the difference between a procedure and a function is being asked? A procedure executes a list of commands when called.
A function will execute a list of commands and also return a value.
Did you come up with something similar? Which of the following would be suitable procedures? The correct answer is options two and three.
A section of code that takes a user's name and prints a greeting message and a section of code that displays the admission prices for a theme park.
Option one and option four cannot really be procedures because option one is a section of code that adds together sales over a week and returns the total value, the total sales to the main programme.
And option four is a section of code that calculates a 10% discount and returns a new total to the main programme.
Both option one and option four are returning values, so they would not be suitable procedures.
Well done on getting through the first section.
Let's move on.
We are now going to look at the second part of the lesson where you will learn how to create and call a procedure.
In Python, the syntax for defining a procedure is def calculate bracket open, A comma B bracket closed, followed by a colon.
Let's look at these different parts of that statement.
Def is used to define the procedure.
Calculate is the identifier that is used to give the procedure a meaningful name.
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, more, or no parameters.
Let's do a quick recap.
What are num1 and num2 in this line of code? Pause the video here and have a quick think.
You got that right.
The correct option is option C parameters.
Well done.
You must remember that defining a procedure will not run the procedure.
As you can see in this image here, the procedure calculate has been defined with parameters A and B, and what this procedure does is it calculates the sum of A and B, stores it in the variable answer, and then prints the value of answer.
To run the procedure, you need to call it in the main programme.
Lines four and five set the values of the variables, num1 and num2.
Line six is the procedure call.
Let's do a recap.
What will happen when this code is wrong? Why don't you pause the video here and have a quick think.
You're right, nothing will happen.
The procedure has been defined, but it has not been called.
Well done on figuring that out.
In Python, the syntax for a procedure call is for example, calculate bracket open num1 comma num2 bracket closed.
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 will notice that the parameters and arguments do not have the same name.
The procedure calculate has the parameters, A and B, while the procedure call on line number six has the arguments 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 our example, num1 becomes A and num2 becomes B, right.
Let's now do a task just to make sure you have understood what we have covered in this part.
Really well done on working through this part.
So the task B has a few parts.
I'm going to take you through these step-by-step.
You need to start by creating a new Python programme.
Then you need to define a procedure called average underscore value.
The average underscore value procedure should accept three parameters, A, B, and C.
The purpose of the average underscore value procedure is to calculate the average of three numbers and output them for the user.
In doing this, you create a variable called average and assign to it 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 following output.
The average value is followed by the average.
Next, you need to test your procedure by calling it, you can call your procedure by adding the following statement, average underscore value bracket open 6 comma 8 comma 10 bracket closed to the end of your programme.
If your programme is working correctly, it should display the following when it is executed.
The average value is eight.
As you can see, if you add up six, eight, and 10 and divide it by three, you will get eight.
So this means that your procedure works correctly.
Next, you delete the test line of code you added for part four.
Create three user prompts that ask for the three numbers.
The answers should be held in three variables, num1, num2, and num3.
Create a procedure call that will pass the arguments, num1, num2, and num3 to the average underscore value sub routine.
Finally, use the round function to round the average to two decimal places.
That is quite a lot of steps, so what I'd suggest you do is pause the video, go back and look at these steps and try to work out the programme step by step.
I am sure you can do it.
You are doing so well.
Pause the video here and take some time to work the code out.
Well done on giving this an attempt.
Let's see what the programme should look like.
We start by defining the procedure, average underscore value that takes the three parameters, A, B, and C.
Within this procedure, you calculate the average by adding A, B, and C and dividing it by three.
Then you round the average to two decimal places.
Finally, print out the average value.
Within the main body of the programme, you start at line number six by printing the statement, enter the first number.
You store the user input in num1 as shown on line number seven.
Line eight prints the statement, enter the second number.
Line nine takes the input and stores it in the variable, num2.
Line 10 prints the statement, enter the third number, and line 11 takes the user input and stores it in the variable num3.
Finally, line 13 calls procedure average underscore value with the arguments num1, num2, and num3.
You can find the solution for this task by clicking on the link below.
The final part of this task asks you to test your procedure using the data below.
Pause the video here and run your programme using these values that are shown in the table below.
Right, did you get the answers? I'm sure you did.
You've been doing so well.
So when you test your programme with the values 10, 20, and 30 as the three user inputs, you should get the average value as 20.
On testing the programme with the user values 25, 30 and 40, you should get the answer, the average value as 31.
67.
Finally, on testing your programme with the user values 40, 90 and 55, you should get the answer, 61.
67.
That brings us to the end of this lesson.
You have done so brilliantly.
Let's just quickly summarise what we have covered in this lesson.
A subroutine 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, and also returns a value.
To run a procedure, it must be called by the main programme.