Functions

Intro to Code B Progress
34%

Defining and Calling Functions

Let’s remember functions from Reeborg. We would define functions and call them later in our code. The definition told the computer what a function should do. The calling told the function to actually execute the definition of the function at a certain point in the main code. Something like this:

#defining function, telling the computer that any time turn_right is called, Reeborg should turn left 3 times
def turn_right():
  for i in range(3):
    turn_left()

#calling function, telling Reeborg to turn right
turn_right() 

This concept of defining and subsequently calling functions holds in Reeborg AND programming in formal Python, so you should use this form in your test files for coding exercises.

Return Values

In Reeborg, our functions did not have return values, but many of the functions you’ll be writing from here on out will have return values. This means that the last line of your function should return a value, which should match the requirements of the exercise. For example, if the exercise asks for a function that returns a boolean, verify that your function returns a True value or a False value (or a variable that stores one of these two values). If it should return a number or a string or a list, make sure your return statement returns something of that form. To return something, use the keyword return followed by the name of the variable or the literal value returned.

Here are some examples:

#A function that returns a boolean. More specifically, it always returns True.
def function_1():
  return True
  
#A function that returns a variable called nothing_important, 
which stores the empty string
def function_2():
  nothing_important = ""
  return nothing_important
  
#A function that returns the same thing as function_2(),
but does so by returning the literal string value "" instead of a variable
def function_3():
  return ""

#A function that returns a number
def function_4():
  return 1

Testing Return Values / Function Behavior

Think of the function with the return value as a passenger on a boat, who throws you an item while you’re standing on the mainland, before they sail away forever. When the computer runs through the code inside a function, it “tosses” the main program the return value before exiting the function. However, similar to the boat situation, if there’s no one on the mainland to catch the return value, it simply disappears (aka the computer doesn’t save in its memory). You must use a variable to “catch” the return value in order to use the returned value outside the scope of the function.

For example, the following piece of code defines a function called function_4(), which returns 1. The number variable “catches” the return value from calling function_4() in the main program. Then, it prints the value now stored in number, which is 1.

#Incorrect way to test function_4
def function_4():
  return 1

function_4() #return value from function not assigned to a variable

#Correct way to test function_4, with output to console
def function_4():
  return 1

number = function_4() #return value from function assigned to variable
print(number)

#Another correct way to test function_4, with output to console
def function_4():
  return 1

print(function_4()) #return value printed without assigning to a variable

Passing Arguments

In Reeborg, every function ended with (). Though the parentheses after a function name were left empty in Reeborg, they are often used for passing arguments into a function.

Arguments are the values passed to the function at run-time (used in function calls). Parameters are accessible inside functions and accept arguments passed into the function. When an argument is passed into a function, the parameter defined to accept it stores the value of the argument inside the function scope. You can think of them as variables sent to your own function from the outside that are now accessible within your function.

Since functions can only access variables created inside its own scope or parameters passed into it from outside its own scope, we call both of these kinds of variables local variables. Variables declared in the main program are called global variables.

The number of parameters in the function definition should match the number of arguments passed in to the function; however, within the function, only the parameter names are recognized as local variables, regardless of what the global variable names of the arguments are in the main program. We’ll discuss this further in the next section on function scope.