Week-8Functions
Week-8Functions
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.
• 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.
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
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.
• 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