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.
Global Variable
Accessible to all the functions in a program file.
Reasons not to use global variables:
- Make debugging difficult
- Functions that use global variables are dependent on those variables.
- Make programs hard to understand.
Global Constant
- A name that references a value that cannot be changed.
- Permissible to use, unlike global variables.
Value Returning Function
A function that returns a value back to the part of the program that called it.
Standard Library
Pre-written functions (library functions) that the developer does not have to write.
Module
A Python source code file that can be imported into other Python programs.
Black Box
Any mechanism that accepts input, performs some operation that cannot be seen on the input, and produces output.
Pseudorandom Numbers
A number that is seemingly random but is actually calculated.
Seed Value
The number that is used to initialize the formula that produces random numbers.
Boolean Functions
A function that returns either True or False.
Modularization
Writing a program in such a way that each task is performed by a separate function.
What is a function?
A group of statements that exist within a program for the purpose of performing a specific task.
What is meant by the phrase “divide and conquer”?
A large task is divided into several smaller tasks that are easily performed.
How do functions help you reuse code in a program?
If a specific operation is performed in several places in a program, a function can be written once to perform that operation, then be executed anytime it is needed.
How can functions make the development of multiple programs faster?
By reusing the code.
How can functions make it easier for programs to be developed by teams of programmers?
The team and work can be broken down to work on different functions.
A function definition has what two parts?
- Function Header
- Block
What does the phrase “Calling a function” mean?
The interpreter jumps to that function and executes the statements in its block.
When a function is executing, what happens when the end of the function’s 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.
Why must you indent the statements in a block?
Indentation is how the interpreter understands block grouping.
What is a local variable? How is access to a local variable restricted?
What is a variables scope?
The part of the program in which the variable may be accessed.
Is it permissable for a local variable in one function to have the same name as local variable in a different function?
Yes, that is variable overloading. As long as all of the variables are local to their functions, that is permissable.