# Simple function
# Function definition
def a_func():
print ("A Message from the other world!")
# Function Call
a_func()
a_func()
# Passing arguments to functions
def add_two(num):
print (int(num)+2)
add_two (3)
add_two ("45") # This will work as we are casting the passed parameter to Integer before adding
# Multiple arguments
def add_sub(add_two_to_this, sub_two_from_this):
print ("Added 2 to : " + str(add_two_to_this))
print ("Answer: " + str(int(add_two_to_this)+2))
print ("Subtracted 3 from : " + str(sub_two_from_this))
print ("Answer: " + str(int(sub_two_from_this)-2))
add_sub (45, "67")
add_sub ("-156745", 12131)
# Ordering of passed argument matters
def coding_in(name, language):
print (str(name) + " is coding in " + str(language))
coding_in("Jennifer", "Python")
# If you change the order, results are unexpected
coding_in("Python", "Jennifer")
# Keyword Arguments
# Pass the argumnets as key:value pairs, so even if unordered it won't produce unexpected results
def coding_in(name, language):
print (str(name) + " is coding in " + str(language))
coding_in(name="Jennifer", language="Python")
# If you change the order, results are same
coding_in(language="Python", name="Jennifer")
# Default Values for parameters
# Note: If you do not pass arguments required by the function and that argument does not have a default value,
# then python will throw an error
def coding_in(name, language="Python"):
print (str(name) + " is coding in " + str(language))
coding_in("Jennifer") # Since 2nd argument is not passed, it takes on the default parameter given
coding_in("Jennifer", "R") # Since 2nd argument is passed, it takes on the passed arguemnt
# Note: You cannot have parameters with no default value after parameters having default value
# Following is an error
def coding_in(name, language1="Python", language2):
print (str(name) + " is coding in " + str(language1) + " and " + str(language2))
coding_in("Jennifer", "Python", "R")
# Easy fix to above error is to declare all non-default parameters,
# and then start declaring the default parameters
# Note: Default parameters can be used to make an argument optional
def coding_in(name, language2, language1="Python"):
print (str(name) + " is coding in " + str(language1) + " and " + str(language2))
coding_in("Jennifer", "Python", "R")
coding_in("Jennifer", "R")
# All parameters can be default
def coding_in(name="Jennifer", language1="Python", language2="R"):
print (str(name) + " is coding in " + str(language1) + " and " + str(language2))
coding_in()
# return
def pow_4(num):
return num**4
v1 = pow_4(34) # v1 now stores 34^4
v2 = pow_4(23) # v2 now stores 23^4
print ("34^4 = " + str(v1))
print ("23^4 = " + str(v2))
# can return any data type or object from function
def make_a_coder(name, age, language):
d1 = {'name': name,
'age' : age,
'language' : language}
return d1
print(make_a_coder("Jennifer", "21", "Python"))
# can pass any data type or object to function
def make_many_coders(list_of_coders):
print ("Name \tLanguage \tAge")
print ("========\t==========\t===")
for coder,details in list_of_coders.items():
print (str(coder) + "\t" + str(details[0]) + "\t\t" + str(details[-1]))
return str(len(list_of_coders))
d1 = {"Jennifer": ["Python", 21],
"Scarlett": ["R", 21]}
print ('\n' + make_many_coders(d1) + " coders found!")
# If a list passed to a function is changed inside the function then the change is permanent
def make_language_list(language_list, new_language):
language_list.append(new_language)
lang_list = ["Python", "R"]
print (lang_list)
make_language_list(lang_list, "Julia")
print (lang_list)
# Preventing a function from modifying a list
def make_language_list(language_list, new_language):
language_list.append(new_language)
lang_list = ["Python", "R"]
print (lang_list)
make_language_list(lang_list[:], "Julia")
print (lang_list)
# Passing Arbitrary number of arguments
# The '*' tells Python to make a tuple of whatever number of arguments it receives at that position
def languages(*many_languages):
print (many_languages)
languages ("Python")
languages ("Python", "R", "Julia", "Ruby", "Go")
# Passing Arbitrary number of arguments with a normal argument
# Note: The parameter that accepts arbitrary number of arguments needs to be placed at last
def knows_languages(name, *languages):
print (str(name) + " knows: " + str(languages))
knows_languages("Jennifer", "Python")
knows_languages("Jennifer", "Python", "R", "Julia", "Ruby")
# Note: You cannot have two or more parameters that accepts arbitrary number of arguments
# Hence, following is an error!
def knows_languages_and_modules(name, *languages, *modules):
print (str(name) + " knows: " + str(languages))
knows_languages("Jennifer", "Python", "PyGtk")
knows_languages("Jennifer", "Python", "R", "Julia", "Ruby", "PyGtk", "PyGame", "audioop")
# Using Arbitrary Keyword Argument
# Note: '**' tells Python to create a dictionary of the extra arguments passed after the first required positional argument
def make_a_coder(name, **details):
coder = {}
coder["name"] = name;
for key, value in details.items():
coder[key] = value
return coder
print (make_a_coder("Jennifer", location="California", age="21", language=("Python", "R")))
# We can do this since we are using keyword arguments
print (make_a_coder(location="California", age="21", language=("Python", "R"), name="Jennifer"))