Difference between revisions of "PHP-and-MySQL/C2/Functions-Advanced/English-timed"
From Script | Spoken-Tutorial
(Created page with '{| border=1 !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 …') |
Sandhya.np14 (Talk | contribs) |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | + | |'''Time''' | |
− | + | |'''Narration''' | |
|- | |- | ||
− | | | + | |00:03 |
− | |Welcome to the Spoken Tutorial on Advanced | + | |Welcome to the Spoken Tutorial on '''Advanced Functions'''. Here, I'll show you how to create a little calculator program. |
|- | |- | ||
− | | | + | |00:11 |
− | |We will deal with a function that allows you to input a value | + | |We will deal with a '''function''' that allows you to '''input''' a value, then gain a value out of this after a mathematical operation. |
|- | |- | ||
− | | | + | |00:20 |
− | |So, we'll create a function in the same way that we've done before. I'll call this 'calc' | + | |So, we'll create a function in the same way that we've done before. I'll call this 'calc'. |
|- | |- | ||
− | | | + | |00:27 |
− | |And I'm going to create my first block . Here , I'll type | + | |And I'm going to create my first '''block''' . Here, I'll type 'number1', 'number2' and an 'operator'. |
|- | |- | ||
− | | | + | |00: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' | + | |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'. |
|- | |- | ||
− | | | + | |00:52 |
− | |Now inside our function we need to start creating our code. I'm going to create a switch statement inside. | + | |Now, inside our '''function''' we need to start creating our '''code'''. I'm going to create a '''switch''' statement inside. |
|- | |- | ||
|01:00 | |01:00 | ||
− | |I'll say switch and put the switch condition or rather the input of the switch is 'op' | + | |I'll say '''switch''' and put the '''switch condition''' or rather the input of the switch is '''$op'''. |
|- | |- | ||
− | | | + | |01:09 |
− | |I'll create a block for this and I'll say case | + | |I'll create a '''block''' for this and I'll say '''case''' "+" (plus) then carry out this. |
|- | |- | ||
− | | | + | |01:18 |
− | |I'll make a new variable called 'total' which will be equal to 'num1' which is input here plus 'num2' | + | |I'll make a new variable called '''$total''' which will be equal to 'num1' which is input here plus 'num2'. |
|- | |- | ||
− | | | + | |01:32 |
− | |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. | + | |I'll '''break''' that with a semicolon. Now there's a probability, a much easier way to do this by combining the '''switch''' statement with a function. |
|- | |- | ||
− | | | + | |01:44 |
− | |So you will be able to use all different kinds of things inside other statements and inside functions. | + | |So, you will be able to use all different kinds of things inside other statements and inside functions. |
|- | |- | ||
− | | | + | |01:52 |
− | |So I have created a case for 'plus'. So when this equals to 'plus' supplied by the user, we have 'num1' added to 'num2'. | + | |So I have created a '''case''' for 'plus'. So when this equals to 'plus' supplied by the user, we have '$num1' added to '$num2'. |
|- | |- | ||
− | | | + | |02:03 |
− | |Now we need to go down and create another 'case' | + | |Now we need to go down and create another '''case''' which is 'minus'. I'll type '''$total = $num1 - $num2;''' |
|- | |- | ||
− | | | + | |02:17 |
− | | We will scroll down | + | | We will scroll down; make sure you break that. |
|- | |- | ||
− | | | + | |02:21 |
|We'll now copy this code down. | |We'll now copy this code down. | ||
|- | |- | ||
− | | | + | |02:24 |
− | |And here we'll say | + | |And here, we'll say "*" (multiply) and we'll say "/" (divide) and make sure you change the sign here. |
|- | |- | ||
− | | | + | |02: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. | |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. | ||
|- | |- | ||
− | | | + | |02:45 |
− | |In the default we're going to echo | + | |In the default, we're going to '''echo''' out "Unknown operator". OK? |
|- | |- | ||
− | | | + | |02:51 |
− | |Let me just run through this. Then we will start to call the function. | + | |Let me just run through this. Then we will start to '''call''' the '''function'''. |
|- | |- | ||
− | | | + | |02:56 |
− | |I have got a function called calculator or calc for short | + | |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'. |
|- | |- | ||
− | | | + | |03:12 |
− | | As you have probably seen in my mathematical operator - sorry in my | + | | As you have probably seen in my mathematical operator - sorry in my '''Arithmetic Operator''' tutorial. |
|- | |- | ||
− | | | + | |03:20 |
− | |Now we have a switch statement inside | + | |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. |
|- | |- | ||
− | | | + | |03:42 |
− | |If it equals to a | + | |If it equals to a "+" (plus) then we will create a new variable called 'total'. |
|- | |- | ||
− | | | + | |03:48 |
|That's going to be equal to the first number which was entered and added to the second number which was entered. | |That's going to be equal to the first number which was entered and added to the second number which was entered. | ||
|- | |- | ||
− | | | + | |03:56 |
− | |Here we will say if it's a | + | |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 number1 - number2 and the same for multiply and divide as well. |
|- | |- | ||
− | | | + | |04:21 |
− | |Now this would do absolutely nothing. Refresh this. Now, if we enter this page, there is nothing | + | |Now this would do absolutely nothing. '''Refresh''' this. Now, if we enter this page, there is nothing because we haven't called our '''function'''. |
|- | |- | ||
− | | | + | |04:33 |
− | |Now to call our function, as you know, we will just say calc and put our values in. | + | |Now to call our '''function''', as you know, we will just say '''calc();''' and put our values in. |
|- | |- | ||
− | | | + | |04:40 |
− | |Let | + | |Let 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? |
|- | |- | ||
− | | | + | |04:55 |
|The reason is that we haven't echoed this out. We've just set it as a variable. | |The reason is that we haven't echoed this out. We've just set it as a variable. | ||
|- | |- | ||
− | | | + | |05:01 |
− | |So what we | + | |So, what we ideally do is, we will echo what has been '''output''' from '''calc'''. Now, at the moment this won't do anything if we do a '''refresh'''. |
|- | |- | ||
− | | | + | |05:11 |
− | |We have got nothing | + | |We have got nothing because there is no return output. So, in each case, we should say '''return''' '''$total;'''. |
|- | |- | ||
− | | | + | |05:24 |
− | |What this does is - | + | |What this does is - if you think of the function as a variable, it sets the function's value as the '''$total'''. |
|- | |- | ||
− | | | + | |05:32 |
− | |As long as you say return whatever you say here the function will equal that. | + | |As long as you say '''return''', whatever you say here, the function will equal that. |
|- | |- | ||
− | | | + | |05:39 |
− | |So we are going to say return total and we are going to copy that and paste it down for each case. | + | |So, we are going to say '''return $total''' and we are going to copy that and paste it down for each '''case'''. |
|- | |- | ||
− | | | + | |05:47 |
− | |Okay | + | |Okay. So, obviously we don't need to do that for "Unknown operator". This is because there is no operator to be found here. |
|- | |- | ||
− | | | + | |05:58 |
− | |And we can refresh that. | + | |And we can '''refresh''' that. |
|- | |- | ||
− | | | + | |06:00 |
|We still don't have anything. Guess why? | |We still don't have anything. Guess why? | ||
|- | |- | ||
− | | | + | |06:04 |
|The reason this is not working is because I've echoed this inside a function. That's a mistake. | |The reason this is not working is because I've echoed this inside a function. That's a mistake. | ||
|- | |- | ||
− | | | + | |06:10 |
− | |You can see the bracket of a function that starts here and ends here | + | |You can see the bracket of a function that starts here and ends here. |
|- | |- | ||
− | | | + | |06:15 |
− | |I'll place this underneath 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. |
|- | |- | ||
− | | | + | |06:37 |
|So let's pick some different values, say, 13 and 7 and divide. Let's see what we get. | |So let's pick some different values, say, 13 and 7 and divide. Let's see what we get. | ||
|- | |- | ||
− | | | + | |06:46 |
− | |Okay, we have got a | + | |Okay, we have got a quite 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. |
|- | |- | ||
− | | | + | |07:00 |
− | |And through a switch statement it detects which one and performs the relevant operation to it. | + | |And through a '''switch''' statement, it detects which one and performs the relevant operation to it. |
|- | |- | ||
− | | | + | |07:06 |
− | |An | + | |An "Unknown operator" error will be given if it can't be determined. |
|- | |- | ||
− | | | + | |07:11 |
− | |So, for example let's take | + | |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. |
|- | |- | ||
− | | | + | |07:31 |
− | |This is Sidharth dubbing for the Spoken Tutorial Project. Thanks for watching. | + | |This is Sidharth, dubbing for the Spoken Tutorial Project. Thanks for watching. |
|- | |- |
Latest revision as of 22:39, 25 May 2015
Time | Narration |
00:03 | Welcome to the Spoken Tutorial on Advanced Functions. Here, I'll show you how to create a little calculator program. |
00: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. |
00:20 | So, we'll create a function in the same way that we've done before. I'll call this 'calc'. |
00:27 | And I'm going to create my first block . Here, I'll type 'number1', 'number2' and an 'operator'. |
00: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'. |
00: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. |
01:09 | I'll create a block for this and I'll say case "+" (plus) then carry out this. |
01:18 | I'll make a new variable called $total which will be equal to 'num1' which is input here plus 'num2'. |
01:32 | I'll break that with a semicolon. Now there's a probability, a much easier way to do this by combining the switch statement with a function. |
01:44 | So, you will be able to use all different kinds of things inside other statements and inside functions. |
01:52 | So I have created a case for 'plus'. So when this equals to 'plus' supplied by the user, we have '$num1' added to '$num2'. |
02:03 | Now we need to go down and create another case which is 'minus'. I'll type $total = $num1 - $num2; |
02:17 | We will scroll down; make sure you break that. |
02:21 | We'll now copy this code down. |
02:24 | And here, we'll say "*" (multiply) and we'll say "/" (divide) and make sure you change the sign here. |
02: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. |
02:45 | In the default, we're going to echo out "Unknown operator". OK? |
02:51 | Let me just run through this. Then we will start to call the function. |
02: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'. |
03:12 | As you have probably seen in my mathematical operator - sorry in my Arithmetic Operator tutorial. |
03: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. |
03:42 | If it equals to a "+" (plus) then we will create a new variable called 'total'. |
03:48 | That's going to be equal to the first number which was entered and added to the second number which was entered. |
03: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 number1 - number2 and the same for multiply and divide as well. |
04: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. |
04:33 | Now to call our function, as you know, we will just say calc(); and put our values in. |
04:40 | Let 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? |
04:55 | The reason is that we haven't echoed this out. We've just set it as a variable. |
05:01 | So, what we ideally do is, we will echo what has been output from calc. Now, at the moment this won't do anything if we do a refresh. |
05:11 | We have got nothing because there is no return output. So, in each case, we should say return $total;. |
05:24 | What this does is - if you think of the function as a variable, it sets the function's value as the $total. |
05:32 | As long as you say return, whatever you say here, the function will equal that. |
05:39 | So, we are going to say return $total and we are going to copy that and paste it down for each case. |
05: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. |
05:58 | And we can refresh that. |
06:00 | We still don't have anything. Guess why? |
06:04 | The reason this is not working is because I've echoed this inside a function. That's a mistake. |
06:10 | You can see the bracket of a function that starts here and ends here. |
06:15 | 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. |
06:37 | So let's pick some different values, say, 13 and 7 and divide. Let's see what we get. |
06:46 | Okay, we have got a quite 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. |
07:00 | And through a switch statement, it detects which one and performs the relevant operation to it. |
07:06 | An "Unknown operator" error will be given if it can't be determined. |
07: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. |
07:31 | This is Sidharth, dubbing for the Spoken Tutorial Project. Thanks for watching. |