LU09.L01 - Einfache Funktionenen

Aufgabe 1

main.py
def function1():
    """
    Function without params or return
    :return: None
    """
    print('Function 1 is called')
 
 
def function2():
    """
    Function without params, but returns a value
    :return: str
    """
    return 'Function 2 is called'
 
 
def function3(argument):
    """
    Function with a param and no return
    :param argument: str
    :return: None
    """
    print(f'Function 3 is called with argument: {argument}')
 
 
def function4(argument):
    """
    Function with a param and a return
    :param argument: str
    :return: str
    """
    return f'Function 4 is called with argument: {argument}'
 
 
def four_functions():
    """
    Main function
    :return: None
    """
    function1()
    received_from_2 = function2()
    print(received_from_2)
    function3('passed Argument to print in function3')
    received_from_4 = function4('passed Argument to print in function4')
    print(received_from_4)
 
 
 
if __name__ == '__main__':
    four_functions()

Aufgabe 2

calculator.py
def add(x, y):
    """
    Add two numbers.
    :param x: Number
    :param y: Number
    :return: Sum of x and y
    """
    return x + y
 
def subtract(x, y):
    """
    Subtract y from x.
    :param x: Number
    :param y: Number
    :return: Difference of x and y
    """
    return x - y
 
def multiply(x, y):
    """
    Multiply x by y.
    :param x: Number
    :param y: Number
    :return: Product of x and y
    """
    return x * y
 
def divide(x, y):
    """
    Divide x by y.
    :param x: Number
    :param y: Number
    :return: Quotient of x and y
    """
    return x / y if y != 0 else 'Division by zero'
 
def power(x, y):
    """
    Calculate x raised to the power of y.
    :param x: Base number
    :param y: Exponent
    :return: x raised to the power of y
    """
    return x ** y
 
def root(x, y):
    """
    Calculate the y-th root of x.
    :param x: Number
    :param y: Root
    :return: y-th root of x
    """
    return x ** (1 / y) if y != 0 else 'Root by zero'
 
def main():
    print(add(5, 5.5))
    print(subtract(5, 3))
    print(multiply(5, 3))
    print(divide(6, 3))
    print(divide(5, 0))
    print(power(2, 3))
    print(root(27, 3))
    print(root(16, 0))
 
 
if __name__ == '__main__':
    main()