Functions are used in any programming language to envelope a part of code which for example might be called several times. Instead of keep rewriting the same code it is possible to call a function with that specific code where required. Functions can have inputs (called also arguments) and can return an output. The keyword to create a function is def
, then the function needs to have a name and inputs within round brackets (if any).
def Print_Hello(): #Function called "Print_Hello" and does not have inputs print("Hello!") #Function prints "Hello!" and does not return any output Print_Hello() #Call Print_Hello function which then prints "Hello!" def Print_Name(name): #Function called "Print_Name" requires an input called "name" print("Hello "+name) #Prints "Hello " and then the name provided as input Print_Name("Valiant") # It prints "Hello Valiant"
The function can outputs some data/values by using the keyword return
.
def Double_Value(number): # Function that doubles the input value return 2*number value1=Double_Value(5) # value1 becomes 10 print(value1) # Prints 10 print(Double_Value(8)) # Prints 16
It is possible to pass multiple inputs to the function or also a single variable which is a collections of data (such as lists, tuples, etc...).
def add_values(a,b): # Function that adds two input values return a+b print(add_values(10,15)) # Prints 25 def multiply_values(list_values): # Function that multiplies all values in a list final_value=1 # Value that needs to be multiplied by the values in the list for i in list_values: # For loop of all the values in the list final_value*=i # Multiplication of the values return final_value l1=[5,2,8] # Created list l1 print(multiply_values(l1)) # It prints 80 (which is 5*2*8)
The functions can also have some default input values in case that specific value is not provided. This is done by including the default value in the round brackets during the creation of the function.
def divide_value(value,divide=2): # Function that divides a value by a number. This number is 2 by default. return value/divide print(divide_value(10)) # Prints 5 because second value is not provided and it is 2 by default. print(divide_value(10,4)) # Prints 2.5 because now divide = 4 (10/4=2.5)
A concept important with functions is the concept of scope, this defines the visibility of the variable that is created and where it can be used. The scope can be local or global. When a variable is defined in the main body of the script then it has global scope: this means that it can be seen and modified by all the functions. On the other hand, a variable created within a function has a local scope and cannot be used outsite that function (unless is returned). It is possible to change a local variable to global by using the keyword global
. Some examples of different scopes are provided below.
a=10 def print_function(): b=5 print(a) # Prints 10, because a is global print(b) # Prints 5 because it is local print_function() # Call the function print(b) # This produces an error because b does not exist outside the print_function def print_function2(): a=8 print(a) # Prints 8, because a is overridden within the function print_function2() # Call the function print(a) # Here a is still 10 def print_function3(): global a # Make a within the function global a=15 print(a) # Prints 15, because a is overridden and it is now global print_function3() # Call the function print(a) # Here a is now 15 because the global variable was modified
A function can also be called recursively, this means that the function calls itself. This is typically done when a problem needs to be decomposed in a smaller problem (or a different aspect of the same problem) still solved by the same function. The example below shows how this can be done to calculate the factorial of a number: this is defined mathematically as $n!=n \cdot n-1 \cdot n-2 \cdot \ldots \cdot 1$.
def factorial(n): if(n==1): # Definition of the smaller problem, factorial of 1 is 1 return 1 return n*factorial(n-1) # It multiplies n by the factorial of n-1. print(factorial(5)) # Prints 120 because 5*4*3*2*1=120
Python also allows the creations of anonymous functions with inputs and expressions defined typically in a single line. These are called lambda functions and their syntax is lambda inputs : expression
. These can also be included into other functions and allow the creation of multiple instances of these functions which can have different behaviors as shown below.
fun=lambda x : x*2 # The variable fun is a function which multiplies any value by 2 print(fun(10)) # Prints 20 fun2=lambda x,y : x+y # The variable fun 2 is a function which sums x and y print(fun2(2,3)) # Prints 5 because def pow_function(power): # The function raises a number to the power return lambda x : x**power # The function returns a lambda with that specific power quadratic=pow_function(2) # Created fuction that does x^2 cubic=pow_function(3) # Created fuction that does x^3 print(quadratic(8)) # Prints 64, becase 8^2=64 print(cubic(5)) # Prints 125, because 5^3=125
This concludes this overview on the functions in Python. Use the console below to create some functions.