Python Functions
Dr M Chandralekha
Assistant Professor (Sr. Grade)
Dept. of CSE
AVV, Chennai
Agenda
• Introduction to Functions
• Syntax of a Function
• Defining Functions and Parameters
• Default Parameters
• Return Values
• Scope of Variables
• Types of Functions
• Python Functions are a block of statements that does a specific task.
• The idea is to put some commonly or repeatedly done task 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 over and over again
• Suppose we need to create a program to make a circle and color it.
• We can create two functions to solve this problem:
function to create a circle
function to color the shape
• Dividing a complex problem into smaller chunks makes our program
easy to understand and reuse.
Example
Adding Multiple Items
The add_to_cart() function is reused for each item.
You don’t need to write separate code for each item
addition
Functions
• What is a Function?
• Definition: A block of code designed to perform a specific task.
• Purpose: Simplifies complex problems and allows code reuse.
• Benefits of Using Functions:
Code Reusability - Define once, use multiple times.
Modularity - Divides code into logical, manageable sections.
Easier Debugging - Isolates issues in specific functions.
Improved Readability - Organizes code for better understanding.
Encapsulation - Hides complex logic behind simple function calls.
• Why Use Functions?
• Reduces Repetition: Avoids redundant code.
• Better Organization: Groups tasks logically.
• Facilitates Collaboration: Multiple people can work on different functions.
• Easier Maintenance: Update code in one place.
Syntax of a Function
•def: Keyword used to define a function.
•function_name: The name you give to your
function (should be descriptive).
•parameters: Inputs for the function (optional).
These are variables passed into the function to work
with.
•Code block: The statements that are executed
when the function is called.
•return: Keyword to send back a value from the
function to where it was called (optional, but useful
if you want the function to give you a result).
Simple Function
•Function name: greet
•Parameters: None
•Body: print("Hello, World!") is the
code that runs when the function is
called.
Defining Functions & Parameters
• What Are Parameters and Arguments?
• Parameters: Variables listed inside the
parentheses in the function definition.
They act as placeholders for values that
the function will use when it's called.
• Arguments: The actual values or data that
you pass into a function when you call it.
These arguments are assigned to the
function's parameters.
After printing, the function finishes and implicitly returns None.
Example 1
Example 2
Explicit Return with Reuse
Implicit Return
Positional Arguments
• The most common way of passing
arguments to a function. The order
in which the arguments are passed
matters.
• Key Point: The first argument is
passed to the first parameter, the
second to the second parameter, and
so on. The position of the arguments
must match the position of the
parameters.
Default Parameters
• Parameters that assume a default value if
no argument is provided during the
function call. This allows functions to be
more flexible, as you can either provide a
specific argument or use the default.
Return Values
• What is a Return Value?
• Return Value: The value that a function sends
back to the part of the program that called it.
• Why Use return?
• It allows functions to produce a result or output
that can be used elsewhere in the program.
• The function ends immediately when a return
statement is executed.
• How to Use return in a Function
• The return statement is used to exit a function
and pass the result back to the caller.
Returning Multiple Values
• Python allows functions to return
multiple values using commas to
separate them. These values are
returned as a tuple.
• Why Return Multiple Values?
• Efficiency: You can calculate and return
several related values in one function call
rather than having to write multiple
functions for each.
• Example: For geometric calculations, a
function can return both the area and
perimeter of a shape at the same time,
reducing redundant code.
Returning Without a Value
• If no return statement is used, or if you use return
without a value, the function returns None by
default. This means the function finishes without
producing a value.
Scope of Variables
• What is Scope?
• Scope: Refers to the part of a program where a variable can be accessed or modified.
• Types of Scope:
• Local Scope: Variables declared inside a function.
• Global Scope: Variables declared outside of any function.
• Understanding variable scope is crucial for controlling how and where variables can be used in
your code.
Local Variables
• These are variables defined within a
function and are only accessible
within that function. Once the
function has finished executing, the
local variables are destroyed.
• Key Point: Variables defined inside a
function only exist within that
function. You can't access them
outside the function where they are
declared.
Global Variables
• These are variables defined outside
any function, and they can be
accessed from any part of the
program, including inside functions.
• Key Point: Global variables can be
accessed anywhere in the program,
both inside and outside functions.
Different Types of Functions in Python
• Built-in Functions
• These are pre-defined functions that come with Python.
• You can use them right away without defining them yourself.
• They save time and effort for common tasks.
• They are optimized for performance and simplify complex operations.
• User-defined Functions
• These are functions that you create to perform specific tasks in your program.
• Python allows you to define your own functions using the def keyword.
• They allow you to organize your code and reduce repetition.
• You can encapsulate logic specific to your program and reuse it easily.
Built-in Functions Examples
• print(): Displays output.
• len(): Returns the length of a string or other data types.
• sum(): Calculates the sum of a range of numbers.
• type(): Returns the data type of a value or variable.
User-defined Functions
Example#1: Write a function square(n) that returns the square of a given
number.
Example#2: Write a function is_prime(n) that checks if a number is
prime.
Thank You