Functions
are a block of code that only are execute when called. Functions have parameters that can be used to pass data.
Functions are created by using def keyword
def example_function():
print("This is a function")
If you were to run this code above, nothing would be printed. In order for a function be executed it must be called.
def example_function():
print("This is a function")
# calling function
example_function()
Output :
This is a function
Parameters
are created after the functions has been named. Users can add as many parameters as needed.
For the example below there is one parameter it is called ( names )
def example_function(names):
print(" Welcome!" + names)
example_function("Larry")
example_function("Curly")
example_function("Moe")
Output :
Welcome!Larry
Welcome!Curly
Welcome!Moe
Multi parameter Function
Function can have multiple parameters. In the example below, the function customer has two parameters: firstName and number.
When the function is called it should produce the customer’s first name and the customer’s number.
def customer(firstname, number):
print(firstName, number)
customer(firstName ='Mike', number ='234')
customer(firstName ='Kelly', number ='235')
Output :
Mike 234
Kelly 235