Python: Functions

Functions are a way to achieve modularity and reusability in code. Before moving forward we need to know what are these:

Modularity: Modular programming is the process of subdividing a computer program into separate sub-programs. A module can often be used in various applications and functions with other components of the system.

Reusability: Using already developed code according to our requirements without writing from scratch.

In Python, we define a function with a keyword def then the function name after the name of the function we supply a pair of parentheses and a colon sign ():

def add():
number1 = int(input('Enter a value'))
number2 = int(input('Enter another value'))
print(number1 + number2)

Note that every statement which is part of the function body is a level indented more than the definition of the function.

We have declared a function named add now we can call it as many times and in any module as we want. To call the function we just need to write the name of function followed by pair of parentheses.

add()

Passing information positional arguments

A generic function does not define any data it processes inside it hard-coded instead it accepts the data when it is called and processes that data.

def add(number1, number2):
       print (number1 + number2)

Now to call the function we need to pass two arguments and they are matched according to their position in the function call. For example: here, value 3 will be assigned to number1 while value 5 will be assigned to number2 variable.

add(3, 5)

Passing information keyword arguments

def add(number1, number2):
       print (number1 + number2)

There is another way to call the same function that we pass the arguments with the variable name, this way position does not matter but the value is assigned to the matching name variable in function parameters.

add(number2 = 5, number1 = 3)

Default value parameters

def add(number1 = 0, number2 = 0):
     print(number1 + number2)

There are times when some parameter values are optional but still, you need a default value in case someone does not provide the value to avoid any non-deterministic behaviours.

add(number2 = 5)

Dealing with an unknown number of arguments

In some cases, we can not guess how many arguments the user would pass when calling the function so we need a parameter that can take all values provided by the user.

def display_nums(first_num, second_num,opt_nums):
      print(first_num)
      print(second_num)
      print(opt_nums)

Here we see a parameter with (*), this parameter will deal with an arbitrary number

Passing information back from them

Functions not only perform a given task when they are called. But a function may also return some value to the user. This returned value can be assigned, reused and modified then. For this, we use the return keyword

Using functions as variables

Functions can be used as variables. This is done by using function calls in our expressions.

def add (a,b):
     return a+b

def sub(b,a):
     return a-b

result = add(2, 3) + sub(2,3)

Local vs. global variables

Local Variables are the variables defined inside the functions. Their scope is only inside the function. They are not accessible outside the function.

Global variables are the variables defined outside the function and can be accessed and modified in and outside the function.

def add (a,b):
     return a+b

def sub(b,a):
     return a-b

result = add(2, 3) + sub(2,3)

Functions within functions

Right now we have learned how to call function. We can call a function inside another function providing the required signature of the function. This technique is called Nested Functions.

commissionCalc(sales):
   if sales>100:
       return sales*100
   else:
       return 0


def salaryCalc (basic) :
     grossSalary = basic + commissionCalc()
     print (f" your gross salary is {grossSalary}")