PHP-and-MySQL/C2/Functions-Advanced/English-timed
From Script | Spoken-Tutorial
Time | Narration |
---|---|
0:02 | Welcome to the Spoken Tutorial on Advanced Function. Here, I'll show you how to create a little calculator program. |
0:11 | We will deal with a function that allows you to input a value. Then gain a value out of this, after a mathematical operation |
0:19 | So, we'll create a function in the same way that we've done before. I'll call this 'calc' |
0:26 | And I'm going to create my first block . Here , I'll type 'number1', 'number2' and an 'operator' |
0:35 | 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' |
0:52 | Now inside our function we need to start creating our code. I'm going to create a switch statement inside. |
01:00 | I'll say switch and put the switch condition or rather the input of the switch is 'op' |
1:08 | I'll create a block for this and I'll say case = plus then carry out this. |
1:18 | I'll make a new variable called 'total' which will be equal to 'num1' which is input here plus 'num2' |
1:31 | 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. |
1:43 | So you will be able to use all different kinds of things inside other statements and inside functions. |
1:51 | So I have created a case for 'plus'. So when this equals to 'plus' supplied by the user, we have 'num1' added to 'num2'. |
2:03 | Now we need to go down and create another 'case', which is 'minus'. I'll type total = 'num1' - 'num2' |
2:17 | We will scroll down. Make sure you break that. |
2:20 | We'll now copy this code down. |
2:23 | And here we'll say 'multiply' and we'll say 'divide' and make sure you change the sign here. |
2:34 | 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. |
2:45 | In the default we're going to echo out 'unknown operator'. OK? |
2:51 | Let me just run through this. Then we will start to call the function. |
2:56 | 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'. |
3:12 | As you have probably seen in my mathematical operator - sorry in my arithmetic operator tutorial. |
3:20 | 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. |
3:42 | If it equals to a 'plus' then we will create a new variable called 'total'. |
3:48 | That's going to be equal to the first number which was entered and added to the second number which was entered. |
3:56 | 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. |
4:21 | Now this would do absolutely nothing. Refresh this. Now, if we enter this page, there is nothing, because we haven't called our function. |
4:32 | Now to call our function, as you know, we will just say calc and put our values in. |
4:40 | 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? |
4:55 | The reason is that we haven't echoed this out. We've just set it as a variable. |
5:01 | 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 |
5:11 | We have got nothing, because, there is no return output. So, in each case what we should say is 'return total'. |
5:23 | What this does is - If you think of the function as a variable it sets the function's value as the total. |
5:32 | As long as you say return whatever you say here the function will equal that. |
5:39 | So we are going to say return total and we are going to copy that and paste it down for each case. |
5:47 | Okay so obviously we don't need to do that for unknown operator. This is because, there is no operator to be found here. |
5:57 | And we can refresh that. |
6:00 | We still don't have anything. Guess why? |
6:04 | The reason this is not working is because I've echoed this inside a function. That's a mistake. |
6:10 | You can see the bracket of a function that starts here and ends here |
6:14 | 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 |
6:37 | So let's pick some different values, say, 13 and 7 and divide. Let's see what we get. |
6:46 | 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. |
6:59 | And through a switch statement it detects which one and performs the relevant operation to it. |
7:06 | An unknown operator error will be given if it can't be determined. |
7:11 | 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. |
7:31 | This is Sidharth dubbing for the Spoken Tutorial Project. Thanks for watching. |