π Introduction to Functions
Contents
π Introduction to Functions#
What are Functions?#
Functions are a convenient way to divide your code into useful blocks
order our code
make it more readable
reuse it and save some time.
way to define interfaces so programmers can share their code.
How do you write functions?#
def name_of_function(input_argument1, input_argument2):
Line_1_of_function = all_lines_in_function_must_be_tab_indented
Line_2_of_function = all_lines_in_function_must_be_tab_indented
The most simple function#
def my_function():
print("I am a Drexel Dragon")
When you ran this code why did it not print the output?
It did not print because the code just defined the function my_function. You need to call the function for it to run.
my_function
<function __main__.my_function()>
Why did this not print the result?
We just typed the object for the function and it returned the object to the function. You need to call the function using ()
for it to run
my_function()
I am a Drexel Dragon
Functions with Arguments#
def my_function(name, major):
print(f"I, {name} am a Drexel Dragon, and I am majoring in {major}")
my_function("Jay", "Mechanical Engineering")
I, Jay am a Drexel Dragon, and I am majoring in Mechanical Engineering