Functions are named, reusable blocks of code that perform tasks when called. Python programs use built-in functions such as print() and sqrt(), and programmers can define their own functions to organize and reuse instructions.
A function call is an instruction that asks a function to run. It is written using the function's name followed by parentheses. For example, print("Hello") calls the built-in print function to display text. A call to sqrt(25) asks the square-root function to calculate a result.
All flashcards
What is a function?
A function is a named, reusable block of code that performs a task when called.
How can you recognize a function call?
Look for a function name followed by parentheses, such as `print("Hello")` or `welcome()`.
What are the parts of a basic function definition?
The `def` keyword, a function name, parentheses, a colon, and an indented body. A docstring can describe the function's purpose.
What does parameterless mean?
A parameterless function has no parameters in the parentheses of its definition, as in `def welcome():`. It may still call other functions.
How do functions benefit a program?
They group related statements into modules, allow code to be reused with repeated calls, and make programs easier to understand, modify, and share.
Quiz questions
When Python reaches a function definition, what happens to the function body?Answer: Python records the function, but does not run its body yet.
Which expression is a function call that displays text?Answer: `print("Hello")`
Which naming style is conventionally used for Python function names?Answer: Words separated by underscores, as in `show_options`.
What does indentation do in a Python function definition?Answer: It marks which statements belong to the function body.
What is reusability when applied to a Python function?Answer: Running the same defined instructions through multiple calls.
Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.