Python Functions:

A function is a block of code to perform a specified task.

In this context, there is a function call and there is a function definition.

When a function is calling then the function definition is executing.

We can pass data known as parameters(arguments) into a function.

At the time of function call whatever we take arguments, is known as actual arguments and at the time of function call, we take arguments is known as formal arguments.

Here actual arguments equals to the formal arguments.

A function can return a value as result.

In python a function is defined by using def keyword.



Example(without arguments):

def show(): #function definition
	print('python means SILAN Software')
show()   #function call

Output:
python means SILAN Software

Example(with arguments)

def area(base,height):      #function definition
	a=0.5*base*height
		print("area of triangle is:",a)
area(4.5,5.6)     #function call

Output:
area of triangle is: 12.6


Passing a List as an argument

We can take any data types of argument to a function like string, number, list, dictionary etc., and it will be treated as the same data type inside the function.

For example, if we take a List as an argument, it will still be a List when it reaches the function:

Example

def fruits_detail(items):
	for k in items:
		print(k)
fruits = ["apple", "banana","orange", "cherry"]
fruits_detail(fruits)

Output
apple
banana
orange
cherry


Return Values

A function returns a value explicitly by using return statement.

Example

def calculate(x):
	return 7* x
print(calculate(2))
print(calculate(3))
print(calculate(4))

Output:
14
21
28


pass statement

function definitions cannot be empty, when a function definition having no content, then we write pass statement to avoid getting an error.

def show( ):
pass


Recursion

In general if something is defined in terms of itself is known as recursion. For example, we find the factorial of a number by taking the concept of factorial.

Python also accepts function recursion, which means a defined function call itself. That means when a function is calling to itself, known as recursive function.


#Python program to find the factorial of a number using Recursion
def factorial(n):
	if n==0:
		return 1
	elif n==1:
		return 1
	else:
		return n * factorial(n-1)
x=int(input("enter the number"))
print("factorial is:",factorial(x))

Output
enter the number5
factorial is: 120


#Python program to generate a Fibonacci series using recursion
def fibonacci(n):
	if n<=1:
		return n
	else:
		return fibonacci(n-1)+fibonacci(n-2)
no_of_terms=int(input("enter the no.of terms"))
if no_of_terms<=0:
	print("please enter a positive integer")
else:
	print("fibonacci sequence is:")
	for k in range(no_of_terms):
		print(fibonacci(k))

Output
enter the no.of terms7

fibonacci sequence is:
0
1
1
2
3
5
8


Scope and Lifetime of variables

A variable scope is the portion of a program where the variable is recognized. Variables defined inside a function is not visible from outside. Hence, they have a local scope.
Lifetime of a variable is the period throughout which the variable exits in the memory. The lifetime of variables inside a function is as long as the function executes.
They are destroyed once we return from the function.
Here is an example that I have presented the scope of a variable inside a function.

Def show():
    x = 100
    print("Value inside function:",x)

x = 200

show()

print("Value outside function:",x)

Output
Value inside function: 100
Value outside function: 200

Here, we can see that the value of x is 200 initially. Even though the function show()changed the value of x to 100, it did not effect the value outside the function.
Here the variable x inside the function is different (local to the function) from the one outside. Although they have same names, they are two different variables with different scope.
On the other hand, variables outside of the function are visible from inside. They have a global scope.
We can read these values from inside the function but cannot change (write) them. If we want to modify the value of variables outside the function, they must be declared as global variables using the keyword global.


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext