0% found this document useful (0 votes)
17 views4 pages

Python Functions: Definitions & Examples

The document explains the concept of functions in Python, including how to define, call, and pass arguments to functions. It covers various types of arguments such as arbitrary, keyword, and default arguments, as well as generator functions, variable scope, lambda functions, and recursion. Examples are provided to illustrate each concept clearly.

Uploaded by

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

Python Functions: Definitions & Examples

The document explains the concept of functions in Python, including how to define, call, and pass arguments to functions. It covers various types of arguments such as arbitrary, keyword, and default arguments, as well as generator functions, variable scope, lambda functions, and recursion. Examples are provided to illustrate each concept clearly.

Uploaded by

durgahs177
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Page 1

Functions : Program to illustrate concepts functions


# A function is a block of code which only runs when it is called. You can pass data, known as
arguments, into a function. A function can return data as a result.

DEFINING A FUNCTION
# In Python a function is defined using the def keyword. The indented block of code will be
executed when the function is called.
def func():
print('hello world')

# A function can take some arguments that can be used in the block of code that is executed.
def add(a, b):
print(a+b)

# A function can be made to return values using the return keyword.


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

CALLING A FUNCTION
# A function can be called using its name.
def func():
print('hello world')
func() # The function is called
hello world
def add(a, b):
print(a+b)
add(4, 5) # The function is called with some arguments
9
def add(a, b):
return a+b
c = add(1, 2)
# The function is called and the returned value is stored in a variable print(c)
3

MORE ON FUNCTION ARGUMENTS


Arbitrary Arguments

# In our functions definitions before, the number of arguments of the function was fixed. However it is
also possible to define a function so that any number of arguments can be passed to it. This is done by
adding * before the argument name in the function definition. This way the function will receive a tuple of
arguments, and can access the items accordingly.
def func(*args):
for x in args:
print(x)
func(1)
func(4,5)
Page 2

1
4
5

Keyword Arguments

# Arguments can also be sent to the function with the key = value syntax. This way the order of the
arguments does not matter. The arguments sent this way are called keyword arguments.

def func(a, b):


print(a)
print(b)
func(b='world', a='hello')
hello
world

Arbitrary Keyword Arguments

# It is possible to define a function so that any number of keyword arguments can be passed to it. This is
done by adding ** before the argument name in the function definition. This was the function will
receive a dictionary of arguments, and can access the items accordingly.
def func(**args):
print(args['brand'])
func(brand='Ford', model='Mustang')
Ford

Default Argument Value

# A function with an argument can be defined so that it automatically gives some default value to the
argument if none is passed to it.
def func(a=1):
print(a)
func(2)
func()
2
1

GENERATOR FUNCTIONS
# Generator functions are functions with yield statement instead of return. Unlike normal functions,
generator functions will return a generator object, and won't execute immediately. The function will
execute till the first yield statement when next() is called on the object, and pause. It will execute again till
the next yield statement if next() is called again on the object, and pause again, and so on.
def func(n): yield
n yield n*n
obj = func(2) # obj is a generator object
print(type(obj))
print(next(obj))
print(next(obj))
<class 'generator'> 2
4
def func(): yield
1
yield 2
yield 3
Page 3

for x in func(): # generator objects are iterable


print(x)
1
2
3
def func(n): yield
n yield n*n
for x in func(3): # generator objects are iterable
print(x)
3
9

# List comprehensions also create generator objects when enclosed within (). Example:
a = (x for x in [1, 2, 3])
print(type(a)) print(next(a))
print(next(a)) print(next(a))
# calling next() once more on a will give an error
<class 'generator'> 1
2
3
SCOPE OF VARIABLES
# The scope of a variable is the region of code in which a defined variable is accessible. Outside of the scope
of the variable, it will be undefined.

# A variable that is defined inside a function cannot be accessed outside of the function. These are called
local variables. The example below will throw an error:
def func():
a=5
print(a)
# The above code will throw an error because a is a local variable defined inside of func(). It is undefined
outside of the definition of func(), and hence it will be as if we are calling a variable that was never even
created.

# However variables that are created outside the function definition can be accessed from within the
function. These are called global variables. Example:
a=5
def func():
print(a)
func()
5

If a local variable in a function definition has the same name as a global variable defined outside of the
function, then for the scope of the function definition, the name will refer to the local variable. The global
variable won't be modified. Example:
a = 10
def func():
a=5
print(a)
print(a)
5
10

Although by default, a variable created inside a function definition will be a local variable, it is also possible
to define a variable with global scope inside a function using the global keyword.
def func():
global a
Page 4

a= 5
func()
print(a)
5
In such a case it is necessary for the function to be called for the variable to be created. The
below code will throw an error:
def func():
global a
a=5
print(a)

LAMBDA FUNCTIONS
A lambda function or an anonymous function is a short hand way of defining a function in python. A
lambda function can take any number of arguments, but can only have one expression.
The general syntax for a lambda function is given below:
lambda arguments : expression

Lambda functions create objects (of class function) that act as functions.
x = lambda a : a + 10
print(type(x))
print(x(5))
<class 'function'> 15
x = lambda a, b: a + b
print(x(7,8))
15

Since lambda functions are objects, they can even be returned by a function. So it will be like the return value
of a function is another function. Example:
def func(n):
return lambda a : a*n
double = func(2)
triple = func(3)
print(double(5)) # will multiply 5 by 2 and print print(triple(5)) # will
multiply 5 by 3 and print
10
15

RECURSION
It is possible to call a function in its own definition. When done so it is called recursion.
def factorial(n):
return n*factorial(n-1) if n>1 else 1 print(factorial(5))
120

# Another Example (function that returns the nth Fibonacci number):


def fib(n):
return fib(n-1)+fib(n-2) if n>1 else n print(fib(10))
55

You might also like