Module 6 - Functions, Scopes, and Exceptions
Module 6 - Functions, Scopes, and Exceptions
and Exceptions
PPE - Module 6
Functions
You've come across functions many times so far, but the view on their merits that
we have given you has been rather one-sided. You've only invoked the functions
by using them as tools to make life easier, and to simplify time-consuming and
tedious tasks.
When you want some data to be printed on the console, you use print(). When you
want to read the value of a variable, you use input(), coupled with either int() or
float().
How to make a function?
The function's full power reveals itself when it can be equipped with an interface
that is able to accept data provided by the invoker. Such data can modify the
function's behavior, making it more flexible and adaptable to changing conditions.
A parameter is actually a variable, but there are two important factors that make
parameters different and special:
● parameters exist only inside functions in which they have been defined, and
the only place where the parameter can be defined is a space between a pair
of parentheses in the def statement;
● assigning a value to the parameter is done at the time of the function's
invocation, by specifying the corresponding argument.
Parameterized Functions(cont.)
A technique which assigns the ith (first, second, and so on) argument to the ith
(first, second, and so on) function parameter is called positional parameter
passing, while arguments passed in this way are named positional arguments.
Parameterized Functions(cont.)
Python offers another convention for passing arguments, where the meaning of
the argument is dictated by its name, not by its position - it's called keyword
argument passing.
Parameterized Functions(cont.)
It happens at times that a particular parameter's values are in use more often than
others. Such arguments may have their default (predefined) values taken into
consideration when their corresponding arguments have been omitted.
Return Functions
All the previously presented functions have some kind of effect - they produce
some text and send it to the console.
To get functions to return a value (but not only for this purpose) you use the return
instruction
This word gives you a full picture of its capabilities. Note: it's a Python keyword.
The return instruction has two different variants - let's consider them separately.
Recursive Functions
This term may describe many different concepts, but one of them is especially
interesting - the one referring to computer programming.
The scope of a name (e.g., a variable name) is the part of a code where the name
is properly recognizable.
For example, the scope of a function's parameter is the function itself. The
parameter is inaccessible outside the function.
global keyword
Simulation [18]
Exceptions
Dealing with programming errors has (at least) two sides. The one appears when
you get into trouble because your – apparently correct – code is fed with bad data.
For example, you expect the code will input an integer value, but your careless
user enters some random letters instead.
It may happen that your code will be terminated then, and the user will be left
alone with a terse and ambiguous error message on the screen. The user will be
unsatisfied, and you should be unsatisfied, too.
We're going to show you how to protect your code from this kind of failure and how
not to provoke the user's anger.
Simulation [19]
Key Takeaways Functions
Key Takeaways Functions
Key Takeaways Functions
Key Takeaways Functions
Key Takeaways Functions
Key Takeaways Functions
Key Takeaways Functions
Key Takeaways Functions
Key Takeaways Functions
Key Takeaways Scopes
Key Takeaways Scopes
Key Takeaways Scopes
Key Takeaways Scopes
Key Takeaways Exceptions
Key Takeaways Exceptions
Key Takeaways Exceptions
Key Takeaways Exceptions
Key Takeaways Exceptions
Key Takeaways Exceptions