Functions in Python

What is a Function

Why Use Functions

Syntax

def function():
    #Here goes everything(block of code)
def say_hi():
    print('Hi')
    #This function will say hi
say_hi() #invoke the function

Return values from functions

def square_of_two():
    return 2**2

Parameters in the Functions

def square(num):
    return num**2
# These parameters only exists in the functions
def add(a,b):
    return a+b    
#Parameters should be named in a better way that makes sense

Parameters VS Arguements

def multiply(a,b):
    return a * b
print(multiply(1,5))
# a & b are parameters
# 1 & 5 are arguements
# Order matters

Common Mistakes Made while returning in a Function

def sum_odd_num(numbers):
    total = 0
    for x in numbers:
        if x%2 ==1:
        `   total +=x
        return total

print(sum_odd_numbers([1,3,5]))        
def sum_odd_num(numbers):
    total = 0
    for x in numbers:
        if x%2 ==1:
        total +=x
    return total

print(sum_odd_numbers([1,3,5]))  

Default Parameters

def square(num,power=2):
    return num **power
#Even if we dont pass a value for the power it will retain its default value 2

Passing Functions inside of function

def add(a,b):
    return a+b
#The math function takes in add function as a parameter also
def math(a,b,fn=add):
    return add(a,b)

Keyword Arguements

def exponent(num,pow):
    return num **pow;
print(exponent(pow=2,num=3)) #This will return 9 

Scopes

x = 2
def add_two(num):
    return x+num
#In this example the x variable is available outside the function

def add_three(num):
    y = 3
    return num+y
#In this example the y is just the part of the function and it cannot be accessed from outside    
total = 0
def increment():
    global total
    total +=1
    return total
print(increment())

Documenting Functions Better

"""
This will allow us to document the function better
# These are called the docstrings in python
__doc__ they are used to document the functions and it helps us learn better.

print.__doc__
# All built in functions have a docstring

def add(x,y):
    """Add function will take two parameters x and y. Then adds them"""
    return x + y
print(add.__doc__)
"""

** and * Operators in the Functions

# Lets say we want to add 5 numbers we will write a function and pass 5 parameters
def add(num1,num2,num3,num4,num5):
    return num1+num2+num3+num4+num5
print(add(1,2,3,4,5))

#But what if we want to add 3 number with the same function then we have pass default values and it gets messy and takes a long time so therefore we use *args for function.

def addition(*args):
    total = 0
    for num in args:
        total+=num
    return total
#This is better function now
#args parameter will say the input in the tuple
# **kwargs parameter this will save the arguements passed into a dictionary 
# **kwargs is standard keyword used in the community
def color(**kwargs):
    for person,color in kwargs.item():
        print(f"{person}'s favorite color is {color}")

Parameter Odering

1) Parameters 2) args 3) Default Parameters 4) kwargs

def odering(a,b,**args,name='john',**kwargs):
    return [a,b,**args,name='john',**kwargs]

List Unpacking

def addition(*args):
    total = 0
    for num in args:
        total+=num
    return total
x = [1,2,3,4]
# Now we will pass the list inside of the function

print(addition(*x))