0% found this document useful (0 votes)
11 views

Module 6 - Functions, Scopes, and Exceptions

Uploaded by

merryfil.adolfo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module 6 - Functions, Scopes, and Exceptions

Uploaded by

merryfil.adolfo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Functions, Scopes,

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?

● It always starts with the keyword def (for define)


● next after def goes the name of the function (the rules for naming functions
are exactly the same as for naming variables)
● after the function name, there's a place for a pair of parentheses (they
contain nothing here, but that will change soon)
● the line has to be ended with a colon;
● the line directly after def begins the function body - a couple (at least one) of
necessarily nested instructions, which will be executed every time the
function is invoked; note: the function ends where the nesting ends, so you
have to be careful.
● The function body - must follow the indentation rules of python
Remember, Python is a interpreter not a
compiler
Simulation [16]
Parameterized Functions

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.)

Positional parameter passing

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.)

Keyword Argument passing

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.)

Argument default value

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.

Of course, functions - like their mathematical siblings - may have results.

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.

In this field, recursion is a technique where a function invokes itself.


Simulation [17]
Functions and scopes

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

Nothing is perfect in this world and software is no exception.

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

You might also like