0% found this document useful (0 votes)
7 views11 pages

Function

The document explains the concept of functions in Python, highlighting their importance in organizing code and improving reusability. It covers how to define and call functions, the types of functions (built-in, user-defined, lambda, and recursive), and the different types of function arguments (positional, keyword, default, and variable length). Additionally, it discusses global and local variables, emphasizing their scope and accessibility within a program.

Uploaded by

anil
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)
7 views11 pages

Function

The document explains the concept of functions in Python, highlighting their importance in organizing code and improving reusability. It covers how to define and call functions, the types of functions (built-in, user-defined, lambda, and recursive), and the different types of function arguments (positional, keyword, default, and variable length). Additionally, it discusses global and local variables, emphasizing their scope and accessibility within a program.

Uploaded by

anil
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

Function

A function in Python is a block of organized and reusable code that performs a specific task. It helps
to divide a large program into smaller and manageable parts. Functions improve code reusability and
make programs easier to understand and maintain. A function in Python is a group of statements
written together to perform a specific task. It allows the same code to be used many times in a program
without writing it again and again.

Concept

In Python, instead of writing the same code again and again, we can create a function once and use it
many times whenever needed. A function can take input values called arguments or parameters,
perform some operations, and return a result.

Fig:1.1 Functions

Defining a Function

Concept

Defining a function in Python means creating a function to perform a specific task using the def
keyword. When a function is defined, the programmer writes instructions inside the function body
that will execute whenever the function is called. The function definition includes the function name,
parameters (if required), and statements that describe what the function will do. Defining functions
helps in organizing programs and avoids repeating the same code multiple times.
Important Points

 A function is defined using the def keyword.


 Function name is written after def.
 Parameters or arguments can be written inside parentheses.
 A colon (:) is used after parentheses.
 The function body must be properly indented.
 A function may return a value using the return statement.

Syntax

def function name(parameters):


statements
return value

Example

def greet(name):
print ("Hello", name)

In this example:

 greet is the function name.


 name is the parameter.
 The function prints a greeting message.
Calling a Function

Concept

Calling a function means executing the function after it has been defined. When a function is called,
the program control moves to the function body, executes all the statements written inside it, and then
returns back to the main program. If the function requires arguments, they must be passed during the
function call. A function can be called many times in a program whenever needed.

Important Points

 A function must be defined before calling it.


 Calling is done using the function name followed by parentheses.
 Arguments are passed inside parentheses if required.
 A function can be called multiple times.
 After execution, control returns to the main program.

Example

def greet(name):
print("Hello", name)

greet("Ram")
greet("Sita")

Output

Hello Ram
Hello Sita

Here:

 The function is called two times.


 Different arguments produce different outputs.
Types of Functions in Python

Concept (Paragraph)

Functions in Python are divided into different types based on how they are created and used. Some
functions are already provided by Python for common tasks, while others are taken from modules or
created by programmers according to their needs. Using different types of functions helps to reduce
programming effort and makes programs easy to understand and manage.

1. Built-in Functions

Definition

Built-in functions are functions that are already available in Python. They are automatically loaded
when the Python interpreter starts.

Concept

These functions perform common operations such as printing output, calculating length, converting
data types, and performing mathematical operations. Programmers can directly use them without
defining or importing anything.

Important Points

 Already defined in Python.


 No need to create or import.
 Easy and fast to use.

Examples

print()
len()
sum()
int()

Example Program
print("Hello Python")
print(len("Programming"))

2. Functions Defined in Built-in Modules

Definition

Functions available inside Python modules are called module functions. A module contains related
functions grouped together.

Concept

These functions are not directly available. The programmer must import the module before using
them. Modules help organize large libraries into categories.

Important Points

 Module must be imported before use.


 Used for special operations such as mathematics and random number generation.

Example
import math
print([Link](36))

Random module example:

import random
print([Link](1,10))

3. User Defined Functions

Definition

Functions created by programmers according to their requirements are called user-defined


functions.

Concept

These functions allow customization and help divide large programs into smaller manageable parts.

Important Points

 Created using def keyword.


 Can accept arguments.
 Can return values.

Example Program
def add(a,b):
return a+b

print(add(5,3))
1. User-Defined Functions

Definition

User-defined functions are functions created by programmers according to their own requirements.

Concept

When built-in functions are not enough to solve a problem, programmers create their own functions
using the def keyword. These functions help divide a large program into smaller parts.

Important Points

 Created using def keyword.


 Can accept arguments.
 Can return values.
 Used for customized tasks.

Example
def greet(name):
print("Hello", name)

greet("Ram")

Output:

Hello Ram

2. Built-in Functions

Definition

Built-in functions are already available in Python. They are automatically loaded when Python
starts.

Concept

These functions perform common operations such as printing output, calculating length, converting
data types, etc.

Important Points
 No need to define.
 Easy and fast to use.
 Available directly in Python.

Examples

print()
len()
sum()
int()

Example Program
print(len("Python"))

Output:

3. Lambda Functions

Definition

A lambda function is a small anonymous function that is defined without using a name.

Concept

Lambda functions are used when a function is required for a short time or simple operation. They
are written in a single line.

Important Points

 Created using lambda keyword.


 No function name.
 Used for short operations.

Syntax
lambda arguments : expression

Example
square = lambda x : x*x
print(square(5))

Output:

25
4. Recursive Functions

Definition

A recursive function is a function that calls itself repeatedly until a condition is satisfied.

Concept

Recursion is useful for solving problems that can be divided into smaller similar problems such as
factorial or Fibonacci series.

Important Points

 Function calls itself.


 Must have a stopping condition.
 Used in mathematical problems.

Example
def factorial(n):
if n == 1:
return 1
return n * factorial(n-1)

print(factorial(5))

Output:

120
Types of Function Arguments

1. Positional Arguments

Arguments passed in the same order as defined.

Example:

def add(a,b):
print(a+b)

add(5,3)

2. Keyword Arguments

Arguments passed using parameter names.

Example:

def info(name,age):
print(name,age)

info(age=20,name="Ram")

3. Default Arguments

Arguments having default values.

Example:

def info(name,age=18):
print(name,age)

info("Ravi")

Output:

Ravi 18

4. Variable Length Arguments

Used when number of arguments is unknown.

Example:

def total(*num):
print(sum(num))

total(1,2,3,4)
Global Variables

Definition

A global variable is a variable declared outside all functions and can be accessed anywhere in the
program.

Concept (Paragraph)

Global variables are created outside the function body. They remain active throughout the program
execution and can be used by multiple functions. These variables are useful when many parts of a
program need the same data.

Important Points

 Declared outside functions.


 Accessible inside and outside functions.
 Exist during entire program execution.

Example
x = 10

def show():
print(x)

show()

Output:

10

Local Variables

Definition

A local variable is a variable declared inside a function and can only be used within that function.

Concept (Paragraph)

Local variables are created when a function starts execution and destroyed when the function ends.
They cannot be accessed outside the function. Local variables help avoid conflicts between
variables having the same name.

Important Points
 Declared inside a function.
 Accessible only within that function.
 Exists only during function execution.

Example
def demo():
y = 5
print(y)

demo()

Output:

5
Difference Between Global and Local Variables

Global Variable

 Declared outside function.


 Accessible everywhere.

Local Variable

 Declared inside function.


 Accessible only inside that function.

You might also like