Functions


        
    
        

Create Function

We create a function with the keyword def.

Syntax:

def function_name (parameters): #line of codes to be executed

Example:

total=10 def store(a): total+=a goods=10 store(goods)

The function above will not return any value, but it will change the variable total.

Example:

def store (a): total = 10 total +=a return total goods=10 result = store(goods)

Notice that the function above has the return statement. The return statement is used to return value to other functions. And, because the function has a return value, we need to prepare a variable to store the return value (in this case, the variable name is result).


Calling Function

Declared functions are not executed when we don't call them. So, to execute them, we need to call them. To call a function, we merely need to write the function name with parentheses. If the function needs parameters, we need to also include parameters in the parentheses.

Syntax: function_name (parameters)

Example:

total=10 def store(a): total+=a goods=10 store(goods) #calling the function

Arguments

Information can be passed to functions as a parameter. Parameters are variables inside a function that are useful for operations inside the function. There is also another type of parameter called arguments. The argument is a parameter that has been passed to a function, or we can say that the argument is the input from other functions to a particular function.

Example:

def displayInfo(age, phone): print(f"This is your age, {age}") print(f"This is your phone number, {phone}") displayInfo(10, 9912);

In this example, 10 and 9912 are the arguments, while age and phone are the parameters.


        

We can also set a default parameter value for a function. So, if you call a function but do not provide an argument to it, it will automatically use the default value. A parameter with a default value is called an optional parameter.

Example:

def display (age=20): print(age) display(10) #the output will be 10 display() #the output will be 20

Sometimes when we make a program, we do not know how many arguments that will be passed into a function. So, we can add arbitrary arguments (*args). We just need to add asterisk symbol in front of the parameter's name.

Syntax: *parameter_name

Example:

def display (*toys): for toy in toys: print(toy) display("car", "plane", "doll")

In Python, we can also pass a parameter using keyword arguments. So, the order of the arguments passed does not matter.

Example:

def displayInfo(age, phone): print(f"This is your age, {age}") print(f"This is your phone number, {phone}") displayInfo(phone = 9912, age = 10)

If you do not know how many keyword arguments that will be passed into your function, just use the arbitrary keyword arguments (**kwargs). This way the function will receive a dictionary of arguments and access them.

Syntax: **parameter_name

Example:

def display (**name): for key, value in name.items(): print("%s = %s" % (key, value)) display(fname="Jane", lname="Houston") #the output will be: # fname = Jane # lname = Houston

Lambda

Lambda is an expression that allows us to define function objects which can be used inline.

Example:

lambda arguments : expression

Example:

x = lambda a : a + 10 print(x(5)) #the output will be 15