Programming V

Functions and Arguments

Randy J. Fortier
randy.fortier@uoit.ca
@randy_fortier

Outline

  • Functions
    • Purpose
    • Syntax
    • Documentation
    • Calling functions
    • Arguments
      • Pass by value

Programming V

Defining Functions

Modularity

  • So far, we've created only small programs
    • When programs get large, they become more complex to write, debug, modify, and understand
    • Modularity can make it easier to comprehend large programs
    • Modularity can also make it possible to reuse part of our program
  • Types of modularity:
    • Functions
    • Objects

Functions

  • A function is a module of program code
    • A function takes input (arguments)
      • The arguments allow us to customize the operation performed by the function
    • A function produces output (return value)
      • The return value is often the result of executing the code
    • Calling a function causes the code in a function to execute
      • If we call the function again, the code executes again

Defining Functions

  • Functions without arguments or return value:
  • 
    def sayHello():
    	print('Hello!')
                                
  • To call this function:
  • 
    sayHello()
                                

Defining Functions

  • Functions with a return value:
  • 
    def getAnswer():
       return 42
                                
  • To call this function:
  • 
    answer = getAnswer()
                                

Defining Functions

  • Functions with arguments:
  • 
    def getDogAge(hAge):
       return hAge * 7
                                
  • To call this function:
  • 
    answer1 = getDogAge(5)
    
    humanAge = 7
    answer2 = getDogAge(humanAge)
                                

Documenting Functions

  • To document a function, use a multi-line comment immediately after the def line:
    • Don't forget to indent the comment

def getAgeInDogYears(humanAge):
   """
   This function, given an age in human
   years, returns the age in dog years.
   """
   return humanAge * 7
                        

Local Variables

  • If you create any variables inside functions, they are local variables
    • A local variable is accessible/usable within that function only
    • The word local refers to the variable's scope
      • The scope is local to the function

def getAgeInDogYears(humanAge):
   dogYearsFactor = 7  # local variable
   return humanAge * dogYearsFactor
                        

Global Variables

  • Variables created outside of a function are called global variables
    • Global variables' scope includes both inside and outside of functions
    • We explicitly declare when we use global variables

dogYearsFactor = 7  # global variable
def getAgeInDogYears(humanAge):
   global dogYearsFactor
   return humanAge * dogYearsFactor
                        

Programming V

Calling Functions and Arguments

Arguments

  • Arguments are values passed into (and sometimes out of) functions
    • They are used to customize the behaviour of the function
    • Compare this Python function:
    • 
      def eightTimesTwo():
         return 8 * 2
                              
    • ... with this Python function:
    • 
      def timesTwo(num):
         return num * 2
                              
    • Which one can be used in more situations?

Arguments

  • At first, arguments can be confusing
    • Consider the following situation:
    • 
      def timesTwo(num):
         return num * 2
      
      value = 8
      print(timesTwo(value))
                              
    • The number 8 seems to have two different names: num and value

Arguments: Passing by Value

  • In Python, arguments are passed by value
    • By value means that the value passed to the function when it is called is copied, and the copy is put into the argument variable
    • An argument variable has the same scope as a local variable
  • Advantage:
    • When calling a function by passing its arguments from variables, you don't have to worry about those variables' values being modified

Arguments: Passing by Value

Arguments: Passing by Value

Arguments: Passing by Value

Arguments: Passing by Reference

  • Another way that arguments are passed into a function is by reference
    • By reference means that the value passed to the function is linked to the argument variable
    • Any change to the argument also changes the original value
    • Advantage:
      • When calling a function by passing its arguments by reference, you can have side effects elsewhere in the program
  • Note: In Python, passing by reference is not supported

Wrap-Up

  • Functions are a way to create reusable units of program code
  • Functions can be customized by passing arguments
    • Arguments can (in general) be passed by value or by reference
  • Functions may or may not return values