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

Void Functions and Functions with Return Values

The document explains the concepts of void functions and functions with return values in Python, detailing how return statements work and the different types of parameters. It also covers variable scopes, namespaces, and types of arguments, including default, keyword, positional, and arbitrary arguments. Additionally, it provides examples and assignments to illustrate these concepts.

Uploaded by

pranavrao168
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Void Functions and Functions with Return Values

The document explains the concepts of void functions and functions with return values in Python, detailing how return statements work and the different types of parameters. It also covers variable scopes, namespaces, and types of arguments, including default, keyword, positional, and arbitrary arguments. Additionally, it provides examples and assignments to illustrate these concepts.

Uploaded by

pranavrao168
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1 Void Functions and Functions with Return Values

Functions:

 Parameter:

Void and Not Void (Returns Value)

 No Parameter:

Void and Not Void (Returns Value)

Void function in python is a function is a function that performs an action but does not return any
computed or final values to the column.

A return statement is used to end the execution of the function called and returns the result (value
of the expression following the return keyword) to the caller. The statements after the return
statements are not executed. If the return statement is without any expression, then the special
value ‘N’ is returned. A return statement is overall used to invoke a function so that the passed
statements can be executed.
Example:

1. Parameters – Void

Output:

2. Parameters – Returns Value

Output:

3. No Parameter – Void

Output:
4. No Parameters – Returns Value

Output:

Assignment 1

Q.1 Write a function ThrowDice() that accepts no Parameter and returns a number in the range 1 to
6.

Q.2 Write a function to accept length and breadth and calculate area. Accept height inside the
function and return the volume of a cuboid.

Q.3 Create a calculator function that accepts two numbers and displays the result after performing 4
main arithmetic operations. Analyse how a menu system can be incorporated to return the only the
chosen operation result.
16/02/2023

Local Variable

Global Variable

Enclosed Variable

Built-in Variable

Namespace

A name space is a collection of currently defined symbolic names along with information about the
object that each name prefers. You can think of a namespace as a dictionary in which the keys are
the object names and values are the objects itself.

The dir () function returns all properties and methods, even built-in properties which are default for
all objects.

Scopes of Variables/ Types of Namespaces

A variable is only a variable from inside the region it is created –

1. Local Scope:
A variable created inside a function belongs to the scope of that function and can only be
used inside that function.
Example:
2. Global Scope:
A variable created in the main body of the python code is a global variable and belongs to
the global scope.
Example:

Output:

If you operate the same variable name inside and outside the function, python will treat
them as 2 separate variables. One variable in global scope which is outside the function and
one variable in local scope which is inside the function.
Example:

Output:

Output:

Here, example 2 will throw an error called UnboundedLocalError, because python treats the
variable ‘x’ as two separate variables.
If you need to create a global variable but stuck in the local scope you can use the global
keyword. The global keyword makes the variable global
Example:
Output: 11

3. Enclosing Scope:
Enclosing or nonlocal scope is a special scope that only exists for nested functions. If a local
scope is an inner or a nested function then the enclosing scope is the scope of the outer or
enclosing function. This scope contains the name that you define within the enclosing
functions.
Example:

Output:
10
11

4. Built-in Scope:
When a variable or object is not found in local, global, or enclosing scope then python starts
looking for it in the built in scope. Built-in Scope are one of the widest scopes that cover all
the reserved keywords. These are easy to call anywhere in the program prior to using them,
without the need to define them.
 Built-in variable __name__: Since there is no main function in python, when the commands
to run a python program, is given to the interpreter the code that is at level 0 indentation is
to be executed. However before doing that, it will define a few special variables. __name__
is one such special variable. If the source file is executed as the main program, the
interpreter sets the name variable to have the value “__main__”. If this file is being
imported from another module, __name__ will be set to the modules name.
__name__ is a built-in variable that evaluates the name of the current module. Thus, it can
be used to check whether the current script is being run on its own or being imported
somewhere else by combining it with the if statement as shown below.
Example:

Output:

Example2:

Output:

 LEGB- Local Enclosed Global Built in – Python resolves names using LEGB rule. This rule is a
kind of lookup procedure which determines the order in which python looks up for names.
Ex: We can access a name, then python will lookup the name sequentially in the local,
enclosing, global and built in scope

UnboundLocalError
This error occurs when a local variable is referred to before it is assigned. The variables in
python are specified only inside a function that is global by default. If a value is assigned to a
variable in the function body, unless it is explicitly defined to be global it is presumed to be
local.

Types of Arguments

1. Default Arguments:
Defaults arguments are values that are provided while defining functions. The assignment
operator ‘=’ is used to assign a default value to the argument. Default arguments become
optional during the function calls. If we provide a value to the default arguments during
function calls, it overrides the default value. The function can have any number of default
arguments. The default arguments should follow non default arguments.
Example 1:

Output:
12

2. Keyword Arguments:
Functions can also be called using keyword arguments of the form ‘Kwarg = value’. During a
function call, values passed through arguments don’t need to be in the order of parameters
in the function definition. This can be achieved by keyword arguments but all the keyword
arguments should match the parameters in the function definition.
Example 2:

Output:
9

3. Positional Arguments:
During a function call value passed thorough arguments should be in the order of
parameters in the function definition. This is called positional arguments.
Example 3:
def sum (a,b):
return a + b
print(sum (2,4)) → Position Argument

4. Arbitrary Arguments:
Variable length arguments are also known as arbitrary arguments. If we don’t know the
number of arguments needed for the function in advance, we can use arbitrary arguments.
An ‘*’ is placed before the parameters in function definition which can hold non keyword
variable length arguments. These arguments will be wrapped up in a tuple.
Example 4:

Output:

You might also like