0% found this document useful (0 votes)
10 views41 pages

Lecture 4 Functions

Uploaded by

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

Lecture 4 Functions

Uploaded by

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

Application Development (CSF 510)

Lecture 4 : Functions
Lecture Contents
• Introduction
• Python Syntax to define and call functions
• Void and value returning functions
• Flow of execution
• Variable scope , local and global variables
• Arguments and parameters
• Types of arguments
• Return statement
• Recursion
• Lambda functions

Lecture 4: Functions Application Development 2


Introduction
• What is a function
• In the context of programming, a function is a named sequence of statements that
performs a desired operation. This operation is specified in a function definition.
• What is the objective?
• The objective is to break a large complex structure/program into smaller manageable
units that can work independently
• Often referred to as “ Divide and Conquer “ Approach
• Divide a large problem into smaller sub problems
• This will make the task easier to manage

Lecture 4: Functions Application Development 3


Divide and Conquer – Using Functions

Lecture 4: Functions Application Development 4


Example – Payroll Computation

Lecture 4: Functions Application Development 5


Functions- Payroll Computation

Lecture 4: Functions Application Development 6


Defining a Function - Syntax

Lecture 4: Functions Application Development 7


Defining a Function- Syntax

Lecture 4: Functions Application Development 8


Void and Value Returning Functions

• Void functions
• Function which do not return a value
• Just execute the statements it contains and terminates
• Value Returning Function
• Executes the statements it contains and returns a value back to the statement/program
which called it.
• When you call the input function, it gets the data that the user types on the keyboard and returns
that data as a string.
• The int and float functions are also examples of value-returning functions.
• You pass an argument to the int function, and it returns that argument’s value converted to an integer

Lecture 4: Functions Application Development 9


Function Definition – Value Returning
Functions
Indentation is important and
necessary

Lecture 4: Functions Application Development 10


Flow of Execution

Lecture 4: Functions Application Development 11


Flow of Execution
• What will be the output?

Lecture 4: Functions Application Development 12


Flowchart Symbol

Function Symbol :a rectangle that has vertical bars at each side


When drawing a flowchart for a function, the starting terminal
symbol usually shows the name of the function and the ending
terminal symbol usually reads Return.
Lecture 4: Functions Application Development 13
Functions without Body

Lecture 4: Functions Application Development 14


Scope of a Variable
• The scope of a variable in Python is the region of the program where the
variable can be accessed.
• Variables can have different scopes, depending on where they are defined.
• There are three main scopes in Python:
• Local scope: A variable defined within a function is local to that function and can only
be accessed from within that function.
• Global scope: A variable defined outside of any function is global and can be accessed
from anywhere in the program.
• Enclosing scope: A variable defined in an outer function is enclosed by a nested
function and can be accessed from within that nested function.

Lecture 4: Functions Application Development 15


Local Variables and Local Scope
• A variable defined inside a function body is called a local variable
• A local variable’s scope is the function in which the variable is created.
• A variable defined inside a different function cannot be accessed.
• A local variable cannot be accessed by code that appears inside the function
at a point before the variable has been created

Lecture 4: Functions Application Development 16


Local Variables

Lecture 4: Functions Application Development 17


Global Variables

Lecture 4: Functions Application Development 18


Local Variables vs Global Variables

Lecture 4: Functions Application Development 19


Passing Arguments & Parameters

A function should be called with correct number of arguments , otherwise we


will get an error
Lecture 4: Functions Application Development 20
Types of function arguments

Lecture 4: Functions Application Development 21


Arbitrary Arguments

Lecture 4: Functions Application Development 22


Arbitrary Positional Arguments

Lecture 4: Functions Application Development 23


Arbitrary Positional Arguments

Lecture 4: Functions Application Development 24


Arbitrary Keyword Arguments

Lecture 4: Functions Application Development 25


Return Statement

Lecture 4: Functions Application Development 26


Dead Code

Lecture 4: Functions Application Development 27


Recursion

Lecture 4: Functions Application Development 28


Recursion

Lecture 4: Functions Application Development 29


Recursion

Lecture 4: Functions Application Development 30


Problem Solving with Recursion

Lecture 4: Functions Application Development 31


Factorial –Using Recursion

Lecture 4: Functions Application Development 32


Factorial using Recursion – Execution
Flow

Lecture 4: Functions Application Development 33


Binary Search (HW)

Lecture 4: Functions Application Development 34


Merge Sort (HW)

Lecture 4: Functions Application Development 35


Lambda Functions
• Lambda functions are anonymous functions
• While normal functions are defined using the def keyword in Python, anonymous
functions are defined using the lambda keyword
• Lambda functions contain a single expression
• Can have any number of arguments
• Syntax

• Example

Lecture 4: Functions Application Development 36


Lambda Functions used in Higher Order
Functions
• A higher order function is a function which uses a function as an argument or
return a function as a return value
• Function that operate on other functions

Lecture 4: Functions Application Development 37


Using lambda functions with python
built-ins

Lecture 4: Functions Application Development 38


filter()

Lecture 4: Functions Application Development 39


map()

Lecture 4: Functions Application Development 40


Some built-in functions
• len()
• min()
• max()
• pow()
• sorted()
• Help()

Lecture 4: Functions Application Development 41

You might also like