8.3. Function Parameters

8.3.1. Arguments vs Parameters

  • argument is the value/variable/reference being passed to the function

  • parameter is the receiving variable used within the function/block

8.3.2. Syntax

Listing 8.7. Function definition with parameters
def my_function(<parameters>):
    <do something>
def add(a, b):
    print(a + b)

8.3.3. Required Parameters

  • Parameters without default values are required

def add(a, b):
    print(a + b)


add()
# TypeError: add() missing 2 required positional arguments: 'a' and 'b'

add(1)
# TypeError: add() missing 1 required positional argument: 'b'

add(1, 2)
# 3

add(1, 2, 3)
# TypeError: add() takes 2 positional arguments but 3 were given

8.3.4. Optional Parameters

  • Optional parameters has default value

  • Function will use default value if not overwritten by user

  • Parameters with default values can be omitted while executing

def add(a=10, b=20):
    print(a + b)


add()
# 30

add(1)
# 21

add(1, 2)
# 3

add(1, 2, 3)
# TypeError: add() takes from 0 to 2 positional arguments but 3 were given

8.3.5. Required and Optional Parameters

  • Required parameters must be at the left side

  • Optional parameters must be at the right side

  • There cannot be required parameter after optional

def add(a, b=20):
    print(a + b)


add()
# TypeError: add() missing 1 required positional argument: 'a'

add(1)
# 21

add(1, 2)
# 3

add(1, 2, 3)
# TypeError: add() takes from 1 to 2 positional arguments but 3 were given
def add(a=1, b):
    print(a + b)

# SyntaxError: non-default argument follows default argument
def add(a, b=1, c):
    print(a + b + c)

# SyntaxError: non-default argument follows default argument

8.3.6. Examples

Listing 8.8. Example 1
def add(a, b):
    print(a + b)


add(1, 2)
# 3

add(1.5, 2.5)
# 4.0

add('a', 'b')
# 'ab'
Listing 8.9. Example 2
def echo(text):
    print(text)


echo('hello')
# hello
Listing 8.10. Example 3
def connect(username, password, host='127.0.0.1', port=22,
            ssl=True, keep_alive=1, persistent=False):

    print('Connecting...')
Listing 8.11. Example 4. Definition of pandas.read_csv() function. Source: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html
def read_csv(filepath_or_buffer, sep=', ', delimiter=None, header='infer',
             names=None, index_col=None, usecols=None, squeeze=False, prefix=None,
             mangle_dupe_cols=True, dtype=None, engine=None, converters=None,
             true_values=None, false_values=None, skipinitialspace=False,
             skiprows=None, nrows=None, na_values=None, keep_default_na=True,
             na_filter=True, verbose=False, skip_blank_lines=True, parse_dates=False,
             infer_datetime_format=False, keep_date_col=False, date_parser=None,
             dayfirst=False, iterator=False, chunksize=None, compression='infer',
             thousands=None, decimal=b'.', lineterminator=None, quotechar='"',
             quoting=0, escapechar=None, comment=None, encoding=None, dialect=None,
             tupleize_cols=None, error_bad_lines=True, warn_bad_lines=True,
             skipfooter=0, doublequote=True, delim_whitespace=False, low_memory=True,
             memory_map=False, float_precision=None):

    print('Reading CSV...')

8.3.7. Assignments

8.3.7.1. Function Parameters Example

English
  1. Define function add

  2. Function parameter is sequence of integers

  3. Print sum of all sequence values

Polish
  1. Zdefiniuj funkcję add

  2. Parametrem do funkcji ma być sekwencja liczb całkowitych

  3. Wypisz sumę wszystkich wartości sekwencji

Solution
def add(sequence):
    print(sum(sequence))

8.3.7.2. Function Parameters Echo

English
  1. Define function echo with two parameters

  2. Parameter a is required

  3. Parameter b is required

  4. Wypisz a i b

  5. Compare result with "Output" section (see below)

Polish
  1. Zdefiniuj funkcję echo z dwoma parametrami

  2. Parametr a jest wymagany

  3. Parametr b jest wymagany

  4. Wypisz a i b

  5. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Output
>>> echo(1, 2)
a=1 b=2

>>> echo(3, 4)
a=3 b=4

8.3.7.3. Function Parameters Default

English
  1. Define function default with two parameters

  2. Parameter a is required

  3. Parameter b is optional and has default value None

  4. If only one argument was passed, consider second equal to the first one

  5. Print a i b

  6. Compare result with "Output" section (see below)

Polish
  1. Zdefiniuj funkcję default z dwoma parametrami

  2. Parametr a jest wymagany

  3. Parametr b jest opcjonalny i ma domyślną wartość None

  4. Jeżeli tylko jeden argument był podany, przyjmij drugi równy pierwszemu

  5. Wypisz a i b

  6. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Output
>>> default(1)
a=1 b=1

>>> default(2, 3)
a=2 b=3