Module 5 - Functions cont'd Flashcards
How do you activate a function?
function_name()
*MUST include the parentheses!
What is scope?
The place within a program where the variable is defined and can be accessed (local vs. global)
Where is local variable scope? Global variable scope?
Local = within the function in which they were created Global = entire program
Can different functions have the local variables with the same name?
Yes, they don’t see each others’ variables
What is an argument?
A piece of data that is passed to the function when you call it:
Function_name(argument)
What gets assigned to the parameter variable when the function is called?
The argument value is assigned to the parameter variable when a function is called
What is the scope of the parameter?
The scope is the function in which the parameter is used
How do you create a global variable? Where can you access a global variable?
To create, write an assignment statement outside all the functions
Can be accessed by ANY statement in the program, including INSIDE functions
If a function needs to assign a value to the global variable, how do you do that?
The global variable must be REDECLARED within the function; so, within the function, type:
global variable_name
What is a global constant?
References a value that cannot be changed (e.g., pi)
To stimulate in python, create a global variable and DO NOT re-declare it within functions
Should you use global variables? why or why not?
No! Best practice is to NOT use them unless code won’t work without it
- Makes debugging difficult
- Can’t use functions in another program
- Program is hard to understand
Can the called function change a variable within the calling function?
No! You can pass a value or variable to the called function but you cannot change the variable’s value in the calling function because that variable is out of scope in the called function
What is a keyword argument and what is the syntax for it?
Argument that specifies which parameter the value should be passed to
function_name(parameter=value)
What’s the difference between void and value-returning functions?
Void functions will perform a task; they consist of statements
Value-returning functions will return a value to the part of the program that called it
What is the standard library?
Library of pre-written functions that comes with Python
Library functions have tasks that programmers commonly need (print, input, range, etc.)
What are modules?
Files that store the standard library’s functions
T or F: Modules containing the related built-in functions are loaded into RAM when you start the interpreter.
False, some are loaded, some you must load by using the import statement (e.g., import math)
What is the code for generating a random number between 1 and 5?
What is the data type of the return value?
import random
random.randint(1,5)
It returns an integer
What does random.random() function do (after you’ve imported random module)?
Returns a random float between 0.0 and 1.0
does not receive arguments
What does the range function do?
Generates the integer numbers between the given start integer to the stop integer
i.e., it returns a range object
(typically used in loops)
What does the random.randrange function do?
Similar to range function, but returns randomly selected integer from the resulting sequence
What does the random.uniform function do?
Returns a random float but allows user to specify
range
T or F: Random number created by functions in random module are actually random.
False, they are pseudo-random numbers
What initializes the formula that generates the random numbers?
Seed value The random module uses the seed value as a base to generate a random number.
What does the random module use if seed value is not present? How do you specify desired seed value?
It takes system current time
Can use random.seed()function to specify desired seed value
What happens if you provide the same seed value before generating random data?
It will produce the same data
Need to use different seeds in order to get different series of random numbers
How do you write a value-returning function?
def function_name(): \_\_stmt \_\_return expression
the value for expression will be returned to the part of the program that called the function
What types of things can you return from a function to the part of the program that called it?
Literal, expression (can be complex), result of another value-returning function
What is dead code?
Code that will never be executed (for example, after the ‘return’ statement in a function)
What is a temporary variable? Why use it?
A variable used to store an intermediate value in a complex calculation.
They make debugging easier
Define incremental development.
A program development plan intended to avoid debugging by adding and testing only a small amount of code at a time.
What is scaffolding?
Code that is used during program development but is not part of the final version.
For example, print statements to check values
Define guardian.
A programming pattern that uses a conditional statement to check for and handle circumstances that might cause an error.
T or F: After a return statement runs, the function continues to execute any subsequent statements
False. As soon as a return statement runs, the function terminates without executing any subsequent statements.