1/20
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
benefits of modularizing a program with functions
simpler code
code reuse
better testing
faster development
easier facilitation of teamwork
black box analogy
when you make your own functions, you will want to make them appear as black boxes to other programmers
there are fewer details that you need to keep in mind
implementing functions
a function to calculate the volume of a cube
what does it need to do its job?—-input
what does it answer with—-output
when writing this function
pick a name
declare a variable to receiver incoming argument
pull all info together to make the first line of the function’s definition
use return statement to get result
void functions
a function without a return statement
return None
testing a function
in order to test the function, your program should contain:
definition of the function
statements that call the function
docstrings
describe what your function does
main function
required function name used by other common languages
using functions: order
important that you define any function before you call it
but, a function can be called from within another function before the former has been defined
parameter variable
declared in the called function
initialized w/ the value of the argument value
used as a variable inside the called function
parameter passing
parameter variables receive the argument values supplied in the function call
the argument value may be:
a variable
a literal value
pass-by-value
the called function receives a copy of the argument’s value and works exclusively with that copy
for immutable objects, if changed inside a function, Python leaves the original object alone and forces the local parameter variable to point to a brand-new copy of the object
(int, float, str, tuple) can’t be change after made
pass-by-reference
Python copies the argument object’s reference-not the object itself- into the corresponding parameter
for mutable objects
objects like list, dict, and set can be changed in place
bc the function receives a copy of the reference pointing to the exact same object in memory, any in-place modifications will change the original object
return values
does two things:
immediately terminates the function
passes the return value back to the calling function
reusable functions
find repetitive code
may have different values but same logic
variable scope
can be declared:
inside a function
known as ‘local variables
only available inside this function
parameter variables of a function are local variables
outside of a function
‘global scope’
can be used (and changed) by coed in any function
how do u chose between which variable scope
the scope of a variable is the part of the program in which it is visible and can be accessed
local variables of functions
variables declared inside one function are not visible to other functions
sideLength is local to main
Using it outside main will cause a compiler error
global variables
variables that are defined outside functions
a global variable is visible to all functions that are defined after it
any function that wishes to modify a global variable must include a global declaration
variable lifetime
the duration for which the variable exists in memory
global: created when they are defined in the main body of the script and persist until the entire program ends or the variable is explicitly deleted using keywords del
local: created when the function finishes execution or encounters a return statement
recursive functions
a function that calls itself
solves a problem by using the solution of the same problem with simpler inputs
to terminate, there must be special cases for the simplest inputs
string slicing
if s is a string, the expression s[start : stop : step] returns the portion of the string from inex start to index stop at a step size step