0% found this document useful (0 votes)
8 views13 pages

Unit 2 - 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Unit 2 - 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Functions: Introduction, defining a Function, calling a Function, Functions with/without

Return Values, Positional and Keyword Arguments, Passing Arguments by Reference Values,
ModularizingCode, The Scope of Variables, Default Arguments, Returning Multiple Values,
Function Abstraction andStepwise Refinement.
Functions: Introduction
• Functions can be used to define reusable code and organize and simplify code.
• Functions is a block of statements that return the specific task.
• The idea is to put some commonly or repeatedly done tasks together and make a
function so that instead of writing the same code again and again for different
inputs, we can do the function calls to reuse code contained in it repeatedly.
• Some Benefits of UsingFunctions:
o Increase Code Readability.
o Increase Code Reusability.

Declaration and Defining a Function


• A function definition consists of the function’s name, parameters, and body.
• The syntax to declare a function is:

• The syntax for defining a function is as follows:

• A function contains a header and body. The header begins with the def keyword,
followedby the function’s name and parameters, and ends with a colon.
• The variables in the function header are known as formal parameters or simply
parameters.
• A parameter is like a placeholder: When a function is invoked, you pass a value to
the parameter.
• This value is referred to as an actual parameter or argument. Parameters are
optional;that is, a function may not have any parameters.
• Some functions return a value, while other functions perform desired operations
withoutreturning a value. If a function returns a value, it is called a value-returning
function.
• Example 1:
• Example Diagram is as follows:

• Example 2:
• Example 3:

Calling a Function
• Calling a function executes the code in the function.
• Example: See the Previous example.

Functions with/without Return Values

• To see the differences between a function that does not return a value and a
function thatreturns a value.
• Program 1: The printGrade function does not return any value. So, it is invoked as a
statement in line 17 in the main function.
• Program 2:let us redesign the printGradefunction to return a value. We call the new
function that returns the grade, getGrade, as shown in:
Positional and Keyword Arguments
• A function’s arguments can be passed as positional arguments or keyword
arguments.

Keyword-Only Argument Positional-Only Argument

Arguments are passed in the order of


Parameter Names are used to pass the
parameters. The order defined in the
argument during the function call.
order function declaration.

Order of parameter Names can be Order of values cannot be changed to


changed to pass the argument (or values). avoid the unexpected output.

Syntax : Syntax:
FunctionName(paramName=value,…) FunctionName(value1,value2,value3,….)

• Example for Keyword – Only arguments:


• Note:
So now, we will call the function by using the keyword-only arguments (or by using
the parameter names) in two ways and In both cases, we will be getting the correct
output but not necessarily in the positional argument. Here we have used argument
naming to declare the value. This is called the keyword-only argument.

• Example for Positional-Only Arguments:

• Note:
Position-only arguments mean whenever we pass the arguments in the order we
have defined function parameters in which if you change the argument position then
you may get the unexpected output. We should use positional Arguments whenever
we know the order of argument to be passed. So now, we will call the function by
using the position-only arguments in two ways and In both cases, we will be getting
different outputs from which one will be correct and another one will be incorrect.
Passing Arguments by Reference Values
• When you invoke a function with arguments, each argument's reference is passed
by value to the parameter in the function.
• Because all data are objects in Python, a variable for an object is a reference to the
object. When you invoke a function with arguments, the reference value of each
argument is passed to the parameter. This is referred to as pass-by-value in
programming terminology.
• For simplicity, we say that the value of an argument is passed to a parameter when
invoking a function.
• The value is a reference value to the object.
• If the argument is a number or a string, the argument is not affected, regardless of
the changes made to the parameter inside the function. Listing 6.4 gives an example.

• As shown in the output for Listing 6.4, the value of x (1) is passed to the parameter n
to invoke the increment function. The parameter n is incremented by 1 in the
function, but x is not changed no matter what the function does.
• The reason is that numbers and strings are known as immutable objects. The
contents of immutable objects cannot be changed.
Modularizing Code
• Modularizing makes code easy to maintain and debug, and enables the code to be
reused.
• Functions can be used to reduce redundant code and enable code reuse.
• Functions can also be used to modularize code and improve a program’s quality.
• In Python, you can place the function definition into a file called module with the
file-name extension .py.
• The module can be later imported into a program for reuse. The module file should
be placed in the same directory with your other programs. A module can contain
more than one function.
• Each function in a module must have a different name.

The Scope of Variables


• The scope of a variable is the part of the program where the variable can be
referenced.
• This section discusses the scope of variables in the context of functions.
• A variable created inside a function is referred to as a local variable.
• Local variables can only be accessed within a function. The scope of a local variable
starts from its creation and continues to the end of the function that contains the
variable.
• In Python, you can also use global variables. They are created outside all functions
and are accessible to all functions in their scope. Consider the following examples.
• A global variable is created in line 1. It is accessed within the function in line 4 and
outside the function in line 8. A local variable is created in line 3. It is accessed within
the function in line 5.
• Attempting to access the variable from outside of the function causes an error in line
9.

• Here a global variable x is created in line 1 and a local variable with the same name
(x) is created in line 3. From this point on, the global variable x is not accessible in the
function.
• Outside the function, the global variable x is still accessible. So, it prints 1 in line 7.

Default Arguments
• Python uses the default value for such an argument if no value is passed to it. If any
value is passed, the default value is overridden with the actual value passed.
• Default arguments in Python are the function arguments that will be used if no
arguments are passed to the function call.
• Example:
o The following example shows use of Python default arguments. Here, the
second call to the function will not pass value to "city" argument, hence its
default value "Hyderabad" will be used.
Returning Multiple Values
• The Python return statement can return multiple values.
• Python allows a function to return multiple values.
• Listing 6.10 defines a function that takes two numbers and returns them in
ascending order.
Function Abstraction and Stepwise Refinement
• Function abstraction is achieved by separating the use of a function from its
implementation.
• The key to developing software is to apply the concept of abstraction. You will learn
many levels of abstraction from this book.
• Function abstraction separates the use of a function from its implementation. A
client program, called simply the client, can use a function without knowing how it is
implemented. The details of the implementation are encapsulated in the function
and hidden from the client that invokes the function. This is known as information
hiding or encapsulation. If you decide to change the implementation, the client
program will not be affected, if you do not change the function header.
• The implementation of the function is hidden from the client in a “black box,” as
shown in Figure 6.5.

• You have already used many of Python’s built-in functions; you used these in client
programs.
• You know how to write the code to invoke these functions in your program, but as a
user of these functions, you are not required to know how they are implemented.
• The concept of function abstraction can be applied to the process of developing
programs.
• When writing a large program, you can use the “divide-and-conquer” strategy, also
known as stepwise refinement, to break down the problem into subproblems. The
subproblems can be further divided into smaller, more manageable ones.

You might also like