<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=PHP-and-MySQL%2FC2%2FFunctions-%28Advanced%29%2FEnglish</id>
		<title>PHP-and-MySQL/C2/Functions-(Advanced)/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=PHP-and-MySQL%2FC2%2FFunctions-%28Advanced%29%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=PHP-and-MySQL/C2/Functions-(Advanced)/English&amp;action=history"/>
		<updated>2026-04-08T20:01:40Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=PHP-and-MySQL/C2/Functions-(Advanced)/English&amp;diff=542&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !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 w…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=PHP-and-MySQL/C2/Functions-(Advanced)/English&amp;diff=542&amp;oldid=prev"/>
				<updated>2012-11-29T06:38:46Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Time !Narration |- |0:0 |Welcome to the Spoken Tutorial on Advanced Function. Here, I&amp;#039;ll show you how to create a little calculator program. |- |0:04 |We will deal w…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Time&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
|0:0&lt;br /&gt;
|Welcome to the Spoken Tutorial on Advanced Function. Here, I'll show you how to create a little calculator program.&lt;br /&gt;
|-&lt;br /&gt;
|0:04&lt;br /&gt;
|We will deal with a function that allows you to input a value.  Then gain a value out this, after a mathematical operation&lt;br /&gt;
|-&lt;br /&gt;
|0:13&lt;br /&gt;
|So, we'll create a function in the same way that we've done before. I'll call this 'calc'&lt;br /&gt;
|-&lt;br /&gt;
|0:19&lt;br /&gt;
|And I'm going to create my block first. Here inside, I'll type  'number1', 'number2' and an 'operator'&lt;br /&gt;
|-&lt;br /&gt;
|0:28&lt;br /&gt;
|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'&lt;br /&gt;
|-&lt;br /&gt;
|0:44&lt;br /&gt;
|Now inside our function we need to start creating our code. I'm going to create a switch statement inside.&lt;br /&gt;
|-&lt;br /&gt;
|0:54&lt;br /&gt;
|I'll say switch and put the switch condition or rather the input of the switch is 'op'&lt;br /&gt;
|-&lt;br /&gt;
|1:03&lt;br /&gt;
|I'll create a block for this and I'll say case = plus then carry out this.&lt;br /&gt;
|-&lt;br /&gt;
|1:14&lt;br /&gt;
|I'll make a new variable called 'total' which will be equal to 'num1' which is input here plus 'num2'&lt;br /&gt;
|-&lt;br /&gt;
|1:28&lt;br /&gt;
|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. &lt;br /&gt;
|-&lt;br /&gt;
|1:39&lt;br /&gt;
|So you will be able to use all different kinds of things inside other statements and inside functions.&lt;br /&gt;
|-&lt;br /&gt;
|1:45&lt;br /&gt;
|So I have created a case for 'plus'. So when this equals to 'plus' supplied by the user, we have 'num1' added to 'num2'.&lt;br /&gt;
|-&lt;br /&gt;
|1:57&lt;br /&gt;
|Now we need to go down and create another 'case', which is 'minus'. I'll type total = 'num1' - 'num2'&lt;br /&gt;
|-&lt;br /&gt;
|2:10&lt;br /&gt;
| We will scroll down. Make sure you break that. &lt;br /&gt;
|-&lt;br /&gt;
|2:16&lt;br /&gt;
|We'll now copy this code down. &lt;br /&gt;
|-&lt;br /&gt;
|2:20&lt;br /&gt;
|Here we'll say 'multiply' and here we'll say 'divide' and make sure you change the sign here.&lt;br /&gt;
|-&lt;br /&gt;
|2:27&lt;br /&gt;
|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.&lt;br /&gt;
|-&lt;br /&gt;
|2:36&lt;br /&gt;
|In the default we're going to echo out 'unknown operator'. OK?&lt;br /&gt;
|-&lt;br /&gt;
|2:45&lt;br /&gt;
|Let me just run through this. Then we will start to call the function. &lt;br /&gt;
|-&lt;br /&gt;
|2:50&lt;br /&gt;
|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'.&lt;br /&gt;
|-&lt;br /&gt;
|2:58&lt;br /&gt;
| As you have probably seen in my mathematical operator - sorry in my arithmetic operator tutorial.&lt;br /&gt;
|-&lt;br /&gt;
|3:14&lt;br /&gt;
|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.&lt;br /&gt;
|-&lt;br /&gt;
|3:32&lt;br /&gt;
|If it equals to a 'plus' then we will create a new variable called 'total'. &lt;br /&gt;
|-&lt;br /&gt;
|3:39&lt;br /&gt;
|That's going to be equal to the first number which was entered and added to the second number which was entered.&lt;br /&gt;
|-&lt;br /&gt;
|3:45&lt;br /&gt;
|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.&lt;br /&gt;
|-&lt;br /&gt;
|4:10&lt;br /&gt;
|Now this would do absolutely nothing. Refresh this. Now, if we enter this page, there is nothing, because we haven't called our function. &lt;br /&gt;
|-&lt;br /&gt;
|4:20&lt;br /&gt;
|Now to call our function, as you know, we will just say calc and we will put our values in.&lt;br /&gt;
|-&lt;br /&gt;
|4:25&lt;br /&gt;
|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?&lt;br /&gt;
|-&lt;br /&gt;
|4:45&lt;br /&gt;
|The reason is that we haven't echoed this out. We've just set it as a variable. &lt;br /&gt;
|-&lt;br /&gt;
|4:50&lt;br /&gt;
|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&lt;br /&gt;
|-&lt;br /&gt;
|5:00&lt;br /&gt;
|We have got nothing, because, there is no return output. So, in each case what we should say is 'return total'. &lt;br /&gt;
|-&lt;br /&gt;
|5:16&lt;br /&gt;
|What this does is - If you think of the function as a variable it sets the function's value as the total.&lt;br /&gt;
|-&lt;br /&gt;
|5:26&lt;br /&gt;
|As long as you say return whatever you say here the function will equal that. &lt;br /&gt;
|-&lt;br /&gt;
|5:31&lt;br /&gt;
|So we are going to say return total and we are going to copy that and paste it down for each case. &lt;br /&gt;
|-&lt;br /&gt;
|5:42&lt;br /&gt;
|Okay so obviously we don't need to do that for unknown operator. This is because, there is no operator to be found here.&lt;br /&gt;
|-&lt;br /&gt;
|5:49&lt;br /&gt;
|And we can refresh that.&lt;br /&gt;
|-&lt;br /&gt;
|5:49&lt;br /&gt;
|We still don't have anything. Guess why?&lt;br /&gt;
|-&lt;br /&gt;
|5:55&lt;br /&gt;
|The reason this is not working is because I've echoed this inside a function. That's a mistake.&lt;br /&gt;
|-&lt;br /&gt;
|6:03&lt;br /&gt;
|You can see the bracket of a function that starts here and ends here&lt;br /&gt;
|-&lt;br /&gt;
|6:08&lt;br /&gt;
|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&lt;br /&gt;
|-&lt;br /&gt;
|6:24&lt;br /&gt;
|So let's pick some different values, say, 13 and 7 and divide. Let's see what we get. &lt;br /&gt;
|-&lt;br /&gt;
|6:35&lt;br /&gt;
|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.&lt;br /&gt;
|-&lt;br /&gt;
|6:46&lt;br /&gt;
|And through a switch statement it detects which one and performs the relevant operation to it. &lt;br /&gt;
|-&lt;br /&gt;
|6:54&lt;br /&gt;
|An unknown operator error will be given if it can't be determined.&lt;br /&gt;
|-&lt;br /&gt;
|6:58&lt;br /&gt;
|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.&lt;br /&gt;
|-&lt;br /&gt;
|7:13&lt;br /&gt;
|This is Sidharth dubbing for the Spoken Tutorial Project. Thanks for watching.&lt;br /&gt;
|-&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>