<?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=Python%2FC4%2FGetting-started-with-functions_%2FEnglish</id>
		<title>Python/C4/Getting-started-with-functions /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=Python%2FC4%2FGetting-started-with-functions_%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C4/Getting-started-with-functions_/English&amp;action=history"/>
		<updated>2026-04-08T19:59:52Z</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=Python/C4/Getting-started-with-functions_/English&amp;diff=507&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and welcome to the tutorial 'Get…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C4/Getting-started-with-functions_/English&amp;diff=507&amp;oldid=prev"/>
				<updated>2012-11-29T06:20:37Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and welcome to the tutorial &amp;#039;Get…&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;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello friends and welcome to the tutorial 'Getting started with functions'.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of this tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# Define a function.&lt;br /&gt;
# Define functions with arguments.&lt;br /&gt;
# Learn about docstrings.&lt;br /&gt;
# Learn about function return value.&lt;br /&gt;
# Read code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
Pre-requisite slide &lt;br /&gt;
| Before beginning this tutorial,we would suggest you to complete the tutorial on &amp;quot;Conditionals&amp;quot; and &amp;quot;Loops&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4 &lt;br /&gt;
&lt;br /&gt;
Function &lt;br /&gt;
| While writing code, we always want to reduce the number of lines of code, and functions is a way of reusing the code. Thus the same lines of code can be used as many times as needed. A function is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code. Now let us get more familiar with functions,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5 &lt;br /&gt;
&lt;br /&gt;
f(x) a mathematical function &lt;br /&gt;
| Consider a mathematical function f(x) = x square. Here x is a variable and with different values of x the value of function will change. When x is one, f(1) will return the value 1 and f(2) will return us the value 4. Let us now see how to define the function f(x) in python.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open the terminal &lt;br /&gt;
&lt;br /&gt;
 ipython&lt;br /&gt;
| Start your ipython interpreter by typing,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  def f(x):&lt;br /&gt;
  return x*x&lt;br /&gt;
| Let us define our function f(x)&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f(1)&lt;br /&gt;
 f(2)&lt;br /&gt;
| Well that defined the function, so before learning what we did let us see if it returns the expected values, try,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Yes, it returned 1 and 4 respectively. And now let us see what we did. We wrote two lines: The first line &amp;lt;tt&amp;gt;def f(x)&amp;lt;/tt&amp;gt; is used to define the name and the parameters to the function, and the second line is used to fix what the function is supposed to return. &amp;lt;tt&amp;gt;def&amp;lt;/tt&amp;gt; is a keyword and &amp;lt;tt&amp;gt;f&amp;lt;/tt&amp;gt; is the name of the function and &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; the parameter of the function.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| '''Write a python function named &amp;lt;tt&amp;gt;cube&amp;lt;/tt&amp;gt; which computes the cube of'''&lt;br /&gt;
&lt;br /&gt;
a given number n.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 def cube(n):&lt;br /&gt;
   return n**3&lt;br /&gt;
| Switch to your terminal for solution. The problem can be solved as,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  cube(2)&lt;br /&gt;
| Let us check whether our function returns the cube of a number or not&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  def greet():&lt;br /&gt;
    print &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
| It returned 8,which means we have defined our function,the right way. And now let us see how to write functions without arguments.&lt;br /&gt;
&lt;br /&gt;
let us define a new function called &amp;lt;tt&amp;gt;greet&amp;lt;/tt&amp;gt; which will print &amp;lt;tt&amp;gt;Hello World&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  greet()&lt;br /&gt;
| now we call the function as,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Well that is a function which takes no arguments. Also note that it is not mandatory for a function to return values. The function &amp;lt;tt&amp;gt;greet&amp;lt;/tt&amp;gt; neither takes any argument nor returns any value.&lt;br /&gt;
&lt;br /&gt;
Now let us see how to write functions with more than one argument.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7 &lt;br /&gt;
&lt;br /&gt;
Assignment 2 &lt;br /&gt;
| Write a python function named &amp;lt;tt&amp;gt;avg&amp;lt;/tt&amp;gt; which computes the average of &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;b&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 def avg(a,b):&lt;br /&gt;
     return (a + b)/2&lt;br /&gt;
| Switch to your terminal for solution.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  avg(20, 30)&lt;br /&gt;
| Let us test our function,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  def avg(a,b):&lt;br /&gt;
   &amp;quot;&amp;quot;&amp;quot; avg takes two numbers as input (a &amp;amp; b), and&lt;br /&gt;
 returns the average of a and b&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
 return (a+b)/2&lt;br /&gt;
| We get the correct average, 25. Thus if we want a function to accept more arguments, we just list them separated with a comma between the parenthesis after the function's name in the &amp;lt;tt&amp;gt;def&amp;lt;/tt&amp;gt; line.&lt;br /&gt;
&lt;br /&gt;
It is always a good practice to document the code that we write, and for a function we define, we should write an abstract of what the function does, and that is called a docstring. Let us modify the function &amp;lt;tt&amp;gt;avg&amp;lt;/tt&amp;gt; and add docstring to it. Do the following,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8 &lt;br /&gt;
&lt;br /&gt;
docstring &lt;br /&gt;
| Note that docstrings are entered in the immediate line after the function definition and put as a triple quoted string. And here as far as the code functionality is concerned, we didn't do anything. We just added an abstract of what the function does.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 avg?&lt;br /&gt;
| Now try this in the ipython interpreter.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f?&lt;br /&gt;
| It displays the docstring as we gave it. Thus docstring is a good way of documenting the function we write.&lt;br /&gt;
&lt;br /&gt;
Now type,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| It doesn't have a docstring associated with it. Also we cannot infer anything from the function name, and thus we are forced to read the code to understand about the function.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 9&lt;br /&gt;
&lt;br /&gt;
Assignment 3 &lt;br /&gt;
| Add docstring to the function f.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 10 &lt;br /&gt;
&lt;br /&gt;
Solution 3 &lt;br /&gt;
| We need to define the function again to add docstring to the function &amp;lt;tt&amp;gt;f&amp;lt;/tt&amp;gt; and we do it as,&lt;br /&gt;
&lt;br /&gt;
 def f(x):&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;Accepts a number x as argument and,&lt;br /&gt;
 returns the square of the number x.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
 return x*x&lt;br /&gt;
&lt;br /&gt;
Let us solve one more exercise Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 11 &lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 12 &lt;br /&gt;
&lt;br /&gt;
Assignment 4 &lt;br /&gt;
| '''Write a python function named &amp;lt;tt&amp;gt;circle&amp;lt;/tt&amp;gt; which returns the'''&lt;br /&gt;
&lt;br /&gt;
area and perimeter of a circle given radius &amp;lt;tt&amp;gt;r&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 def circle(r):&lt;br /&gt;
  &amp;quot;&amp;quot;&amp;quot;returns area and perimeter of a circle given radius r&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
  pi = 3.14&lt;br /&gt;
  area = pi * r * r&lt;br /&gt;
  perimeter = 2 * pi * r&lt;br /&gt;
  return area, perimeter&lt;br /&gt;
| Switch to the terminal for solution.&lt;br /&gt;
&lt;br /&gt;
The problem requires us to return two values instead of one which we were doing till now. We can solve the problem as,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  a, p = circle(6)&lt;br /&gt;
 print a&lt;br /&gt;
 print p&lt;br /&gt;
| A python function can return any number of values. There is no restriction for it.&lt;br /&gt;
&lt;br /&gt;
Let us call the function &amp;lt;tt&amp;gt;circle&amp;lt;/tt&amp;gt; as,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 13 &lt;br /&gt;
&lt;br /&gt;
what &lt;br /&gt;
| Now we have done enough coding, let us do some code reading exercise,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Pause here and try to figure out what the function &amp;lt;tt&amp;gt;what&amp;lt;/tt&amp;gt; does.&lt;br /&gt;
&lt;br /&gt;
'''def what( n ):'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;if n &amp;lt; 0: n = -n while n &amp;gt; 0:&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unexpected indentation.&lt;br /&gt;
&lt;br /&gt;
'''if n % 2 == 1:'''&lt;br /&gt;
&lt;br /&gt;
return False&lt;br /&gt;
&lt;br /&gt;
Definition list ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
n /= 10&lt;br /&gt;
&lt;br /&gt;
Block quote ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
return True&lt;br /&gt;
&lt;br /&gt;
continue from paused state It will return true if &amp;lt;tt&amp;gt;n % 2&amp;lt;/tt&amp;gt; is not equal to 1 and will return false,&lt;br /&gt;
&lt;br /&gt;
Unexpected indentation.&lt;br /&gt;
&lt;br /&gt;
otherwise.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 14 &lt;br /&gt;
&lt;br /&gt;
even_digits &lt;br /&gt;
&lt;br /&gt;
'''def even_digits( n ):'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&amp;quot;&amp;quot;&amp;quot;returns True if all the digits in the number n are even, returns False if all the digits in the number n are not even&amp;quot;&amp;quot;&amp;quot; if n &amp;lt; 0: n = -n while n &amp;gt; 0:&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unexpected indentation.&lt;br /&gt;
&lt;br /&gt;
'''if n % 2 == 1:'''&lt;br /&gt;
&lt;br /&gt;
return False&lt;br /&gt;
&lt;br /&gt;
Definition list ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
n /= 10&lt;br /&gt;
&lt;br /&gt;
Block quote ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
return True&lt;br /&gt;
| The function here returns &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt; if all the digits of the number &amp;lt;tt&amp;gt;n&amp;lt;/tt&amp;gt; are even, otherwise it returns &amp;lt;tt&amp;gt;False&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now one more code reading exercise,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 15 &lt;br /&gt;
&lt;br /&gt;
what &lt;br /&gt;
| Pause here and try to figure out what the function &amp;lt;tt&amp;gt;what&amp;lt;/tt&amp;gt; does.&lt;br /&gt;
&lt;br /&gt;
'''def what( n ):'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;i = 1 while i * i &amp;lt; n:&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unexpected indentation.&lt;br /&gt;
&lt;br /&gt;
i += 1&lt;br /&gt;
&lt;br /&gt;
Block quote ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
return i * i == n, i&lt;br /&gt;
&lt;br /&gt;
continue from paused state The function returns two values. One it returns the result of the while statement whether true of false, and second it prints the value that ''ii`'' currently holds.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 16 &lt;br /&gt;
&lt;br /&gt;
is_perfect_square &lt;br /&gt;
&lt;br /&gt;
'''def is_perfect_square( n ):'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&amp;quot;&amp;quot;&amp;quot;returns True and square root of n, if n is a perfect square, otherwise returns False and the square root of the next perfect square&amp;quot;&amp;quot;&amp;quot; i = 1 while i * i &amp;lt; n:&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unexpected indentation.&lt;br /&gt;
&lt;br /&gt;
i += 1&lt;br /&gt;
&lt;br /&gt;
Block quote ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
return i * i == n, i&lt;br /&gt;
| Here, the function returns &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt; and the square root of &amp;lt;tt&amp;gt;n&amp;lt;/tt&amp;gt; if n is a perfect square, otherwise it returns &amp;lt;tt&amp;gt;False&amp;lt;/tt&amp;gt; and the square root of the next perfect square.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 17 &lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of this tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# Define functions in Python by using the keyword &amp;lt;tt&amp;gt;def&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Call the function by specifying the function name.&lt;br /&gt;
# Assign a docstring to a function by putting it as a triple quoted string.&lt;br /&gt;
# Pass parameters to a function.&lt;br /&gt;
# Return values from a function.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
1. What will the function do?&lt;br /&gt;
&lt;br /&gt;
  def what(x)&lt;br /&gt;
      return x*x&lt;br /&gt;
 &lt;br /&gt;
 - Returns the square of x&lt;br /&gt;
 - Returns x&lt;br /&gt;
 - Function doesn't have docstring&lt;br /&gt;
 - Error&lt;br /&gt;
&lt;br /&gt;
# How many arguments can be passed to a python function?&lt;br /&gt;
** None&lt;br /&gt;
** One&lt;br /&gt;
** Two&lt;br /&gt;
** Any&lt;br /&gt;
# Write a function which calculates the area of a rectangle.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 18&lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
# The function will result into an error due to the use of wrong syntax in defining the function. The function line should always end with a colon&lt;br /&gt;
# Any number of arguments can be passed to a python function.&lt;br /&gt;
# As we know, area of a rectangle is product of it's length and breadth. Hence, we define our function as,&lt;br /&gt;
&lt;br /&gt;
Enumerated list ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
 def area(l,b):&lt;br /&gt;
     return l * b&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 19 &lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>