2. Functions

“functions are the lego-blocks that we use to build programs”

Theory

Custom functions

Custom functions are mini-programs inside our program that we can call ‘later’. We use them to split a problem in independent parts.

Karel ignores a function until we ‘call’ it. Karel recognizes the commands that are part of the function because they are all indented 4 spaces to the right.

def foo( ):       # define a function name 'foo'
    command_1     # start of the body of the function
    command_2     # in Python commands in the body are indented 4 spaces
    ....
    command_n     # end of the body of the function

foo()             # calling the function foo()

A function returns to its caller when it runs out of instructions.

The concept of a function is the single most important concept that you will learn in programming. Since this is important, we’ll spell it out:

  • have your functions do only one thing
  • keep your functions short
  • give your functions meaningful names
  • keep the length of all your functions about the same

Aliases

A function is an alias of another function if it is simply renaming it. For example,

def left():          # when left() is called ....
    turn_left()      # it, in turn, calls turn_left()

left() is an alias of turn_left() because all left() does is to call turn_left().

Training Missions

2.TM moving a token

First study the answer, then solve the problem on your own

Work it out

Let’s use functions to solve a scenario similar to the ones we solved before: have Karel take a yellow token, place it in the empty slot, and then take him to his home square.

Answer: Show it to me

sound( True )
think( 900 )

def left():
    turn_left()

def take_the_token():
    say( "taking the token" )
    move()
    take()

def put_the_token():
    say( "putting the token" )
    left()
    move()
    put()

def go_to_home_square():
    say( "going home" )
    move()

# main program
take_the_token()
put_the_token()
go_to_home_square()
say( "done" )

Before...

Before…

..and after.

..and after.

In-class Missions

2.1 Chicken run

Try to solve the problem w/o peeking at the answer

Let me do it


While Karel was in his garage, his chicken got out of its pen. Please, write the function

right()

that turns Karel to the right, and use it to take the chicken back to its pen, and to take Karel home. Have Karel face North so he can verify that the chicken is still in its pen.

Your main program must be:

# main program
take_chicken()
put_chicken()
go_home()



Answer: show it to me


Chicken spotted!

Chicken spotted!


Yeap.. the chicken is back in its pen.

Yeap.. the chicken is back in its pen.


2.2 A dozen chicks

Try to solve the problem w/o peeking at the answer

★★ Let me do it


Ahh!.. now the chicks got out of the pen. Quick.. return them to the pen and then take Karel home.

Your main program must be:

# main program
take_chicks()
put_chicks()
go_home()



Answer: show it to me


Ready to go home

Ready to go home


all done

all done


2.3 Life in SoCal – Touchdown

Try to solve the problem w/o peeking at the answer

★★★ Work it out


Karel needs to pick up the football and score a touchdown. Please help Karel.


By Torsten Bolten, AFpix.de (Own work) [CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons

By Torsten Bolten, AFpix.de (Own work) [CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons


Answer: Answer: Show it to me

Before: Ready!

Before: Ready!

After: Touchdown!

After: Touchdown!

Hint:

Use functions to split the mission in independent parts. The main function could be something like this:

# main program
go_to_ball()
pick_ball_up()
go_to_endzone()
score()

Optional Mission

2.4 Strawberries – Fertilizer tryout

Not-for-credit interesting problem

★★★★ Work it out

The strawberries deplete the nutrients from the soil at an alarming rate. Karel ordered 10 bags of fertilizer (the 10 yellow tokens) to try it out. Please, write the function turn_around() that turns Karel around:

turn_around

and use it to move the fertilizer to the shed (the cell with the 10 empty slots) and take Karel home.

fertilizer tryout

Answer: Show it to me


Before: Ready to move these bags.

Before: Ready to move these bags.


After: Uff.. that was difficult.

After: Uff.. that was difficult.


Hint:

Use functions to split the mission in independent parts. The main function could be something like this:

# main program
pick_up_bags()
put_bags_in_shed()
go_home()