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.)