Python-3.4.3/C4/Getting-Started-with-Functions/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Getting started with functions.
Show Slide

Objectives


In this tutorial, we will learn to,
  • Define a function
  • Define functions with arguments and
  • use docstrings.
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisite

To practise this tutorial, you should know how to use tuples.


If not, see the relevant Python tutorials on this website.

Show Slide


Function

First we will learn about functions.


  • A function is a portion of code within a larger program that performs a specific task.
  • Functions are useful in reusing the code and eliminate code redundancy.
  • Functions are also used to organise our code into manageable blocks.
Slide:

Syntax of functions:


def function_name(parameters):"""docstring""" statement(s)

return [expression]


Highlight according to narration

Here is the syntax for defining the functions.


def is the keyword which defines the function name.


colon is used to mark the end of the function name.


docstring is the documentation string to describe what the function does.

It is an optional, but recommended.


Statement makes the function body and it must have 4 indentation level.


return statement is to return a value from the function and it is also indented by 4 spaces.

Show Slide:

Example: Function


f(x) a mathematical function


def f(x):

return x*x


Now we will understand the functions with an example.


Consider a mathematical function f of x is equal to x squared.


Here x is a variable. f of x changes when x changes.


Let us define our function f of x.


The first line def f of x is used to define the function name and its parameters.


The second line uses the function parameters to return the required value.

Open terminal Let us start ipython.


Open the terminal.

Type, ipython3

then press Enter

Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type,

def f(x):

return x*x

Type,

def f inside brackets x colon

return x asterisk x


and press the Enter key two times.

Type,

f(2)


f(2.5)


Let us call the f of x with different arguments.


Type,

f inside brackets 2


f inside brackets 2.5


It returned 4 and 6.25 respectively.

Type,

def greet():

print ("No function arguments")


Now let us see how to write functions without arguments.


Type the code as shown.


It defines a new function named greet which will print "No function arguments"

Type,

greet()

Now we will call the function as,greet open and close parentheses
Note that it is not mandatory for a function to return values.


The function greet neither takes any arguments nor returns any value.

Show Slide

docstrings


Next we will learn how to comment in a code.


Documenting/commenting code is a good practice.


Docstrings are triple quoted comments entered just after the function definition.


It implies what the function does.

Type, (type manually)def avg(a, b):

""" avg takes two numbers as input a & b. Returns the average of a and b"""

return (a + b) / 2.0

Let us write a function which returns average of two numbers.

Type the code as shown.


and press the Enter key two times.


Comments within triple quotes give a clear explanation about the code.

Type,

avg?


Type,

avg question mark


Here we can see the docstring of the function avg.

Type,

avg(3, 5)

Now let us pass 3 and 5 as values to the arguments a and b to the function avg.


Type, avg inside brackets 3 comma 5


We get 4.0 as output.

Pause the video.


Try this exercise and then resume the video.

Show Slide

Assignment 1

Write a function circle which returns the area and perimeter of a circle with given radius r.
Switch terminal Switch back to the terminal for the solution.
Type,

def circle(r):

"""returns area and perimeter of a circle

given radius r"""

pi = 3.14

area = pi * r * r

perimeter = 2 * pi * r return area, perimeter

Type the code as shown.


Press the Enter key two times.


The circle function requires us to return two values.


A python function can return any number of values in the form of a tuple.

Type,

a, p = circle(6)

Let us call the function circle as,

a comma p is equal to circle inside brackets 6

Type,

print (a, p)

Type,

print inside brackets a comma p


We can see the output as area and perimeter of a circle of radius 6.

Show Slide

Summary


This brings us to the end of this tutorial. Let us summarize.


In this tutorial, we have learnt to,

  • Define functions in Python
  • Call a function by specifying the function name
  • Write docstrings to a function by putting it as a triple quoted string
  • Pass parameters to a function
  • Return values from a function.
Show Slide


Self assessment questions

Here are some self assessment questions for you to solve
  1. How many arguments can be passed to a Python function?
  2. Write a function to find the area of a rectangle.
Show Slide

Solution of self assessment questions


And the answers,
  1. Any number of arguments can be passed to a Python function.
  1. We can write a function to find the area of a rectangle as:

def rectangle underscore area inside brackets l comma b colon

return l asterisk b

Show Slide Forum Please post your timed queries in this forum.
Show Slide Fossee Forum Please post your general queries on Python in this forum.
Slide Textbook Companion FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgment

http://spoken-tutorial.org

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Previous slide This is Priya from IIT Bombay signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Priyacst