Functions#

  • defining a function

    • def

    • return

    • default values

    • keyword vs. positional arguments

  • executing a function

    • parameters

    • separate namespace

Vocab#

  • Define/create/make a function: To set up and write the instructions for a function.

  • Execute/call/use a function: To actually use the pre-defined function.

  • Input/parameter/argument: A variable defined by the user that is put/passed in between the parantheses () that comes after the function name.

  • Output: The variable that is returned to the user after the function is executed.

Functions#

A function is a re-usable piece of code that performs operations on a specified set of variables, and returns the result.

Modular Programming#

Modular programming is an approach to programming that focuses on building programs from independent modules ('pieces').

Copy + Pasting the same/similar bit of code is to be avoided.

Functions are one way to avoid this.

Loops are another! (we’ll get to these soon…)

Functions for Modular Programming#

  • Functions allow us to flexibly re-use pieces of code

  • Each function is independent of every other function, and other pieces of code

  • Functions are the building blocks of programs, and can be flexibly combined and executed in specified orders

    • This allows us to build up arbitrarily complex, well-organized programs

# you've seen functions before
# here we use the type() function
my_var = 3
type(my_var)
# the function print() doesn't depend on type()
# but they can both be used on the same variable
print(my_var)

Function Example I#

When you use def, you are defining a function. You are writing the recipe for the operation you want to do.

# define a function: double_value
# Notice that the parameter `num` is not explicitly defined with an = 
# This is because it will be defined later by the user when they execute the function.
def double_value(num):

    # do some operation
    doubled = num + num
    
    # return output from function
    return doubled    
# excecute a function by calling the function's name
# and defining the parameter within the parentheses
double_value(num = 2) 
# equivalent function call
# Here the parameter `num` is defined without 
# explicitly specifying the name of the parameter
double_value(2) 
my_num = 10
double_value(my_num)

Function Example II#

Here we are defining a function with multiple parameters

def add_two_numbers(num1, num2):
    
    # Do some operations with the parameters
    answer = num1 + num2
    
    # Return the variable answer
    return answer
add_two_numbers(5, 14)
# Execute our function again on some other inputs to double check 
# that our function is working how we think it should
output = add_two_numbers(-1, 4)
print(output)

Function Properties#

  • Functions are defined using def followed by the name of the function, parentheses (), parameters within the parentheses, and then : after the parentheses, which opens a code-block that comprises the function

    • Running code with a def block defines the function (but does not execute it)

  • Functions are executed with the name of the function and parentheses - () without def and :

    • This is when the code inside a function is actually run

  • Inside a function, there is code that performs operations on the available variables

  • Functions use the special operator return to exit the function, passing out any specified variables

  • When you use a function, you can assign the output (whatever is returned) to a variable

Class Question #1#

Given the function defined below, what will the second code cell below return?

  • A) 0

  • B) 2

  • C) 4

  • D) ‘2r.2 + 1’

  • E) ¯\_(ツ)_/¯

def remainder(number, divider):
    
    r = number % divider
    
    return r
ans_1 = remainder(12, 5)
ans_2 = remainder(2, 2)
 
print(ans_1 + ans_2)

Class Question #2#

Write a function greet that takes the parameter name. Inside the function, concatenate ‘Hello’, the person’s name, and ‘Good morning!”. Assign this to output and return output.

  • A) I did it!

  • B) I think I did it.

  • C) I tried but I am stuck.

  • D) Super duper lost.

#YOUR CODE HERE
greet('Eric')

Default Values#

Function parameters can also take default values. This makes some parameters optional, as they take a default value if not otherwise specified.

Default Value Functions#

Specify a default value in a function by doing an assignment within the function definition.

# Create a function, that has a default values for a parameter
def exponentiate(number, exponent=2):  
    return number ** exponent
# Use the function, using default value
exponentiate(3)
# Call the function, over-riding default value with something else
# python assumes values are in order of parameters specified in definition
exponentiate(2, 3)
# you can always state this explicitly
exponentiate(exponent=3, number=2)

Positional vs. Keyword Arguments#

Arguments to a function can be indicated by either position or keyword.
# Positional arguments use the position to infer which argument each value relates to
exponentiate(2, 3)
exponentiate(3, 2)
# Keyword arguments are explicitly named as to which argument each value relates to
exponentiate(number=2, exponent=3)
exponentiate(exponent=3, number=2)
# Note: once you have a keyword argument, you can't have other positional arguments afterwards
# this cell will produce an error
exponentiate(2, exponent=3)

Reminder, setting a default value for parameters is only allowed during function definition.

(This may look like what we did above with the keyword arguments, but here we are including a default value for one parameter during function definition. During function execution, you can’t mix and match using positional vs. keywords)

def exponentiate(number, exponent=2):     
    return number ** exponent

Class Question #3#

What will the following code snippet print?

def exponentiate(number, exponent=2): 
    function_output = number ** exponent
    return function_output

exponentiate(exponent=3, number=2)
  • A) 8

  • B) 9

  • C) SyntaxError

  • D) None

Note: when using Keyword arguments position/order no longer matters

Function Namespace#

  • Each function has its own namespace

  • Functions only have access to:

    • variables explicitly passed into them

    • variables defined inside the function

# You can check variables defined in the global namespace with `%whos`
%whos

Variables defined inside a function only exist within that function.#

# Names used inside a function are independent of those used outside
# variables defined outside of functions are global variables
# global variables are always available
my_var = 'I am a variable'

print(my_var)
# define a function that uses my_var inside the function
def concat_self(my_var):
    
    my_var = my_var + ' ' + my_var 
    
    return my_var

print(concat_self(my_var))
# see that my_var in global name space remains unchanged
print(my_var)
# only way to change my_var in global namespace
# is to assign to variable my_var in global namespace
my_var = concat_self(my_var)
print(my_var) 

Code Style: Functions#

  • Function names should be snake_case

  • Function names should describe task accomplished by function

  • Separate out logical sections within a function with new lines

  • Arguments should be separated by a comma and a space

  • Default values do NOT need a space around the =

Functions: Good Code Style#

def remainder(number, divider=2):
    
    r = number % divider
    
    return r

Functions: Code Style to Avoid#

# could be improved by adding empty lines to separate out logical chunks
def remainder(number,divider=2): # needs space after comma
    r=number%divider # needs spacing around operators
    return r

Summary#

  • how to define a function

  • how to execute a function

  • default values

  • position vs. keyword arguments

  • global namespace vs. local namespace

  • code style