Day 61: Functions Overview

# Functions

# declaring a Function
# if there is no return in the Functions
# python will return the last command line

def func(para1, para2):
    ''' explain what happens here '''
    sum  = para1 + para2                # variables declared inside functions are local
    return sum

#calling a function
sum = func(13,5)               # 18 this sum is not related to the sum-variable in the function
print(sum)


#using default parameters
def def_func(x, y=3):          # y=3 is a default parameter
    return x + y

print(def_func(2))             # 5 - the function will use the default parameter

print(def_func(5,8))           # 13 - default parameters can be overwritten


# Examples:

def is_odd(num):
    ''' returns true if number is odd '''
    if (num % 2 == 0):
        return True
    else:
        return False

print(is_odd(4))                                    # True


def is_prime(x):
    ''' checks if number is prime '''
    ''' only natural number bigger 1 can be considered primes '''
    if (x > 1):
        return all(x % i for i in range(2, x))
    else:
        return False                                # if number is 1 or less

print(is_prime(3))                                  # True

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.