A Second Look at Functions:
- A function is a chunk of code that can be called by name to perform a task. Functions often require arguments, that is, specific data values, to perform their tasks. Arguments are also known as parameters.
- A function can also be thought of as a block of organized and reusable code that is used to perform a certain action.
- Python has built-in predefined functions. For example, print is a function.
- We can also define our own functions:
python
1wcuResidenceHalls = ["walker", "scott", "harrill"]
2
3def printWcuResidenceHallsTitleCase():
4 for wcuResidenceHall in wcuResidenceHalls:
5 wcuResidenceHallTitleCase = wcuResidenceHall.title()
6 print(wcuResidenceHallTitleCase)
7
8printWcuResidenceHallsTitleCase()- This is what a function looks like in Python. The first thing to note is the def keyword (line 3). This we use to indicate to Python that we are defining a function.
- The function name is, printWcuResidenceHallsTitleCase (line 3). After that we open and close parentheses.
- Finally, we add a colon at the end, and then start the function body.
- This is the code thatβs going to get executed (lines 4-6) when we call the function (line 8).
Calling the function as noted (in line 8) would output:
1Walker
2Scott
3Harrill