Udacity CS 101: Procedures Part I

Imagine when every time after you flush the toilet, you have to manually fill the tank with water. Now that would suck. In Python, and almost all other languages, such processes can be automated. You can write a process that will refill the tank every time you flush. Every single time.

Toilet Aquarium

I wouldn’t even want to take a dump anymore
Image courtesy of http://www.wisebread.com

And it is the same idea for extracting links from a page. You do not only want the first link, you want all the links! There are two ways to retrieve all the links on a page:

The first way is to code it manually, search for the first occurrence of the <a href=" tag, retrieve the string until the closing ", set the new starting position to search. Rinse and repeat. The problem is, how many times will you have the code this? It depends on the number of links in a page right? And every page will probably have different number of links! Its a mess.

The second way is by something known as procedures or functions. A procedure basically automates the entire process. You can also think of a procedure as an oven


Hmmmmmmm.. I can almost taste the bread.
Image courtesy of sweetgrace.typepad.com

When you feed some ingredients into this oven, you get a cake or a bread, depending on the ingredients, as well as the degree of heat and time and other factors of the oven. This oven will help you extract the links from the page. It will look at the page, extract the first link, and it will automatically start searching for the next link and extract it. You would not have to code for every single link in the page. That’s the magic of the oven.

In python, the way to build an oven make a procedure is using the keyword def (short for define):

def myMagicOven(flour,water,egg):

<block># block of code stating how the oven will mix the ingredients and make a cake

# this will define a procedure named myMagicOven with the parameters flour, water and egg.

Remember during the lessons on strings you learned about parameters, which are basically inputs to your procedures. A procedure can have zero parameters or as many parameters as you would need, separated by a comma as seen above:

def myUselessOven(): # empty parentheses means that there are no parameters required

print "This oven does nothing, except print this useless statement"

Note that the block consists of instructions and expressions that you will code. Blocks are indented (four spaces by convention), and the indentations are important, because it will tell the Python interpreter where the block ends.

If you have baked before, I’m sure you know how difficult it is. First you’ll need the right ingredients, the correct type of flour, sugar, eggs; the correct quantity of each ingredient; the intense beating kneading to get it to mix; the fine control of temperature and heat. And finally you have a cake, or a loaf of bread, or cookies. It would be sad if the oven eats it all up and leaves you with nothing.

Python procedures don’t disappoint you, usually. You feed inputs into the procedures, and most of the time you can expect to get outputs from the procedures. These outputs are also known as returns, which I mentioned in Strings Part II.

def myMagicOven(flour,water,egg):

goo = flour + water

taste goo

dough = goo + egg

taste dough

cook dough

return cake

In this modified myMagicOven procedure, you can see that it returns a cake (I hope you like cakes), and in the midst of returning it actually tasted goo and dough. A procedure can return multiple expressions (separated by commas).

This Udacity post references Udacity CS 101 Unit 2 Chapters 1, 2, 3, 4, 5 and 6.

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