<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""A demonstration of declaring a function with optional arguments"""

# We are setting the default value of c to None. Argument c will have this
# value if the user calls mathematize without providing a value for argument c.
def mathematize(a, b, c=None):
    """A toy function that performs a mathematical operation on two or three
    numbers together.
    
    If only a and b are provided, then returns a + b. If c is also provided,
    then returns 2 * a + 2 * b - c.
    
    Args:
        a: a number
        b: a number
        c: optional argument. If provided, a number.
    """
    if c is None:
        return a + b
    else:
        return 2 * a + 2 * b - c

# Do NOT use a mutable value as a default value.

# Python won't assign a new empty list each time. Instead, ell will be assigned
# to the same list every time. If you modify the list, the modifications will
# remain the next time you call the function.
#def create_or_append_wrong(ell=[]):
    #"""If argument ell is provided, then appends 'a' to ell and returns it.
    #If not, then returns a new list with value ['a'].
    
    #Args:
        #ell: a list
    #"""
    #ell.append('a')
    #return ell

# Instead, use None as the default value and assign the list within the
# function.

if __name__ == '__main__':
    mathematize(1, 2)
    mathematize(1, 2, 3)
    
    #print(create_or_append_wrong(['abc']))
    #print(create_or_append_wrong())
    #print(create_or_append_wrong())
</pre></body></html>