Lesson 12 Flashcards
(21 cards)
a function is made up of 2 parts…
a header
and
a body
header is composed of (4)
the keyword def
the name of the function
the list of parameters between parantheses
a colon
body is composed of
instructions for the function
can end with a return statement
rules for naming a function (header)
1st character must be a letter or an underscore
keywords or spaces cannot be used
phython distinguishes upper vs lower case
header must always end with :
rules for body
all instructions and the return statement must be indented
ends with a return value when necessary
without the return statement the result of the function is not stored
whats given first, conditional or mandatory parameters
mandatory always first.. then conditional
whats the form of the optional parameter
parx=value
rather than par1, par2
note: one MUST specify arguments by position. same order as parameters were given
-
PRODUCTIVE AND VOID FUNCTIONS
-
definition productive function
a function which performs a specific task when it ends, RETURNING A VALUE to the instruction that called it
what do productive functions always end with
a return statement
definition void functions
a function that performs a specific task, and when it ends DOESNT RETURN ANY VALUE to the instruction that called it
difference between productive and void functions (2)
the return statement (productive functions always have it, void ones dont)
the ability to return a value
global vs. local variable
reached by any instruction in the program
vs.
can be reached within its scope, in the part within which its defined
whats the docstring
a text string, on or more lines enclosed in triple quotes («
whats an exception
an exception is triggered by some type of error
error types (3)
syntax error
runtime error
semantic error
definition of syntax error
an error in how the code is written
told error exists and where, not how to solve it
definition of runtime error
theres an error in the code, even if syntax is correct
phython shows where the error is and the cause of it
semantic error
when code is executed without error messages, but results are not the corect ones i.e. inconsistent or not expected
how are errors handled
most common instruction is:
try:(….)
except(….)