Functions

Calling a function


In [1]:
x=5
y=7
z=max(x,y) #max is the function. x and y are the arguments
print(z) #print is the function. z is the argument


7

Installing libraries and importing functions


In [ ]:
!pip install easygui 
#pip: python installer program
# ! run the program from the shell (not from python)
# easygui: a python library for GUI widgets

In [2]:
import easygui #Imports easygui into the current namespace. We now have access to functiona and objects in this library
easygui.msgbox("To be or not to be","What Hamlet elocuted") #msgbox is a function in easygui.


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-3198e7a23cba> in <module>()
----> 1 import easygui #Imports easygui into the current namespace. We now have access to functiona and objects in this library
      2 easygui.msgbox("To be or not to be","What Hamlet elocuted") #msgbox is a function in easygui.

ImportError: No module named 'easygui'

Importing functions


In [3]:
import math #imports the math namespace into our program namespace
math.sqrt(34.23) #Functions in the math namespace have to be disambiguated


Out[3]:
5.850640990524029

In [4]:
import math as m #imports the math namespace into our program namespace but gives it the name 'm'
m.sqrt(34.23) #Functions in the math namespace have to be disambiguated using the name 'm' rather than 'math'


Out[4]:
5.850640990524029

In [ ]:


In [5]:
from math import sqrt #imports the sqrt function into our program namespace. No other math functions are accessible
sqrt(34.23) #No disambiguation necessary


Out[5]:
5.850640990524029

Returning values from a function

The return statement tells a function what to return to the calling program


In [6]:
def spam(x,y,k):
    if x>y:
        z=x
    else:
        z=y
    p = z/k
    return p #Only the value of p is returned by the function

In [7]:
spam(6,4,2)


Out[7]:
3.0

If no return statement, python returns None


In [8]:
def eggs(x,y):
    z = x/y

print(eggs(4,2))


None

Returning multiple values


In [9]:
def foo(x,y,z):
    if z=="DESCENDING":
        return max(x,y),min(x,y),z
    if z=="ASCENDING":
        return min(x,y),max(x,y),z
    else:
        return x,y,z

In [10]:
a,b,c = foo(4,2,"ASCENDING")
print(a,b,c)


2 4 ASCENDING

Python unpacks the returned value into each of a,b, and c. If there is only one identifier on the LHS, it won't unpack


In [11]:
a = foo(4,2,"ASCENDING")
print(a)


(2, 4, 'ASCENDING')

If there is a mismatch between the number of identifiers on the LHS and the number of values returned, you'll get an error


In [12]:
a,b = foo(4,2,"DESCENDING")


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-4bbd671daaf6> in <module>()
----> 1 a,b = foo(4,2,"DESCENDING")

ValueError: too many values to unpack (expected 2)

Value assignment to arguments

  • Left to right
  • Unless explicitly assigned to the argument identifiers in the function definition
  • 
    
    In [ ]:
    def bar(x,y):
        return x/y
    bar(4,2) #x takes the value 4 and y takes the value 2
    
    
    
    In [13]:
    def bar(x,y):
        return x/y
    bar(y=4,x=2) #x takes the value 2 and y takes the value 4 (Explicit assignment)
    
    
    
    
    Out[13]:
    0.5

    A function can have function arguments

    
    
    In [14]:
    def order_by(a,b,order_function):
        return order_function(a,b)
    
    print(order_by(4,2,min))
    print(order_by(4,2,max))
    
    
    
    
    2
    4
    
    
    
    In [19]:
    def change(x):
        x = (1,)
        print(x)
    x = (1, 2)
    change(x)
    print(x)
    
    
    
    
    (1,)
    (1, 2)
    
    
    
    In [26]:
    def replace(test_string, replace_string):
        start_index = test_string.find(replace_string)
        result = ""
        x = "bodega"
        if start_index >= 0:
            result = test_string[start_index:start_index+len(replace_string)]
            result = test_string.replace(result,x)
        return result
        
    print(replace("Hi how are you?", "yu"))
    
    
    
    
    
    
    
    
    In [ ]: