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

Week-8Functions

The document provides an overview of problem-solving using Python programming, focusing on user-defined functions. It covers the definition, types, and syntax of functions, including built-in, user-defined, and anonymous (lambda) functions, as well as the concept of variable scope. Additionally, it discusses the need for functions, their benefits, and introduces recursive functions with examples.

Uploaded by

valindavijay02
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)
2 views

Week-8Functions

The document provides an overview of problem-solving using Python programming, focusing on user-defined functions. It covers the definition, types, and syntax of functions, including built-in, user-defined, and anonymous (lambda) functions, as well as the concept of variable scope. Additionally, it discusses the need for functions, their benefits, and introduces recursive functions with examples.

Uploaded by

valindavijay02
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/ 38

GE19241

PROBLEM SOLVING USING PYTHON


PROGRAMMING

Python-User
Defined Functions

WEEK 07
CONTENTS
01 02 03
Types of Functions User defined Functions Functions and return
statements

04 05
Functions and Arguments Variable and its scope
with examples
Functions
• A function is a group of statements(block of code) that exist within a program for the
purpose of performing a specific task.

Here are simple rules to define a function in Python.

• Function blocks begin with the keyword def ,It will be followed by the function name and
parentheses ( ( ) ).

• Any input parameters or arguments should be placed within these parentheses. You can also
define parameters inside these parentheses.

• The code block within every function starts with a colon (:) and is indented.

• The statement return [expression] exits a function.


Need for Functions

• Functions allow the same piece of code to run multiple times


• Functions break long programs up into smaller components
• Functions can be shared and used by other
programmers(written once by anyone ,can be re-used many
times by anyone)
Types of Function

There are three types of functions in Python:


 Built-in functions The Python interpreter has a number
of functions built into it that are always available.
 User-Defined Functions (UDFs): The Functions defined
by User is known as User Defined Functions.
 Anonymous functions, which are also called lambda
functions because they are not declared with the standard
def keyword.
Built-in Function
Syntax of User defined
Function
Syntax of User defined
Function

Function-Syntax
Default Function
Default Function
Default Function
Default Function
Sample Problems-
Workout
Functions with
parameters
Functions with
return types
User Defined Function
Functions with
return types
Functions and return statements
Examples

What is the output of the below code snippet?

B will be printed
Functions with 2
return types
Returning more than one value

Example
Types of functions
with arguments
Functions and arguments
Types of functions
with arguments
Functions and arguments
Types of functions
with arguments
Functions and arguments
Types of functions
with arguments
Functions and arguments
Variables and
Scope
Global variables are created when the program
execution starts(outside function) and remains in
Variable and its scope
memory till the program terminates.

Local variables are created inside a function and remains


in memory till the function terminates.

In cases where a global variable needs to be modified


inside a function, Python allows you to do that using the
global keyword.
Anonymous Function

• In Python, an anonymous function is a function that


is defined without a name.
• While normal functions are defined using
the def keyword in Python, anonymous functions are
defined using the lambda keyword.
• Hence, anonymous functions are also called lambda
functions.
Lambda Function
Example

• double = lambda x: x * 2
• print(double(5))

• Output =2
Recursive Functions
Example

Consider 5! = 5x4x3x2x1
can be re-written as 5!=5x4!
In general n! = nx(n-1)!
factorial(n) = n * factorial(n-1)
Recursive Functions
Examples
Example
Example
Example
Sample Problems-Workout
Exercise program
Sample Problems-Workout
Department of Computer Science and
38
Engineering

You might also like