<!DOCTYPE html>
<html>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="css/jquery.mobile-1.4.4.min.css">
		<link rel="stylesheet" href="css/style.css">
		<script src="js/jquery-1.11.1.min.js"></script>
		<script src="js/jquery.mobile-1.4.4.min.js"></script>

		<link rel="stylesheet" href="js/prettify/desert.css">
		<script src="js/prettify/prettify.js"></script>
		
	</head>
	<body onload="prettyPrint()">

		<div data-role="page" id="main" data-theme="b">
			<!-- For the topics panel -->

			<div id="pnlTopic" data-role="panel" data-display="overlay" data-swipe-close="true" data-dismissible="true" data-animate="true">
				<h2>Topics</h2>
				<ul data-role="listview">
					<li>
						<a href="variable.htm" rel="external">Variable</a>
					</li>
					<li>
						<a href="conditional.htm" rel="external">Conditional</a>
					</li>
					<li>
						<a href="function.htm" rel="external">Function</a>

					</li>
					<li>
						<a href="forLoop.htm" rel="external">For Loop</a>

					</li>
					<li>
						<a href="whileLoop.htm" rel="external">While Loop</a>

					</li>
					<li>
						<a href="list.htm" rel="external">List</a>

					</li>
					<li>
						<a href="string.htm" rel="external">String</a>
					</li>
					<li>
						<a href="dictionary.htm" rel="external">Dictionary</a>
					</li>
					<li>
						<a href="tuple.htm" rel="external">Tuple</a>
					</li>
					<li>
						<a href="challenge.htm" rel="external"><img src="images/challenge36.png" class="ui-li-icon">Python Challenge</a>
					</li>
				</ul>
			</div>

			<div data-role="header">
				<h1>Function</h1>
				<a href="#pnlTopic" class="ui-btn ui-icon-grid ui-btn-icon-left ui-btn-icon-notext"></a>
				<a href="index.htm" rel="external" class="ui-btn ui-icon-home ui-btn-icon-right ui-btn-icon-notext"></a>
			</div>

			<div data-role="main" class="ui-content">
				<div data-role="collapsibleset">
					<div data-role="collapsible">
						<h3>Function Declaration</h3>
						<p>
							Function is commonly used to group a series of statements together to achieve a specific objective.<br/><br/>
To declare a function:
						<pre class="prettyprint">
							
def add_one(x):
    '''
    This function takes in a number as argument and increment the number by 1
    '''
    print x + 1     	
						</pre>
						<p>
							To invoke a function:
						</p>
						<pre class="prettyprint">
							
>>> add_one(5)
6
>>> add_one(2)
3
						</pre>
					</div>
					<div data-role="collapsible">
						<h3>Fruitful Function</h3>
						<p>
							By default, all functions return None. Function that returns value is known as fruitful function.
						</p>
						<pre class="prettyprint">

def add_one(x):
    '''
    This function takes in a number as argument and increment the number by 1
    '''
    return x + 1
						</pre>
						<p>
							It is a good practice to assign the value returned by a function to a variable.
						</p>
						<pre class="prettyprint">

>>> x = add_one(5)
>>> print x
6
						</pre>
					</div>
					<div data-role="collapsible">
						<h3>Using Built-in Function</h3>
						<p>
							 The python installation is pre-packaged with many modules which contain some commonly used functions. One such module is the math module. To use a builtin module, you have to use the import statement.
						</p>
						<pre class="prettyprint">

>>> import math
# Note: need to include the module name in the function call
>>> math.sqrt(4.0)              
2
>>> import random
>>> random.randint(0, 100)
>>> 20
						</pre>	
					</div>
					<div data-role="collapsible">
						<h3>Anonymous Function</h3>
						<p>
							 The <em>lambda</em> keyword can be used to create small anonymous functions. An example of using lambda keyword is shown below:
						</p>
						<pre class="prettyprint">
							
# LHS of colon : arguments 
# RHS of colon: function body
>>> total = lambda num1, num2: num1 + num2      
>>> total(1,2)
3
						</pre>	
					</div>
					<div data-role="collapsible">
						<h3>Code Snippets</h3>
						<p>
							This section will contain some code snippets to show some practical use of the topic that you have learnt. 
						</p>
						<pre class="prettyprint">

# Calculation without using function
# ----------------------------------
# circle with radius of 3
areaOfCircle1 = 3.14 * 3 * 3

# circle with radius of 4
areaOfCircle2 = 3.14 * 4 * 4

totalArea = areaOfCircle1 + areaOfCircle2


# Calculation using function
# ----------------------------------
def areaOfCircle(r):
	return 3.14 * r * r

totalArea = areaOfCircle(3) + areaOfCircle(4)
						</pre>
					</div>
					
				</div>
			</div>
			
		</div>

	</body>
</html>