0% found this document useful (0 votes)
8 views21 pages

AADAAR

The document provides an overview of functions in programming, including types such as built-in and user-defined functions. It discusses the advantages of using functions, how to define and call them, and the concepts of local and global variables. Additionally, it covers different argument types, recursion, and lambda functions, illustrating their usage with examples.

Uploaded by

ramanamora18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

AADAAR

The document provides an overview of functions in programming, including types such as built-in and user-defined functions. It discusses the advantages of using functions, how to define and call them, and the concepts of local and global variables. Additionally, it covers different argument types, recursion, and lambda functions, illustrating their usage with examples.

Uploaded by

ramanamora18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Function

Functions are subprograms which are used to compute a value or


perform a task.

Type of Functions:
• Built-in Function

Ex: - print( ), upper( ) etc

• User-defined Function
Advantage of Function

Write once and use it as many times as you need. (code reusability).
Function facilitates ease of code maintenance.
Divide Large task into many small task so it will help you to debug code
You can remove or add new feature to a function anytime.
Function Definition
Define a function using def keyword followed by function name with parentheses.
This is also called as Creating a Function, Writing a Function, Defining a Function.
Syntax :
def Function_name ( ):
Local Variable
block of statement
return (variable or expression) Function Body
Syntax :
def Function_name (para1, para2, …):
Local Variable
block of statement
Function Body
return (variable or expression)
Function Definition
parentheses describes this is a
def represents starting function not variable or other object
of function definition
: describes beginning
of function body
def add () :
x = 10
Local Variable
Name of Function y = 20
Function Body
c=x+y
print(c) Statements
Function Definition
parentheses describes this is a
def represents starting function not variable or other object
of function definition
Parameter : describes beginning
of function body
def add (y) :
x = 10 Local Variable
Name of Function c=x+y Function Body
Statements
print(c)
Calling a function
A Function runs only when we call it, function can not run on its own.

Syntax:

function_name ( )

function_name (arg1, arg2, …)

Ex:

add ( )

add (20)

add(10.56)
Return Statement

Return statements can be used to return something from the function. In

Python, it is possible to return one or more variables/values.

def add (y) : def add_sub (y) :


x = 10 x = 10
c=x+y c=x+y
return c d=y-x
sum = add (20)
return c, d
print(sum) sum, sub = add (20)
print(sum)
print(sub)
Types of function definitions

We can write functions by 4 ways based on the arguments and return type.

1. With Arguments and With Return Type

2. With Arguments and With out Return Type

3. With out Arguments and With Return Type

4. With out Arguments and With out Return Type


Local Variables
• The variable which are declared inside a function called as Local Variable.
• Local variable scope is limited only to that function where it is created.
• local variable value is available only in that function not outside of that
function.
def add (y) : Local Variable
x = 10
print(x)
Using Local variable inside Function
print(x + y)
add(20) Using Local Variable outside function, it will show error
print(x)
Global Variables
• When a variable is declared above a function, it becomes global variable.
• These variables are available to all the function which are written after it.
• The scope of global variable is the entire program body written below it.
a = 50
def show () :
Global Variable
x = 10
print(a) Local Variable
print(x)
Using Global variable inside Function

show() Using Local variable inside Function


print(“x:”, x)
print(“a:”, a) Using Local Variable outside function, it will show error

Using Global variable


Actual and Formal Argument
• Formal Argument - Function definition parameters are called as formal
arguments
• Actual Argument - Function call arguments are actual arguments
Formal Arguments

def add (x, y) :


c=x+y
print(c)

add(10, 20)

Actual Arguments
Type of Actual Arguments
• Positional Arguments
• Keyword Arguments
• Default Arguments
• Variable Length Arguments
• Keyword Variable Length Arguments
Positional Arguments
• These arguments are passed to the function in correct positional order.
• The number of arguments and their positions in the function definition
should be equal to the number and position of the argument in the function
call.

def pw (x, y) : def pw (x, y) : def pw (x, y) :


z = x**y z = x**y z = x**y
print(z) print(z) print(z)

pw(5, 2) pw(2, 5) pw(5, 2, 3)


Keyword Arguments
These arguments are passed to the function with name-value pair so keyword
arguments can identify the formal argument by their names.
The keyword argument’s name and formal argument’s name must match.

def show (name, age) :


print(name, age)

show(name=“Anand”, age=27)

def show (name, age) :


print(name, age)

show(age=27, name=“Anand”)
Default Arguments
If we do not provide actual argument for formal argument explicitly while
calling the function then formal argument will use default value on the other
hand if we provide actual argument then it will use provided value

def show (name, age=27) :


print(name, age)

show(“Anand”)
show(“Anand”, 35)
Variable Length Arguments

Variable length argument is an argument that can accept any number of values.
The variable length argument is written with * symbol.
It stores all the value in a tuple.

def add (*nums) : def add (x,*nums) :


print(x)
for num in nums:
for num in nums:
print(num)
print(num)
add(5, 2, 4)
add(5, 2, 4)
Keyword Variable Length Arguments
 Keyword Variable length argument is an argument that can accept any
number of values provided in the form of key-value pair.
 The keyword variable length argument is written with ** symbol.
 It stores all the value in a dictionary in the form of key-value pair.

def add (**kwargs) :


for k, v in kwargs.items():
print ("%s == %s" %(k, v))

add(a=5, b=2, c=4)


Recursion
A function calling itself again and again to compute a value is referred to
Recursive Function or Recursion.
def fact(n):

if(n==0):
return 1

return n * fact(n-1)

result = fact(5)

print(result)
Lambda Function

 A function without a name is called as Anonymous Function. It is also


known as Lambda Function.
 Anonymous Function are not defined using def keyword rather they are
defined using lambda keyword.
Creating a Lambda Function
Syntax:-
lambda argument_list : expression
Ex:- : represents beginning of the function
Argument List Expression

lambda x, y : x + y

lambda keyword represents an


anonymous Function is being created
Calling Lambda Function

sum = lambda x : x + 1
sum(5)

add = lambda x, y : x + y
add(5, 2)

You might also like