Search . . .
Working with
User-Defined
Functions
Session-9
simrantechmentor
Current Stage Search . . .
• Our code is getting complex
• We find it difficult to manage and understand
our code.
• In some situation we are writing same code at
different places again and again.
simrantechmentor
Search . . .
Solution :- Functions
A function is a block of code that performs a
specific task.
simrantechmentor
Functions- Management of Search . . .
code
Function help us to divide a program into smaller
chunks that makes our program easier to understand
and manage.
Suppose you have to create a program for bill
calculation. You can create three functions to solve
this problem.
• Accept User inputs (Product name, price , quantity)
• Calculate Bill
• Display Receipt
simrantechmentor
Functions- Reusability Search . . .
Once you have created a function you can call it as
many times as you need. You don’t need to write that
code again.
For example, you are creating a banking application
where you have implemented code for checking
existence of bank account. You would need this
functionality many times in application.
• Before deposit money
• Before withdraw money
• Before approving any service request
simrantechmentor
Type of Functions Search . . .
Standard Library Functions : Built-in functions that are
available to use. For example : print(), input(),
randint(), len()
User-defined Functions:- We can create our own
functions based on our requirements.
simrantechmentor
Python Function Syntax Search . . .
simrantechmentor
Implementation Search . . .
Define a Function :- Specify what the
function will do
Call a Function :- Execute functionality
simrantechmentor
Demonstration Function Syntax Search . . .
Program function_syntax.py (Folder In-class Demo) Lab 9
Demonstration Function Sequence Search . . .
Program function_sequence.py (Folder In-class Demo) Lab 9
Demonstration Function Argument Search . . .
Program function_argument.py (Folder In-class Demo) Lab 9
Demonstration Function Argument Search . . .
Program function_argument_2.py (Folder In-class Demo) Lab 9
Demonstration Function Return Search . . .
Value
Program function_return_value.py (Folder In-class Demo) Lab 9
Function Arguments Search . . .
• Positional Arguments: These arguments are matched based on their positions when calling the
function. The order matters.
• Keyword Arguments: These arguments are matched based on their names when calling the
function. The order doesn't matter.
• Default Arguments: You can provide default values for function parameters. If no value is
passed for these parameters, the default values will be used.
• Arbitrary Arguments: You can define a function that accepts an arbitrary number of arguments
using *args or **kwargs (keyword arguments).
simrantechmentor
Demonstration Function Positional Arguments Search . . .
Program function_positional.py (Folder In-class Demo) Lab 9
Demonstration Function Keyword Arguments Search . . .
Program function_keyword.py (Folder In-class Demo) Lab 9
Demonstration Function Default Arguments Search . . .
Program function_default.py (Folder In-class Demo) Lab 9
Demonstration Function Arbitary Arguments Search . . .
Program function_arbitary.py (Folder In-class Demo) Lab 9
Function Scope Search . . .
• In Python, the scope of a variable defines where the variable is
accessible or can be referenced within the code.
• The concept of scope is essential to understand because it determines
the visibility and lifetime of variables in different parts of your program.
simrantechmentor
Function Scope Search . . .
• Global Scope: Variables defined outside of any function have a global
scope. These variables can be accessed from anywhere in the code,
including inside functions.
• Local Scope: Variables defined inside a function have a local scope.
They are only accessible within that specific function and cannot be
accessed from outside the function.
simrantechmentor
Demonstration Function Local Search . . .
Program function_local.py (Folder In-class Demo) Lab 9
Demonstration Function Global Search . . .
Program function_global.py (Folder In-class Demo) Lab 9
Demonstration Function Modify global Search . . .
Program function_modify_global.py (Folder In-class Demo) Lab 9
Search . . .
THANK YOU
Kindly perform all in class exercise before coming to lab
END SLIDE
simrantechmentor
Contact No : 9878138788
Email : [email protected]