Welcome to the Spoken Tutorial on Advanced Function. Here, I'll show you how to create a little calculator program.
We will deal with a function that allows you to input a value. Then gain a value out of this, after a mathematical operation
So, we'll create a function in the same way that we've done before. I'll call this 'calc'
And I'm going to create my first block . Here , I'll type 'number1', 'number2' and an 'operator'
Now this will be a numerical value. This will be an integer or decimal, depending on the user-input. This will also be the same and this will be the string value of either 'add' 'subtract' 'multiply' or 'divide'
Now inside our function we need to start creating our code. I'm going to create a switch statement inside.
I'll say switch and put the switch condition or rather the input of the switch is 'op'
I'll create a block for this and I'll say case = plus then carry out this.
I'll make a new variable called 'total' which will be equal to 'num1' which is input here plus 'num2'
I'll break that with a semicolon. Now there's probability a much easier way to do this by combining the switch statement with a function.
So you will be able to use all different kinds of things inside other statements and inside functions.
So I have created a case for 'plus'. So when this equals to 'plus' supplied by the user, we have 'num1' added to 'num2'.
Now we need to go down and create another 'case', which is 'minus'. I'll type total = 'num1' - 'num2'
We will scroll down. Make sure you break that.
We'll now copy this code down.
And here we'll say 'multiply' and we'll say 'divide' and make sure you change the sign here.
Now if you don't understand what is going on please feel free to contact us via e-mail. I hope every confusion will be resolved in that way.
In the default we're going to echo out 'unknown operator'. OK?
Let me just run through this. Then we will start to call the function.
I have got a function called calculator or calc for short, which takes a number as input, then a second number and then an operator which could be either 'plus' 'minus' 'multiply' or 'divide'.
As you have probably seen in my mathematical operator - sorry in my arithmetic operator tutorial.
Now we have a switch statement inside, which takes this 'op' into account. It takes what has been entered. Now if it equals to a 'plus', remember that it switches over to this statement. It is easier to write and much more efficient.
If it equals to a 'plus' then we will create a new variable called 'total'.
That's going to be equal to the first number which was entered and added to the second number which was entered.
Here we will say if it's a 'minus', then the variable 'total' - okay remember, the variable 'total' will only be set once for each case either plus or minus - so this total variable going to be number 1 - number 2 and the same for multiply and divide as well.
Now this would do absolutely nothing. Refresh this. Now, if we enter this page, there is nothing, because we haven't called our function.
Now to call our function, as you know, we will just say calc and put our values in.
Let's us just give it two numbers say 10 and 10 and a 'plus' . Okay, so that will be 20. Now watch what happens if I refresh this. Nothing. Now why?
The reason is that we haven't echoed this out. We've just set it as a variable.
So what we would ideally do is we will echo what has been out put from calc. Now, at the moment this won't do anything if we do a refresh
We have got nothing, because, there is no return output. So, in each case what we should say is 'return total'.
What this does is - If you think of the function as a variable it sets the function's value as the total.
As long as you say return whatever you say here the function will equal that.
So we are going to say return total and we are going to copy that and paste it down for each case.
Okay so obviously we don't need to do that for unknown operator. This is because, there is no operator to be found here.
And we can refresh that.
We still don't have anything. Guess why?
The reason this is not working is because I've echoed this inside a function. That's a mistake.
You can see the bracket of a function that starts here and ends here
I'll place this underneath here, where it should be and then refresh it. It's 20 okay, we can see the 10 + 10 is 20 through our function
So let's pick some different values, say, 13 and 7 and divide. Let's see what we get.
Okay, we have got a quiet long decimal number. So you can see that this is quite good function that we have made. We have got our first number, our second number and an operator.
And through a switch statement it detects which one and performs the relevant operation to it.
An unknown operator error will be given if it can't be determined.
So, for example let's take 'a' which is not a valid operator. As soon as we refresh it's going to unknown operator. This brings us to the end of the tutorial on advanced functions. We saw that we can input a value and then returned a value echoing out using a return command.
This is Sidharth dubbing for the Spoken Tutorial Project. Thanks for watching.