Writing Python Functions, Part 2: Flow & Scope

Writing Python Functions, Part 2: Flow & Scope

The Python interpreter reads a program just like you are reading this page: one line at a time, from left to right and top to bottom. The interpreter executes operations and functions in the order that it encounters them. This is called control flow or the flow of execution.  Unlike the Python interpreter, when we read something and encounter a word we don’t understand, we don’t freak out and shut down(usually). We continue reading and either guess the definition of the word based on context or pause and consult a dictionary. The Python interpreter isn’t so flexible. You can’t call a function before defining it. The interpreter needs to know what the function is and does before it encounters that function.

Writing Python Functions: Flow

For example, the following program, too_soon.py:

…will return this error:

 

When we call a function, the function reads our arguments, one at a time, from left to right. It then processes the argument values and when it’s finished it returns a value. Try the following, in a program called flow.py:

In the example above, even though we passed in the variable ‘left’ first, our function printed ‘right’ first because that is the order in which we called it.

Writing Python Functions: Scope

Imagine your Python program is the world(the Hello World), and your function is your group of friends. One of the conventions of programming is to declare variables at the top of your program. These variables are global. Any variables declared within a function are local. Global variables are accessible inside or outside a function, but local variables are only accessible inside the function where they are declared. Global variables are like the news or common knowledge: everyone knows the name of the president of the United States. Local variables are like secrets and inside-jokes: only you and your friends understand them; the rest of the world has no idea what you’re talking about. This is called scope. A variable that is accessible by any function in the program has global scope. A variable that is only accessible inside a function has local scope. Let’s write a program, invitation.py:

This will return the following error:

Change line 10 to something amusing, and run the program again:

When I call the party() function, my friends know that it’s both August 4th and my birthday. Calling the global variable outside the party function returns the date, August 4th, because everyone has access to the date. But calling the local variable outside the party() function returns an error because no one outside the party() function knows it’s my birthday.

Speaking of party, there’s cake in the next chapter.

Writing Python Functions, Part 2: Flow & Scope was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion
  • Manoj

    Thanks a lot, enjoying your tutorials, looking forward for some new once :)

  • Manoj

    Thanks a lot, enjoying your tutorials, they are a lot of fun and easy to understand. Looking forward for some new once :)

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid
×