unit 3 lesson 5 Flashcards
(7 cards)
Programming Functions
In the previous unit, we learned that a function is a sequence of statements that is only written once but can be called and reused whenever necessary. Functions are a core concept in programming, and you’ll end up using them all the time! In essence functions take in data, process it, and return a result.
Step 1
Declare the function with the keyword def followed by the function name.
Step 2
Write the arguments inside the opening and closing parentheses of the function, and end the declaration with a colon.
Step 3
Add the program statements to be executed.
Step 4
End the function with/without return statement.
Functions: A guided example
Let’s walk through a simple example of writing and calling a function in Python. You need to create a function to add up the amounts on a shopping list to give the total amount the shopper owes when they are at the checkout. To do its job, the function simply needs the list of items the shopper wants to purchase, and it will then look up their prices and add them together.
Conclusion
Well it’s been a jam-packed unit, that’s for sure. You’ve learned a fair number of the core building blocks of computer programming, and put them to practice. You stand at the precipice of a whole new world, one full of opportunity and endless creativity. Stay tuned for more binary adventures, and give yourself a pat on the back. You’ve come a long way!