Creating functions
Introduction to Functions
The help function:
help(print) # You can pass any other function to the help function and this will provide you with info about that particular function
# You just add the name of the function
This shows arguments
Provides info of what the function does
The print function:
# A function does something
print(args) # You pass to a function arguments
Creating your own function:
You define functions with the word def
# Defining the function:
def greet_person():
print("Hello there, this is a greeting...")
# Calling the function:
greet_person()
Calling function instances with args:
def greet_person(jiberish): # For this to run it awaits the jiberish arg
print("Hello there, this is a greeting...")
# Calling the function with an arg
greet_person("Taz") # It will not complain as we are adding a arg
To do a more personalized greeting:
def greet_person(name):
print("Hello there " + name + "!")
greet_person("Chris")
# Will print: Hello there Chris!
Adding a default argument to the function:
This is used when an arg is not passed to the function
def greet_person(name = "your name"):
print("Hello there " + name + "!")
greet_person()
# Will print: Hello there your name!
Adding and documenting functions in a prod environment:
def greet_person(name = "your name"):
"""
DOCSTRING: this returns a greeting
INPUT: name
OUTPUT: hello ... name
"""
print("Hello there " + name + "!")
When you are defining a function the value is a
Parameter
When you are calling a function the value is a
Argument
Last updated