PHP-and-MySQL/C2/Functions-(Advanced)/English
From Script | Spoken-Tutorial
Time | Narration |
---|---|
0:0 | Welcome to the Spoken Tutorial on Advanced Function. Here, I'll show you how to create a little calculator program. |
0:04 | We will deal with a function that allows you to input a value. Then gain a value out this, after a mathematical operation |
0:13 | So, we'll create a function in the same way that we've done before. I'll call this 'calc' |
0:19 | And I'm going to create my block first. Here inside, I'll type 'number1', 'number2' and an 'operator' |
0:28 | 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:44 | Now inside our function we need to start creating our code. I'm going to create a switch statement inside. |
0:54 | I'll say switch and put the switch condition or rather the input of the switch is 'op' |
1:03 | I'll create a block for this and I'll say case = plus then carry out this. |
1:14 | I'll make a new variable called 'total' which will be equal to 'num1' which is input here plus 'num2' |
1:28 | I'll break that with a semicolon. Now there's probably a much easier way to do this by combining the switch statement with a function. |
1:39 | So you will be able to use all different kinds of things inside other statements and inside functions. |
1:45 | So I have created a case for 'plus'. So when this equals to 'plus' supplied by the user, we have 'num1' added to 'num2'. |
1:57 | Now we need to go down and create another 'case', which is 'minus'. I'll type total = 'num1' - 'num2' |
2:10 | We will scroll down. Make sure you break that. |
2:16 | We'll now copy this code down. |
2:20 | Here we'll say 'multiply' and here we'll say 'divide' and make sure you change the sign here. |
2:27 | Now if you don't understand what is going on here please feel free to contact us via e-mail. I hope every probable confusion will be resolved in that way. |
2:36 | In the default we're going to echo out 'unknown operator'. OK? |
2:45 | Let me just run through this. Then we will start to call the function. |
2:50 | 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 can either be 'plus' 'minus' 'multiply' or 'divide'. |
2:58 | As you have probably seen in my mathematical operator - sorry in my arithmetic operator tutorial. |
3:14 | 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 is much more efficient. |
3:32 | If it equals to a 'plus' then we will create a new variable called 'total'. |
3:39 | That's going to be equal to the first number which was entered and added to the second number which was entered. |
3:45 | 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:10 | 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:20 | Now to call our function, as you know, we will just say calc and we will put our values in. |
4:25 | 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 when I refresh this. Nothing. Now why? |
4:45 | The reason is that we haven't echoed this out. We've just set it as a variable. |
4:50 | So what we would ideally do is we will echo what has been out put from calc. Now, at that moment this won't do anything if we do a refresh |
5:00 | We have got nothing, because, there is no return output. So, in each case what we should say is 'return total'. |
5:16 | What this does is - If you think of the function as a variable it sets the function's value as the total. |
5:26 | As long as you say return whatever you say here the function will equal that. |
5:31 | So we are going to say return total and we are going to copy that and paste it down for each case. |
5:42 | 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:49 | And we can refresh that. |
5:49 | We still don't have anything. Guess why? |
5:55 | The reason this is not working is because I've echoed this inside a function. That's a mistake. |
6:03 | You can see the bracket of a function that starts here and ends here |
6:08 | I'll place this underneath here, where it should be and then refresh it. It is 20 okay, so we can see the 10 + 10 is 20 through our function |
6:24 | So let's pick some different values, say, 13 and 7 and divide. Let's see what we get. |
6:35 | Okay, we have got a quiet long decimal number. So you can see that this is quite a good function that we have made. We have got our first number, our second number and an operator. |
6:46 | And through a switch statement it detects which one and performs the relevant operation to it. |
6:54 | An unknown operator error will be given if it can't be determined. |
6:58 | 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:13 | This is Sidharth dubbing for the Spoken Tutorial Project. Thanks for watching. |