Python Introduction | Functions | Defining Functions
Functions are defined as follows.
Function Definition
def functionName(argument1, argument2, ...):
... processing ...
A function definition begins with def functionName. Add parentheses and a colon after the name. On the following lines, indent the processing performed by the function.
You can add arguments inside the parentheses. Arguments receive values when the function is called. For example:
def showMsg(str):
This function has an argument named str. When calling the function, provide a value to assign to that variable.
showMsg("Taro")
The value "Taro" is passed to the str variable in showMsg. The function can use that variable in its processing.
print("Hello, " + str + ". How are you?")
A function can receive any number of arguments. Separate them with commas.
def showMsg(a, b, c):
Parentheses are required even when a function has no arguments. These are the minimum concepts needed to begin using functions.