Chapter 3: Functions (Part III)

New terms from chapter 3 (Think Python: Think Like a Computer Scientist) – PART III

At this stage, we can define our own functions.

For instance, we defined the function ‘print_bruce’ which prints twice the parameter ‘bruce’.

>>> def print_twice(bruce):          # Here we define the function ‘print_twice’. The function has one (1) parameter: ‘bruce’.
…     print bruce                                 # The function will print the parameter ‘bruce’ two (2) times (or twice).
…     print bruce

>>>

Also, we can nest one function into another. I think of it like a computer programming Matryoshka.

For example:

>>> def sing_song (part1, part2): # Here we define the function ‘sing_song’. The function has two (2) parameters: ‘part1’ and ‘part2’
…     cat = part1 + part2                    # The variable ‘cat’ gets the value of the concatenation of parameter 1 and parameter 2.
…     print_twice (cat)                       # Here the function ‘print_twice’ is called and uses variable ‘cat’ as an argument.

>>>

Let us see what happens when we call the function ‘sing_song’ with the arguments: “Jingle” and “Bell”

>>> sing_song (“Jingle”, “Bell”)
JingleBell
JingleBell

Matryoshka dolls - Python nested functions

Matryoshka dolls – Python nested functions

Here is a more complete definition of what I am trying to convey :

In computer programming, a nested functions […] is a function which is lexically (textually) encapsulated within another function, with lexical scope […]

Due to nesting, the scope of the nested function is inside the enclosing function. This means that it can access local variables and other local functions in the enclosing function, while all of this is invisible outside the enclosing function.

Source: Wikipedia: Nested Function

3.10 Stack Diagrams

Stack diagrams are illustrations that can help us track who is who in nested functions and what sequence the program will follow in order to execute functions.

Stack diagrams show the value of each variable, but they also show the function each variable belongs to.

Each function is represented by a frame. A frame is a box with the name of a function beside it and the parameters and variables of the function inside it.

The frames are arranged in a stack that indicates which function called which, and so on.

Here is an example:

What happens if an error occurs during a function call ?

Python will help us trace the error back to its origin ( __main__ ).

– Traceback : Is a list that will help you find the error. It will provide:

  • the program file the error occurred in,
  • the line of code that caused the error, and
  • the functions that were executing at the time of the error.

3.11 Fruitful functions and void functions

– Fruitful functions: Allen B. Downey, Think Python’s author refers to fruitful functions, all the functions that will yield results (or return a value).

For instance, try this in the Python Shell (interactive mode):>>> import math     # First, you need to import that module math.
>>> math.sqrt(4)     # Then, let us remember the format to use in order to use a function from a module:  module . function (argument or variable)
2.0                               # This is the result that Python returns.
>>>

– Void functions: Void functions are functions, like ‘print_twice’ (that we defined earlier), that perform an action (either display something on the screen or perform some other action). However, they do not return a value.

For instance, we defined the function ‘print_twice’. The function is meant to perform the action of printing twice the parameter ‘bruce’.

In interactive mode:
Python will display the result of a void function if and only if we call a function in interactive mode.
In script mode:
When we call a fruitful function all by itself in script mode, the return value is lost forever if we do not store or display the result.
For instance:
>>> def print_twice(bruce):
…     print bruce
…     print bruce

>>> result = print_twice(‘Bing’)
Bing
Bing
>>> print result
None
>>>
It is important to note that although Python displayed the value of result earlier, the result displayed:
Bing
Bing
is lost forever since we did not store it anywhere.
In order to transmit this idea Python created the notion of ‘None’. It is a special value that has its own type.
>>> print type(None)
<type ‘NoneType’>
>>>

3.12 Why divide a program into functions?

  • By using functions, we are able to group statements. Thus, reading and debugging the program becomes easier. Just like playing with Lego blocks, a long program can be divided into functions (Lego blocks) that allow us to debug functions one at a time and the assembled them into a working whole.
  • By using functions, we can eliminate repetitive code. Therefore, functions are useful to make a program concise.
  • Well-designed functions can be useful for many programs.

3.13 Importing with from

We can use functions in modules in three different ways:

  • Import a module object in Python:

If you import math, you get a module object named math. The module object contains constants like pi and functions like sin and exp.

>>> import math
>>> print math
<module 'math' (built-in)>
>>> print math.pi          #Remember the format: module . function
3.14159265359
  • Import an object from a module in Python

>> from math import pi
Now you can access pi directly, without dot notation.

>>> print pi
3.14159265359
  • Import all objects from a module in Python
>>> from math import *

The advantage of importing everything from the math module is:
that your code can be more concise.

The disadvantage is:
that there might be conflicts between names defined in different modules, or between a name from a module and one of your variables.

3.14 Debugging

Python uses whitespaces indentation in order to delimit code blocks, a feature also termed the “off-side rule”.  An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block.

Source: Wikipedia: Python Programming Language

Indentation bugs are difficult to find because they are invisible. The solution is to find a text editor that will manage indentation for you.

The text editor I use is “Sublime Text version 2”. So, I use tabs while writing code. However, with Sublime Text 2, I have to save my program before I run it.

Think Python’s author, Allen B. Downey provides a great advice to find errors in a particular code block. Here it is:

Debugging can take a long time if you keep running the same, incorrect, program over and over!

Make sure that the code you are looking at is the code you are running. If you’re not sure, put something like print ‘hello’ at the beginning of the program and run it again. If you don’t see hello, you’re not running the right program!

Think Python, p. 28

 

***

Acknowledgments :

These notes represent my understanding from the book Think Python: How to Think Like a Computer Scientist written by Allen B. Downey.

Part of the chapter is transcribed and all the quotes unless specified otherwise come directly from his book.

Thank you Professor Downey for making this knowledge available.

 

Also, I would like to thank the open source community for their valuable contribution in making resources on programming available.

Thank you

2 thoughts on “Chapter 3: Functions (Part III)

  1. Pingback: Chapter 5 – Conditionals and recursion | Python Project

  2. Pingback: Exercises from chapter 3 – Think Python | Python Project

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s