PHP-and-MySQL/C2/Functions-Advanced/English
From Script | Spoken-Tutorial
Time | Narration |
---|---|
0:0 | moving up to slightly more advanced functions I'm gonna show you how to create a little calculator program. |
0:04 | with a function that allows you to imper a value and then gain a value out this after a mathematical operation |
0:13 | okay so we'll go ahead by creating a function like we have done before.I'm gonna call this 'calc' |
0:19 | and I'm gonna create my block first and inside here I'm gonna have a 'number1' 'number2' and a 'operator' |
0:28 | now this will be a numerical value,this will be a integer or decimal depending on the user and this will also be the same and his will be the string value of either '+' '-' '/' '*' |
0:44 | okay now inside our function we need to start creating our code that's gonna do this what I'm gonna do is I'm gonna create a switch statement inside. |
0:54 | and I'm gonna say switch and the switch condition or rather the imper of the switch is 'op' |
1:03 | I'm gonna create a block for this and I'm gonna say case = + I'm gonna say to carry out this. |
1:14 | I'm gonna make a new variable called 'total' now it's gonna equal 'num1' here '+' 'num2' |
1:28 | that's the end of that and then I'm gonna break that and now it's probably a much easier way to do this and I'm sure I'm gonna combine switch statement with a function so you can give used to use all different kinds of things inside other statements. |
1:45 | and inside functions like that.so I have created a case for '+' so when this equals '+' that the user supply remember we have this added to this. |
1:57 | now we need to go down and we need to create another 'case' and that's gonna be '-' and that's gonna be total = 'num1' - 'num2' |
2:10 | okay we are just going down make sure you break that and we are just going to go down copy this down okay and for here we are going to say '*' and here '/' and make sure you change sign here. |
2:27 | now if you don't understand what is going on here and really need to know please e-mail me.i know it might be confusing it should be alright with that |
2:36 | now by default were going to echo out 'unknown operator' okay right.let me just run through this and then we will start to call the function well i have got function called calculator or calc for short which takes from the imper the number the second number and a operator |
2:58 | which can either be '+' '-' '*' '/' as you have probably seen in my mathematical operator my arthimatic operator tutorial |
3:14 | okay now we have a switch statement inside which takes this 'op' into a account it takes what has been entered. now if it equals a '+'remember switch is over statement its just easier to write and much more efficient |
3:32 | we are going to say if it equals '+' then we are going to create a new variable called total and that's gonna equal the number 1 that's entered or the first number that is entered added to the second number. |
3:45 | and here we are gonna say if it's a '-' we are gonna say the total creating a variable again it can't be remembered it can't be this and this so this total variable is only gonna be set once for each case and we are gonna say number 1 - number 2 and the same for multiply and divide |
4:10 | now this would do absolutely nothing i mean if i refresh this sorry if we enter this page there is nothing because we have called our function and now to call our function as you know we are just gonna say calc and we are gonna put our values in |
4:25 | we are going to say let's just give it two numbers say 10 and 10 and a '+' okay so that will be 20 now watch what happens when i refresh this nothing now why? |
4:45 | the reason being is because we haven't echoed this out it says a variable so what we really ideally do is we want to echo what has been out put from calc now the moment this won't do anything if we refresh |
5:00 | we have got nothing because there is no return out put so in each case what we are gonna say 'return total' and what this does is it creates if you think function as a variable it sets the functions value as the total |
5:26 | and when ever you say return and when ever you say here the function is gonna equal so we are gonna say return total and we are gonna copy that and gonna paste it down for each case |
5:39 | okay so obviously we don't need to do an unknown operator because there is no operator that's gonna be found here and we can refresh that. |
5:49 | and we still don't have anything okay now the reason this not working is because i very stupidly echoed this inside a function you can see the bracket of a function that starts here and ends here |
6:08 | I'm gonna place this underneath where it should be and make sure that's that and then we are gonna refresh and it was always 20 okay so we can see the 10 + 10 is 20 |
6:24 | so let's pick some different value say 13 and 7 and / and let's see what we get out okay we have got a quiet long decimal number so you can see this is a quiet 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 forms the relevant operation to it and an unknown operator arrow will be given if it can't be detemined |
6:58 | so for example 'a' that's not a valid operator so as soon as we refresh it's gonna unknown operator so that's basically a bit more advanced functions and we can see we can imper a value and return a value echoing out and using a return command |
7:13 | okay thank you for watching |