In this chapter, you'll learn how to write functions, which are blocks of code designed to perform specific tasks. Functions allow you to reuse code by calling the function whenever you need it, instead of rewriting the same code multiple times. This makes your programs easier to write, read, test, and fix. You'll also explore different ways to pass information to functions, create functions that display information or return values, and store functions in separate files, called modules, to keep your programs organized.
A function is a piece of code which is designed to perform a specific task.
You DEFINE what task you want it to do, and then CALL it to use it
It can be multiple things grouped together, or a single thing
We use the keyword def to define a function.
We also need to make sure we have parenthesis and a colon after the function name!
We use the keyword def to define the function and give it a name
We also have to include ( ): after the name of the function
For now, the brackets are empty, as the function does not need any information
This will change later. We can give functions information (ARGUMENTS) that it can use too!!
Anything we want the function to do HAS TO BE INDENTED
The function's body, indented after the definition, contains the actual code
Once we define it, we can do tasks inside the function
This example was simple... it just prints a statement
In order to USE the function, we call it later by using it's name
This is known as a FUNCTION CALL and it tells Python to use the code inside the function
Note that it is called at the same indent level as it is defined!!!
What happens if we want a function that we write to not just run its own code, but to run code from another function? Can we do that??
YES! But ORDER MATTERS
Python Reads Code Top-to-Bottom:
A function must be defined before it is called in the code.
If you try to call a function that hasn't been defined yet, you'll get a NameError.
Order of Function Definitions:
If a function A calls another function B, B must be defined above or before A is executed
This Gives You an Error!!!
This works As Expected
You must write a function which will display a banner or some sort of message
Congratulations on being hired as the new Jurassic Park Operations Specialist! Your mission is to ensure the park runs smoothly and visitors have an unforgettable (and safe!) experience. As the programmer behind the scenes, you’ll need to set up automated systems for park operations.
You must write the following functions:
A Function that Welcomes The Guests to Jurassic Park - welcome_visitors()
This prints a fun Jurassic Welcome Message
A Function that Unlocks the gates to the park
Write a function called unlock_gates which tells the user the gates are being unlocked
A Function which Checks on the dinosaurs to ensure they’re secure.
Write a function called check_dinosaurs()
Add some fun logic to tell you if they're secure or not. Maybe a die roll??
A Function which Starts park operations to make the park fully operational.
Name this function start_park_operations()
This function has to call the other functions...Write a function called start_park_operations which does the following:
Welcomes the Guests
Unlocks The Gates
Checks Dinosaurs
Prints a Final Message that the park is ready for visitors
Our last function didn't require any extra information to run. When we called it, it just printed a nice message.
Sometimes, we need to pass information into a function for it to do what we want.
SCENARIO: What if we want a function to greet a person by name??
Do we know the name, or have to print out every possible option?
Of COURSE we don't know the name. So, let's pass the name in!!!
#define a function which requires a name to be passed in
def print_greeting(username):
print(f"\nHello, {username.title()})
#Call the function with a specific name
print_greeting('Mr. Mac')
username is a parameter - The piece of information needed
'Mr. Mac' is an argument - The actual information passed in
'Mr. Mac' is then assigned to username when the function is called
Every time we see username in the function, it's really 'Mr. Mac'
When you call a function, Python must match each argument in the function call with a parameter in the function definition. The simplest way to do this is based on the order of the arguments provided.
Values matched up this way are called positional arguments
yellow minion is assigned to minion_type
kevin is assigned to minion_name
What if you switch the position of the arguments?? Does it matter?
TRY THIS AND SEE WHAT HAPPENS!!
Ahoy, matey! 🏴☠️ Welcome to Bikini Bottom, the most exciting underwater town in the Pacific Ocean! SpongeBob SquarePants is throwing a huge bash at the Krusty Krab, and you’ve been hired as the official party programmer to make everything look spectacular.
But wait—what’s a party without an amazing banner to welcome all the sea creatures? SpongeBob needs YOU to code a custom banner generator for the party!
Write a Python program that creates a fancy banner to welcome guests to the party. The banner should:
Display a top and bottom border of stars (*).
Center the party message based on the length of the banner.
Be customizable with the guest's name and desired banner length.
BONUS:
Have the user tell you what character they want the banner to display (it doesn't have to be the * character) - You have to ask the user for that input
Have the user pick if they want the message to be:
Centered, Left Aligned, or Right Aligned
This is a VERY Simplified Version of Passing a List. This is dangerous, as the function gets complete access to your list! You may want that to happen. But... You may not! We will revisit this later this unit!!! It's a bit more advanced!
🦖 Welcome to Jurassic Park, where the dinosaurs are bigger, the adventures are wilder, and the programmers—you—are in charge of the visitor experience! John Hammond has entrusted you with a vital task: designing a menu system for all the park's attractions, restaurants, and safety guidelines. Your program will allow park operators to input items (like rides or rules) and display a neat, formatted menu for visitors.
Write a Python program that:
Has a function which uses a LIST as an argument, that prints out a "menu" and displays the menu neatly formatted. You MUST use the list of menus as the parameter passed to the function!!!
The list can be hard coded! You don't need to let the user create it!
You must also create a banner function!!!
BONUS:
Write a function which prompts the user to create the list
Use this function to create the list that is passed into the menu display function!
YOU MUST HAVE 2 FUNCTIONS!
One function gets the input to modify a list
The other function is passed the list, and displays it as a menu