2. Functions, Conditions And Loops

Hello again! Fancy seeing you here. In this lesson, you’ll learn about some basic concepts used in programming: statements, functions, conditions and loops. Before we apply these concepts with a real programming language, let’s role play: you’re a computer and we are the programmer.

State Your Intentions

In the first lesson, we met the algorithm (the finite list of well-defined instructions). These individual instructions can also be called statements. Predictably, statements are orders you give to a computer. Remember Walk to the coffee machine from your first lesson? That was a statement!

Bear in mind that computers don’t think! It’s your job to describe each and every task you want the computer to perform. Computers never make mistakes. Unexpected behaviors, errors or bugs cannot and should not be blamed on the computer. Ever. They’re caused by ambiguous or erroneous statements (faulty logic!) issued by the programmer.

All right, so let’s pretend that you’re a computer (One that can walk. You’re a robot. Awesome!). We’ve just asked you to go to the coffee machine. Would you walk? Or would you run? Us humans can interpret the context of a situation. So as a human being, you would base your decision on things like the tone of our voice. We are pretty nice, and we didn’t scream at you, so we’d expect you to calmly walk to the coffee machine, not run toward it in a state of panic.

A computer, though, is incapable of making these sort of distinctions. That is why we need to add some precision to our command (or our statement, to use the correct term) by replacing go with walk. Then, as a nice computer-robot, we can be sure you’d walk and not run.

Make Coffee, Again

Consider the following list of statements (an algorithm):

  • Stand up
  • Walk to the machine
  • Press the Coffee button
  • Wait for the machine to fill the paper cup
  • Take the cup
  • Deliver it to Bob

When you’re done, you return to your desk and sit down. As soon as you’re comfortably seated, your colleague, Linda, politely asks for a cup of tea. You’re in luck. We’ve written a program that you, dear computer robot, will run:

  • Stand up
  • Walk to the machine
  • Press the Tea button
  • Wait for the machine to fill the paper cup
  • Take the cup
  • Deliver it to Linda

After executing this program, you return to your desk, sit on your chair, and lo and behold, another person wants a drink. Your team-mate, Jim, asks you for a cup of coffee. What do you do?

  • Stand up
  • Walk to the machine
  • Press the Coffee button
  • Wait for the machine to fill the paper cup
  • Take the cup
  • Deliver it to Jim

Let’s pause here for a second. You’re probably thinking: do we, as a programmer, really need to repeat myself each and every time someone wants a drink? The answer is Yes. Why? Because you’re a computer and, as we’ve already mentioned, we have to issue very explicit and detailed statements, otherwise you won’t really know what to do.

But here’s another question. Do we, the computer programmer, really need to type the complete list of statements each time someone asks for a coffee or a tea? No! we could write it once and ask you to refer to the complete list of statements when you need to make some coffee or tea. In computer programming this is what we call a function.

We create a function (a list of statements) on a sheet of paper and name it MakeCoffee. It contains the following statements:

  • Press the Coffee button
  • Wait for the machine to fill the paper cup
  • Take the cup

If Julia, another thirsty colleague, asks for coffee, we can just write this program that you will then run:

  • Stand up
  • Walk to the machine
  • Execute the statements in the function named MakeCoffee
  • Deliver the result of MakeCoffee (the cup, that is) to Julia

Now, we don’t need to repeat each and every step of the coffee-making process. we can just refer you to the function MakeCoffee. We say that we call the MakeCoffee function. Usually, calling a function gives you a result. The result of a function is called a return value. In this example, the return value is the cup of coffee and we asked you to bring this return value to Julia.

What About Tea?

Looking at the previous requests from our thirsty workmates (how thirsty can you actually be?), Linda wanted tea, not coffee. Obviously, you can’t use the MakeCoffee function. Linda would get upset and tell you, rightly, that she’s in the mood for tea, not coffee. we could easily write a MakeTea function, though. Instead, let’s use a more flexible method: let’s modify the MakeCoffee function in a way that lets us choose Coffee or Tea. Cool?

First, let’s rename our MakeCoffee function to MakeDrink, to make it more general and not make tea feel left out. Then, let’s change this function so that it accepts an argument. Arguments are values you want a function to do something with. We say that we pass an argument (or several) to a function. In this case, we could imagine a DrinkName argument and have our function check whether this DrinkName is Coffee or Tea like this:

MakeDrink(DrinkName)   if DrinkName is Coffee then     press the 'Coffee' button    if DrinkName is Tea then     press the 'Tea' button    Wait for the machine to fill the paper cup   Take the cup 

Now we have a flexible MakeDrink function that takes one argument named DrinkName. Linda asked for tea, right? we would write a program that calls this new MakeDrink function, passing in Tea as the value for the DrinkName argument:

  • Stand up
  • Walk to the machine
  • Execute MakeDrink(Tea)
  • Deliver the result of MakeDrink to Linda

Pay attention to the if word in our MakeDrink function. This is called a conditional statement and is one of the building blocks of any programming language. Conditional statements allow us to tell the computer to perform an action only if certain conditions are true.

In this drink making example, we told the computer (that’s still you) to make some tea only if the desired DrinkName (the argument) was indeed Tea. In English, you’d have said “Make some tea“: make is the function and tea is the value for the argument.

Signatures & Bodies

The line MakeDrink(DrinkName) is called the function signature. It specifies the function name and the list of arguments that the function expects (here, we have only one: DrinkName but we could have imagined a second one such as Sugar, for example). If a function expects arguments, when you call the function, you must give a value for these arguments. If we call the MakeDrink function without telling which DrinkName we want, the program would simply fail because the computer cannot choose for you. We need to remember to be explicit!

Some programming languages require that you also specify what type of return value a function will output. If this was the case in our example, we would write the first line as MakeDrink(DrinkName) returns a filled cup:

MakeDrink(DrinkName) returns a filled cup   if DrinkName is Coffee then     press the 'Coffee' button    if DrinkName is Tea then     press the 'Tea' button    Wait for the machine to fill the paper cup   Take the cup 

The statements that the function will execute form a group called the function body. Going forward we will often use these words, function signature and function body, so it is important that you learn the correct terminology. Let’s consolidate:

  • Function signature: The name of the function, the arguments (if any) you should provide a value for when calling the function, the value the function is expected to return
  • Function body: The list of statements that will be executed when you call the function.

Repeat, Repeat, Repeat, …

Computer (You, yes, you!), make 3 coffees!

  • Stand up
  • Walk to the machine
  • Execute MakeDrink(Coffee)
  • Execute MakeDrink(Coffee)
  • Execute MakeDrink(Coffee)
  • Deliver the cups

What if we asked you for 10 coffees? Would we really have to repeat the call to the MakeDrink(Coffee) function 10 times? How would you say that in English? That’s right: make 10 coffees.

This is what we call a loop. Looping is a programming construct that lets us repeat a statement a number of times. The previous algorithm could be issued like this:

  • Stand up
  • Walk to the machine
  • Execute MakeDrink(Coffee) 3 times
  • Bring the cups

Summary

You should give yourself a mahoosive pat on the back. You’re well on your way to really nailing down the basics of computer programming.

We’ve met some cool and interesting concepts in this lesson. Let’s recap them quickly:

  • A statement is an instruction you give the computer
  • Your statements need to be precise to avoid unwanted behaviors
  • A function groups a list of statements
  • A function is composed of both a signature and a body
  • A function can receive one or several arguments
  • Arguments are values you want a function to manipulate
  • To execute the list of statements grouped in the function body, you call the function
  • If a function requires arguments, you need to pass it the appropriate values (such as the value Tea for the argument DrinkName of the function MakeDrink)
  • As a result of calling it, a function returns a value (the filled cup is returned by the function MakeDrink)
  • You can use conditions to execute certain statements (if this is true, then do that)
  • You can repeat a statement any number of times by using a loop

Important Words

  • Statement
  • Function
  • Return value
  • Argument
  • Conditional statement
  • Loop
  • Function signature
  • Function body

Exercise

For the next few days, pick some daily routines (taking a shower, preparing your lunch, answering the phone, etc). In your head, try to deconstruct what you do into precise and finite statements. For repeated actions, try to come up with a function. Pay attention to all of the conditional logic your brain has to process during one day. For example, if I wear red pants, I wouldn’t select a green shirt to go with them. This fairly simple exercise will start conditioning your brain to see the world the way programmers do. Want to share your statements and functions? Please do! It’ll help and inspire others. Share your coded daily routines in the Forums!

Share to friends