Chapter 3: Functions (Part II)

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

This second part is by far the more abstract and conceptual reading so far. So, let us keep moving forward.

3.7 Flow of execution

Flow of execution: the order in which statements are executed

Execution always begins at the first statement of the program. Statements are executed one at a time, in order from top to bottom.

Function definitions do not alter the flow of execution of the program, but remember that statements inside the function are not executed until the function is called.

A function call is like a detour in the flow of execution. Instead of going to the next statement, the flow jumps to the body of the function, executes all the statements there, and then comes back to pick up where it left off.

3.8 Parameters and arguments

Inside the function, the arguments are assigned to variables called parameters.

I must admit that the notions of parameters and arguments was very abstract.

I read it several times and I was not sure after each reading who was who. This was the perfect occasion to talk to a Computer Science (CS) professional!

If you do not know any CS professional, go to MeetUps. There are Python study groups that can be a valuable resource at this point.

  • PyLadies (is a great start)
  • Girl Develop It (They offer an Intro to Python course. Their approach is hands-on experience)
  • Python MeetUp Groups

Let us consider the following:

x = 3

My mathematical background made me read this as:

x equals to 3

So, when I saw this expression, I freaked out … literally!

x = x + 1

How can x be equal to x plus 1?!

***

After chatting with one CS professional, I understood that:

A variable is a “place holder”. Let us see how this changes our point of view:

x = 3

This statement means that variable x holds or gets the value of 3.

Knowing this, the expression: x = x + 1 is read as: variable x gets the value of x plus one (1).

Let us consider the example in Chapter Three (3):

In the Python shell, let us define a function named: ‘print_twice’

>>> def print_twice(bruce):
…     print bruce
…     print bruce

>>>

‘bruce’ is our parameter.

So, let us call the function ‘print_twice’ that we just  created in the Python shell with the argument “Laaa, lalalalala “:

>>> print_twice (“Laaa, lalalalala “)
Laaa, lalalalala
Laaa, lalalalala
>>>

I stumbled on the fact that the concepts of parameter and variable seemed identical to me. I understood why:

When we define our function, the most important is to establish what the function will do. You do not want to start thinking about what you can put in the function. At the creation stage, you just want your function to work in a particular way. In that case, you will use the concept of parameter.

Later, when you want to call the function and you know what you want to put in to make it work, you may use a variable or an argument.

Let us see how the function works with a variable named x :

Remember: Variable x gets the value of the following string “Hickory Dickory Dock”

>>> x = “Hickory Dickory Dock”
>>> print_twice (x)
Hickory Dickory Dock
Hickory Dickory Dock
>>>

So, we can make our function work with arguments and previously defined variables.

Recap: In a function (built-in or user-defined) the argument is evaluated before the function is called.

When we use built-in modules in our function definition, here is something to consider:

  • def module name . function name (parameters)

This first function definition provides the name of the module, the name of the function we want to use from the module and it states the parameter name.

Remember: A parameter is an information that I want to to consider but I still do not know the exact value.

  • def module name . function name (argument 1, argument 2, … argument n)

This second function definition provides the name of the module, the name of the function we want to use from the module and it states the value of the arguments. An argument is a precise piece of information.

3.9 Variables and parameters are local

When you create a variable inside a function, it is local, which means that it only exists inside the function

Using the user-defined function we created earlier ‘print_twice’, let us try this in the Python shell, in order to seize this concept.

1) Let us create a user-defined function called ‘cat_twice’
NOTE: My comments start with a # sign and are italicized.

>>> def cat_twice(part1, part2):
…     cat = part1 + part2                  #variable ‘cat’ gets the value of the concatenation of parameter no1 (called ‘part1’) and parameter no2 (called ‘part2’)
…     print_twice (cat)                      #we will call the function that we created earlier ‘print_twice’ with the variable ‘cat’

>>>

Moreover, let us the function we just created ‘cat_twice’ with a set of two (2) variables:

>>> line1 = “Hickory Dickory Dock”               #The variable ‘line1’ gets the value of “Hickory Dickory Dock”
>>> line2 = “The mouse ran up the clock.”    #The variable ‘line2’ gets the value of “The mouse ran up the clock”
>>> cat_twice (line1, line2)                            #We will call the function that we created ‘cat_twice’ with the variables ‘line1’ and ‘line2’
Hickory Dickory DockThe mouse ran up the clock.
Hickory Dickory DockThe mouse ran up the clock.
>>>                                                                  #The result is the concatenation of variables ‘line1’ and ‘line2’ and printed twice

Let us double-check what happens with the variable ‘cat’ that we initially defined in the function ‘cat_twice’

>>> print cat
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
NameError: name ‘cat’ is not defined
>>>

When cat_twice terminates, the variable cat is destroyed. If we try to print it, we get an exception:

NameError: name ‘cat’ is not defined

Furthermore, it is important to notice that the parameter ‘bruce’ is useless outside the function we defined called ‘print_twice’.

This concludes the second part of Chapter Three (3).

Rubik's Cube

Rubik’s Cube

 

 

***

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

One thought on “Chapter 3: Functions (Part II)

  1. Pingback: Chapter 7: Iteration – 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