One Pager Cheat Sheet
- We will learn about functions, their
parameters
and working statements to create modular and reusable code blocks. - Functions
enclose a set of statements
tomake it easier to organize and reuse code
, following a specific structure. - We will learn to create and use our own user-defined functions in our program, as well as review existing built-in functions.
- Creating functions in Python requires a
def
keyword followed by the function name,parenthesis
(withfunction parameters
, if any), a colon, and an indentedbody
. - We can make a function work in our program by
calling
it with itsname
, followed by parentheses( )
. - It is not possible to call a function before it has been defined and implemented with all its
statements
andparameters
established. - Functions can take in external information through function parameters specified inside the parenthesis after the function name, which is then passed to the function call when invoked.
- There is no upper limit to the number of
function parameters
that can be specified, provided they are separated correctly and referenced correctly within the function. - A
return
statement in a function can allow the function to pass back areturn value
to the variable that called it. - The variable
result
is set toNone
implicitly returned by thediv()
function, resulting inNone
being printed when printing the value ofresult
. - The variable
result
contains4
, the larger value returned by the functioncompare_num()
, which comparesa
(4) andb
(2) and prints out "a is greater than b" whena
is the greater value. - With
functions
, we canreuse and organize
code more efficiently, avoiding the mistake of forgetting to call them.