- Fruitful function is a special function in which function is defined in the return statement
- It is a function with return type as expression
- In fruitful function, the return statement includes a return value. This statement means: Return immediately from this function and use the following expression as a return value
- In a fruitful function, it is good idea to ensure that every possible path through the program hits a return statement
Example,
import math
def compute_area(radius):
return math.pi * radius ** 2
rad = eval(input("Enter radius"))
area = compute_area(rad)
print(" Area of circle =",area)
Output
Enter radius 5
Area of circle = 78.53981633974483
Example
Two return statements in a program
Sometimes, there is multiple return statements, one in each branch of a conditional.
def absoluteNum(Z):
if Z < 0:
return -Z
else:
return Z
n=eval(input("enter a number"))
a_n = absoluteNum(n)
print("Absolute Number:",a_n)
Output 1:
enter a number 5
Absolute Number: 5
Output 2:
enter a number -5
Absolute Number: 5
- Since these return statements are in an alternative conditional, only one will be executed
- As soon as one is executed, the function terminates without executing any subsequent statements
- Code that appears after a return statement or any other place, in which the flow of execution can never reach, is called dead code
Return values
A function returns a value, use the return statement
Syntax,
return expression
Example
return a – return value of 1 variable
return a,b – return values of 2 variables
return a,b,c – return values of 3 variables
return a+b – return value of the expression
return 8 – return value
Example,
import math
def compute_area(radius):
temp = math.pi * radius ** 2
return temp
rad = eval(input("Enter radius"))
area = compute_area(rad)
print("Area of circle =",area)
Output
Enter radius 5
Area of circle = 78.53981633974483
Function to compute distance between two points
def compute_dis(x1,y1,x2,y2):
dx=x1-x1
dy=y2-y1
sqr_dis=dx*dx + dy*dy
result = sqr_dis ** 0.5
return result
px1=eval(input("Enter x coorinate of point 1"))
py1=eval(input("Enter y coordinate of point 1"))
px2=eval(input("Enter x coordinate of point 2"))
py2=eval(input("Enter y coordinate of point 2"))
distance = compute_dis(px1,py1,px2,py2)
print("Distance between two points : ",distance)
Output
Enter x coorinate of point 1 2
Enter y coordinate of point 1 3
Enter x coordinate of point 2 4
Enter y coordinate of point 2 1
Distance between two points : 2.0
Parameters
- A parameter is the variable listed inside the parenthesis in the function definition
- If there is more than one value, then all are separated by comma
Local and Global Scope
- A variable is only available from inside the region it is created. This is called scope
Local Scope
A variable created inside a function belongs to the local scope of that function, and can only be used inside that function i.e. a variable created inside a function is available inside that function
def myfunc():
x = 200
print(x)
myfunc()
Output
200
Function within Function
A variable that is defined inside a function is not available outside the function but it is available inside the function that is within the outer function
def outerfunc():
x = 200
def innerfunc():
print(x)
innerfunc()
outerfunc()
Output
200
Global Scope
- A variable created in the main body of the python code is a global variable and belongs to global scope
- Global variables are available from within any scope global, local
- A variable created outside of a function is global and can be used by anyone
x = 200
def outerfunc():
print("Inside the function, value of x:",x)
outerfunc()
print("Outside the function, value of x:",x)
Output
nside the function, value of x: 200
Outside the function, value of x: 200
Naming Variables
If the same variable is used inside and outside a function, python will treat them as two separate variables, one available in the global scope(outside the function) and one available in the local scope (inside the function)
x = 200
def outerfunc():
x=10
print("Inside the function, value of x:",x)
outerfunc()
print("Outside the function, value of x:",x)
Output,
Inside the function, value of x: 10
Outside the function, value of x: 200
Function Composition
- Ability of calling one function from within another function is called function composition
- Function composition is the way of combining two or more functions in such a way that the output of one function becomes the input of the second function and so on
- For example: Let there be two function “F” and “G” and their composition can be represented as F(G(x)), where “x” is the argument and output of G(x) function will become the input of F( ) function
Example
import math
def compute_dis(x1,y1,x2,y2):
dx=x1-x1
dy=y2-y1
sqr_dis=dx*dx + dy*dy
distance = sqr_dis ** 0.5
return distance
def compute_area(radius):
temp = math.pi * radius ** 2
return temp
def area(xc,yc,xp,yp):
return compute_area(compute_dis(xc,yc,xp,yp))
px1=eval(input("Enter x coodrinate of centre"))
py1=eval(input("Enter y coordinate of centre"))
px2=eval(input("Enter x coordinate of perimeter"))
py2=eval(input("Enter y coordinate perimeter"))
ar = area(px1,py1,px2,py2)
print("Area ar=",ar)
Output,
Enter x coordinate of centre 2
Enter y coordinate of centre 3
Enter x coordinate of perimeter 4
Enter y coordinate perimeter 1
Area ar= 12.566370614359172
Explanation
- First the function compute_dis( ) is called with the inputs centres and perimeter of the circle
- The output given by compute_dis( ) is distance which is taken by compute_area( ) function which multiplies 3.14* radius * radius
Recursion
- A function that calls itself is known as a recursive function, and the phenomenon is known as recursion.
- Every recursive solution consists of two cases:
Base case
- Base case is the smallest instance of problem which can be solved easily and there is no need to further express the problem in terms of itself, i.e. in this case no recursive call is given and the recursion terminates
- Base case forms the terminating condition of the recursion
- There may be more than one base case in a recursive solution
- Without the base case, the recursion will never terminate and will be known as infinite recursion
Example
n==0 is the base case for factorial of a number
Recursive case
In recursive case, the problem is defined in terms of itself, while reducing the problem size
Example
when fact(n) is expresses as n*fact(n-1), the size of the problem is reduced from n to n-1
fact(n) = 1 when n = 0
fact(n) = n * fact (n-1) when n>0
Example
def factorial(n):
if n == 0:
return 1
else:
return n*factorial(n-1)
num = int(input("Enter a number: "))
if num < 0:
print("Factorial does not exist for negative numbers")
else:
print("The factorial of",num,"is",factorial(num))
Output,
Enter a number: 5
The factorial of 5 is 120
Advantages
- Easy solution for recursively defined problems
- Complex programs can be easily written in less code
Disadvantages
- Recursive code is difficult to understand and debug
- Terminating condition is must, otherwise it will go in infinite loop
- Execution speed decreases because of function call and return activity many times
- Recursive calls are expensive as they take up lot of memory ant time
Views: 1