0% found this document useful (0 votes)
13 views

Function in Python

Functions allow for code reusability in Python. There are pre-defined functions like input() and user-defined functions created using the def keyword. Functions can take parameters (arguments) and return values. Default arguments can be specified for parameters. Positional and keyword arguments determine how values are passed to parameters. Variable-length arguments allow functions to take a variable number of arguments. Recursion involves a function calling itself to solve problems. Lambda expressions are anonymous one-line functions defined using the lambda keyword.

Uploaded by

Rahul Das
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Function in Python

Functions allow for code reusability in Python. There are pre-defined functions like input() and user-defined functions created using the def keyword. Functions can take parameters (arguments) and return values. Default arguments can be specified for parameters. Positional and keyword arguments determine how values are passed to parameters. Variable-length arguments allow functions to take a variable number of arguments. Recursion involves a function calling itself to solve problems. Lambda expressions are anonymous one-line functions defined using the lambda keyword.

Uploaded by

Rahul Das
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

FUNCTION IN PYTHON

By Asst Prof Rahul Das


Function
 A Function is a named block of code which can be
reused.
 Syntax:

def function_name():
Statement
Statement
Statement
 We must call a function in order to run it.

 The main advantage of function is code reusability.


Types of Function

Function

Pre-defined
User-defined
Eg-input()

Takes Nothing Takes Nothing Takes Something Takes Something


Returns Nothing Returns Returns Nothing Returns
Something Something
Arguments or Parameters
 In Python, Parameter refers to the information
passed to the function. Parameters are also known
as arguments.
 Typically, Parameters are of two types – Formal
Parameters, Actual Parameters
 Formal Parameters are the parameters which are
specified during the definition of the function.
 Actual Parameters are the parameters which are
specified during the function call.
Default Arguments
 When you define a function, you can specify a
default value for each parameter.
 When you call a function and pass an argument to
the parameter that has a default value, the function
will use that argument instead of the default value.
 However, if you don’t pass the argument, the
function will use the default value.
 To use default parameters, you need to place
parameters with the default values after other
parameters. Otherwise, you’ll get a syntax error.
Default Argument (Example)
def sum(a,b=0,c=0)
s=a+b+c
print(“Sum:”,s)
sum(10)
sum(10,20)
sum(10,20,30)
Positional and Keyword Arguments
 A positional argument means its position matters in
a function call.
 A keyword argument is a function argument with a
name label. Passing arguments as keyword
arguments means order does not matter.
 Positional argument cannot follow after a Keyword
argument
Variable-length arguments
 Variable-length arguments, varargs for short, are
arguments that can take an unspecified amount of
input.
 We use * operator in-front of a formal argument.
This formal argument will be of type “tuple”.
 While using varargs and single args, if varargs
preceeds single args in argument list. Then while
calling the function, single args must be passed as
keyword arguments.
Variable-length arguments (Example)

def avg(*n) #here n is a tuple


s=0
for x in n:
s+=x
return s/len(n)
Recursion
 When a function calls itself it is called recursion.
 The concept of recursion is used to solve Recurrence
relation problem.
 In recursion, solution of a problem is defined with
the help of the result of simpler version of the same
problem.
 Advantage: Easy to code and read.
 Dis-advantage: Space complexity problem.
Exercise
 Write a recursive function to calculate the factorial
of a number.
Lambda Expression or Anonymous
Function
 A function without any name is called Lambda
expression or Anonymous function.
 We use lambda keyword to define an anonymous
function.
 Unlike normal function, lambda expression are
restricted to a single line expression.
 Syntax: lambda arguments: single_expression
Exercise
 Write a Lambda expression to find the factorial of
a number using recursion.

You might also like