Loading...
variables inside a subroutine
Key Stage 4
Year 10
Computer Science
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, "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 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 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 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 num1, num2, which we're passing into the subroutine. 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.
variables inside a subroutine
Key Stage 4
Year 10
Computer Science
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, "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 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 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 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 num1, num2, which we're passing into the subroutine. 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.