sweigart chapter 3 Flashcards
function
like a mini program within a program. it lets you group code that gets executed multiple times.
def statement
defines the function.
body of the function
the code that follows the def statement. it is executed when the code is called.
deduplicating
means getting rid of duplicated or copy-and-pasted code. doing this makes your program shorter, easier to read, and easier to update. if you duplicate and ever decide to update, you have to change the code everywhere you copied it.
arguments
the values that are passed when a function is called. they are shown between parentheses.
parameters
variables that contain arguments. when a function is called with arguments, the arguments are stored in parameters. this stored value will be forgotten once the function returns.
return value
the value that a function call evaluates down to.
return statement
allows you to specify what the return value should be when creating a function using a def statement. it consists of (1) the return keyword and (2) the value or expression that the function should return.
None value
represents the absence of a value. it is the only value of the NoneType data type. it must be typed with a capital N. Python adds return None to the end of any function definition with no return statement. it is similar to how while and for loops implicitly end with a continue statement.
keyword arguments
identified by the keyword put before them in a function call. they are often used for optional parameters.
end keyword argument
two outputted strings automatically appear on separate lines because the print() function automatically adds a newline character. the end keyword argument is used to change the newline character to a different string.
sep keyword argument
when you pass multiple string values to print(), the function will automatically separate them with a single space. you could replace the default separating string by passing the sep keyword argument a different string.
call stack
the current function to be executed is always on top of the stack. it is how Python remembers where to return the execution after each function call. Python remembers which line of code called the function so that the execution can return there when it encounters a return statement. if that original statement called other functions, the execution would return those function calls first before returning to the original function call.
frame object
store the line number of the original function call so that Python can remember where to return. Python creates a frame object on the top of the stack if a function is called. when a function call returns, Python removes a frame object from the top of the stack and moves the execution to the line number stored in it. frame objects are always added and removed from the top of the stack and not from any other place.
local variables
parameters and variables that are assigned in a call function. they exist in that function’s local scope. they are also stored in frame objects on the call stack.