Comp 112

Lecture 2

Calling and Defining Functions

2018.02.06

Calling Functions

Calling a function:

So functions generalize both statements and expressions.

From now on “statement” means “statement or function call” and “expression” means “expression or function call”.

We already met two functions last week:

Input and Output Functions

Composing Functions

Coercion Functions

Python includes some basic coercion functions that convert values from one type to another.

These are named after the target type:

str (<expression>)      #  tries to convert the argument to a string

int (<expression>)      #  tries to convert the argument to an integer

float (<expression>)     #  tries to convert the argument to a float

bool (<expression>)     #  tries to convert the argument to a boolean

Implicit Coercion

What will the following do?

Defining Functions

def my_fun (x , y) :                                                   #  the function header
    print ("i'll add " + str (x) + ' and ' + str (y) + ' for you...')  #  a statement in the function body
    return x + y                                                       #  a return statement in the function body

Function definitions have two parts:

A header consisting of:

A body consisting of:

Local Variables

Global Variables

Return Statements

None and NoneType

Returning vs Printing

It is important to understand the difference between returning and printing a value.

A value that is returned from a function is available for further use within the program.

It can be assigned to a variable, used in an expression, or passed as an argument to another function.

But a value that is printed to the screen is lost to the program: it can no longer be examined or manipulated.

In fact, print is a function that always returns None:

Compare:

Functions and Effects

Function Call Execution

A function call creates a detour in the flow of program execution:

  1. The function arguments are evaluated.
  2. A new variable table is created, assigning the argument values to the function parameters.
  3. Control is passed to beginning of the function body.
  4. Control flows normally within the function body until a return statement is reached.†
  5. The expression following the keyword return is evaluated.‡
  6. The function’s variable table is abandoned.
  7. The return value is assigned to be the value of the function call.
  8. Control is returned to the function call site.

†) If no return is encountered, then “return None” is implied.

‡) If no expression follows return, then “return None” is implied.

Call Graphs

def add_and_greet (x , y) :
    it  =  x + y
    greet_user ()
    return it

def greet_user () :
    it  =  print ('hi there!')

x  =  39
y  =  add_and_greet (1 + 2 , x)

Lets see what this program does by drawing its call graph.

Function Specifications

Dynamic languages like Python don’t keep track of these things, but you should using documentation string comments:

To Do This Week: