A function is a block of instructions that performs an action and, once defined, can be reused. Functions make code more modular, allowing you to use the same code over and over again.
Python has a number of built-in functions that you may be familiar with, including:
Let’s start with turning the classic “Hello, World!” program into a function.
A function is defined by using the def keyword, followed by a name of your choosing, followed by a set of parentheses which hold any parameters the function will take (they can be empty), and ending with a colon.
In this case, we’ll define a function named hello():
This sets up the initial statement for creating a function.
From here, we’ll add a second line with a 4-space indent to provide the instructions for what the function does. In this case, we’ll be printing Hello, World! to the console:
def hello(): print("Hello, World!")
Our function is now fully defined, but if we run the program at this point, nothing will happen since we didn’t call the function.
So, outside of our defined function block, let’s call the function with hello():
def hello(): print("Hello, World!") hello()
Now, let’s run the program:
You should receive the following output:
Functions can be more complicated than the hello() function we defined above. For example, we can use for loops, conditional statements, and more within our function block.
For example, the function defined below utilizes a conditional statement to check if the input for the name variable contains a vowel, then uses a for loop to iterate over the letters in the name string.
# Define function names() def names(): # Set up name variable with input name = str(input('Enter your name: ')) # Check whether name has a vowel if set('aeiou').intersection(name.lower()): print('Your name contains a vowel.') else: print('Your name does not contain a vowel.') # Iterate over name for letter in name: print(letter) # Call the function names()
The names() function we defined above sets up a conditional statement and a for loop, showing how code can be organized within a function definition. However, depending on what we intend with our program and how we want to set up our code, we may want to define the conditional statement and the for loop as two separate functions.
Defining functions within a program makes our code modular and reusable so that we can call the same functions without rewriting them.
Write a program that creates a single function that adds two numbers together and outputs the total.
Write a program that creates a two functions, one that adds two numbers together and outputs the total. The other should give the length of a string.
Write a program that creates two functions. Each function should use variables to store data and then carry out a process of your choice.
Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.
Have a great day!!
K McGuinness - 2018
No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.