CH 5: Functions Flashcards
Function
A group of statements that exists within a program for the purpose of performing a specific task.
Benefits of Modular Programming
- Simpler Code
- Code Reuse
- Better Testing
- Faster Development
Void Function
A function that executes the statements it contains and does not return a value back to the part of the program that called it.
Value Returning Function
Executes the statements it contains, then returns a value back to the statement that called it.
Function Definition
The code for a function.
Python Function Naming Rules
- Cannot use a Python keyword.
- Cannot contain spaces.
- First character must be one of the letters a through z. A through Z, or an underscore.
- After the first character you may use the letters a through z or A through Z, the digits 0 through 9, or underscores.
- Uppercase and lowercase characters are distinct.
General Format of a function definition.
def function_name():
statement
statement
etc.
Function Header
- First line of a function definition.
- Begins with the keyword def, followed by the name of the function, followed by parenthesis, followed by a colon.
Block
A set of statements that belong together as a group.
Function Call
When a function is called, the interpreter jumps to that function and executes the statements in its block. Then, when the end of the block is reached, the interpreter jumps back to the part of the program that called the function, and the program resumes execution at that point.
Main Function
The function that holds the mainline logic and calls the program’s other functions, as needed.
Top-Down Design
A commonly used technique for breaking and algorithm down into functions.
- The overall task that the program is to perform is broken down into a series of subtasks.
- Each of the subtasks is examined to determine whether it can be further broken down into more subtasks. This step is repeated until no more subtasks can be identified.
- Once all of the subtasks have been identified, they are written in code.
Hierarchy Charts
- Used to visually represent the relationships between functions.
- Also known as a Structure Chart.
- Shows boxes that represent each function in a program.
Pass Keyword
- Facilitates the creation of empty functions.
- Ignored by the Python interpreter.
- Can be used as a placeholder anywhere in Python, not just functions.
Local Variable
- A variable that is created inside a function and cannot be accessed by statements that are outside the function.
- Different functions can have local variables with the same names because the functions cannot see each other’s local variables.
Variable Scope
The part of the program in which the variable may be accessed. A variable is visible only to statements in the variables scope.
Argument
Any piece of data that is passed into a function when the function is called.
Parameter Variable
A variable that receives an argument that is passed into a function.
Passed by Position
When the first argument is passed into the first parameter, the second argument is passed into the second parameter, and so forth.
Pass by Value
A form of argument passing that is used in Python, where a function cannot change the value of an argument that was passed to it.
Keyword Argument
Function calling syntax that specifies which parameter, by name, that an argument is to be passed into.
Keyword Only Parameter
- Parameter list that will only accept keyword arguments.
- Everything after *
Positional-Only Parameters
- Will accept only positional arguments
- Everything before` /
Default Argument
When a default argument is provided for a parameter, you can call teh function without explicitly passing an argument into the parameter.