Overview
This article will explain what is, implementing and using functions in Python.
Materials
Computer
Python
IDE or Terminal
What is a Function
A function is a block that contains code, but the function can be called upon many times to execute the same code many times as needed. This solves the problem of rewriting the same code in the same Python file over and over again.
How to Make a Function
Below is the format to write a Function:
def function_name():
print("Hello")
To call a function, state the name of the function at the end of the Python file:
def function_name():
print("Hello")
function_name()
The Format How Functions are Usually Written
In the parenthesis of the function, inputs or parameters can be added to the function to help run code or perform operations. To optimally use functions, results are often returned:
def multiply_numbers(num1, num2):
return num1 * num2
print(function_name(5, 6))
print(function_name(7, 8))
print(function_name(11, 12))
In functions, code is written in the block and for best practices, "print()" is not used but instead "return" is used. "return" would write any code to the console that is specified after it. Since "print()" is not used within the function, then the function name has to be printed to print the output to the console.
Depending on how many parameters are specified in the function, when calling to execute that same function, the number of arguments must match the same number of parameters and be of a valid data type that makes the function's return statement True.
# the variables num1 and num2 declared within the parenthesis are parameters
def multiply_numbers(num1, num2):
return num1 * num2
print(function_name(5, 6))
# 5 and 6 are arguments. Two arguments are called upon since
# two parameters num1 and num2 were initially declared. The number
# of parameters and arguments must be the same.
To specify an unlimited amount of parameters, the terms "*args" and "**kwargs" can be used.
What is args and how to use it
The keyword "*args" allows an unlimited amount of parameters to pass to the function and arguments when calling the function. The variable can have another name instead of being named *args as long as it includes the "*" before the variable name.
def enter_words(first_words, *more_words):
return first_words, more_words
print(enter_words("One", "Two", "Three"))
print(enter_words("Four", "Five", "Six", "Seven"))
print(enter_words("Eight", "Nine", "Ten", "Eleven", "Twelve"))
Here the first parameter "first_words" is responsible for printing out "One", "Four" and "Eight", while "*more_words" is responsible for printing out the other words. The variable with the "*", prints the variables in a tuple.
What is kwargs and how to use it
The keyword "**kwargs" follows the same methodology of how to use it such as "*args". The variable can be named "kwargs" or another name as long as the variable has "**" before the name and it is used to pass an unlimited amount of parameters and returns the arguments in a dictionary.
def enter_words(first_word, **more_words):
for key, value in more_words.items():
return first_word, more_words
print(enter_words("Hi", One=1, Two=2, Three=3))