Functions
Functions are used to groups of predefined statements for repeated operations. Used to reduce redundancy and keep your program small and clear.
Function Definition
This is made of the function’s name and a block of statements.
e.g. def calc_pizza_area():
Function call
You can use the functions name to “call” it, which causes the function statements to run.
Return Statement
A function can return one value by using a return statement.
Parameters
Is a named variable listed inside the parentheses in the function’s definition. It acts as a place holder or local variable that is used within the body of the function to show what kind of data the function will use.
e.g. def add(x, y):
Arguments
The argument is the actual value that is given to the function when it is called. These values are assigned to the corresponding parameters at runtime.
e.g. def add(5, 3):
Polymorphism
This happens when your function adds different types together.
e.g. (“x” * 5 = xxxxx)
function stubs
You write functions incrementally by writing out all the functions you think you may need and then using the “pass” keyword that acts as a placeholder until you are ready to work on it.
Local Variables
Are variables that are defined INSIDE the function and are invisible to the code outside the function.
use LIBERALLY
Global Variables
These are variables that are defined OUTSIDE the function and can be used both inside and outside of the function.
use SPARINGLY
Scope
Scope refers to the area of code where a name is visible.
- Built-in scope = all built in names int(), str() ect.
docstring
This is a way of documenting each function so that people can understand your code.
help()
The help() function will print out the docstring for whatever function you are trying to use so you can get more information on how to call it.