8.1. Function Definition

8.1.1. Rationale

  • Reuse code

  • Improves code readability

  • Clean-up code

  • Allows for easier refactoring

8.1.2. Syntax

def <name>():
    <do something>

8.1.3. Definition

def say_hello():
    print('My name... José Jiménez')

say_hello()     # My name... José Jiménez
say_hello()     # My name... José Jiménez
say_hello()     # My name... José Jiménez

8.1.4. Convention

  • Do not use camelCase names

  • CamelCase is reserved for class names

  • Use snake_case names # Python - snake ;)

  • Add underscore (_) at the end of name when name collide

  • System functions names starts and ends with 'dunder' - double underscore: __

Listing 8.1. Do not use camelCase, CamelCase is reserved for class names. Use snake_case
def sayHello():
    print('This is camelCase() name')
    print('It is c/c++/Java/JavaScript convention')

def say_hello():
    print('This is snake_case() name')
    print('It is Pythonic way')
Listing 8.2. Use better names, rather than comments
def cal_var(data, m):
    # Calculate variance
    return sum((Xi-m) ** 2 for Xi in data) / len(data)

def calculate_variance(data, m):
    return sum((Xi-m) ** 2 for Xi in data) / len(data)
Listing 8.3. Add underscore (_) at the end of name when name collide. Although prefer naming it differently.
def print_(text):
    # Add underscore (``_``) at the end of name when name collide.
    print(f'<strong>{text}</strong>')

def print_html(text):
    # Although prefer naming it differently.
    print(f'<strong>{text}</strong>')
Listing 8.4. System functions names starts and ends with 'dunder' - double underscore: __
def __import__(module_name):
    ...

8.1.5. Docstring

  • Docstring is a first multiline comment in: File/Module, Class, Method/Function

  • Used for generating help() documentation

  • It is accessible in __doc__ property of an object

  • Used for doctest

  • PEP 257 Docstring convention - For multiline always use three double quote (""") characters

  • More information in Function Doctest

Listing 8.5. Docstring used for documentation
def say_hello():
    """This is the say_hello function"""
    print('Hello')


help(say_hello)
# Help on function say_hello in module __main__:
#
# say_hello()
#     This is the say_hello function

print(say_hello.__doc__)
# This is the say_hello function
Listing 8.6. Docstring used for documentation
def say_hello():
    """
    This is the say_hello function
    And the description is longer then one line
    """
    print('Hello')


help(say_hello)
# Help on function say_hello in module __main__:
#
# say_hello()
#     This is the say_hello function
#     And the description is longer then one line

print(say_hello.__doc__)
#    This is the say_hello function
#    And the description is longer then one line

8.1.6. Assignments

8.1.6.1. Function Definition Print

  • Assignment name: Function Definition Print

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 3 min

  • Solution: solution/function_definition_print.py

English
  1. Define function call without parameters

  2. Print Beetlejuice on the screen

  3. Call function three times

Polish
  1. Zdefiniuj funkcję call bez parametrów

  2. Wypisz Beetlejuice na ekranie

  3. Wywołaj funkcję trzy razy