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

functions 2

The document explains the concepts of fruitful and void functions, detailing how fruitful functions return values while void functions perform actions without returning any. It covers the importance of parameters, local and global scope, function composition, and recursion, providing examples for each concept. Additionally, it illustrates how to implement these concepts in programming through various examples, including calculating sums and factorials using recursion.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

functions 2

The document explains the concepts of fruitful and void functions, detailing how fruitful functions return values while void functions perform actions without returning any. It covers the importance of parameters, local and global scope, function composition, and recursion, providing examples for each concept. Additionally, it illustrates how to implement these concepts in programming through various examples, including calculating sums and factorials using recursion.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

4) Fruitful Function


Fruitful function

Void function

Return values

Parameters

Local and global scope

Function composition
• Recursion

A function that returns a value is called fruitful function.


Example:
Root=sqrt (25)
Example:
def add():
a=10
b=20
c=a+b
return c
c=add()
print(c)

74
Void Function
A function that perform action but don’t return any value.
Example:
print(“Hello”)
Example:
def add():
a=10
b=20
c=a+b
print(c)
add()

Return values:
return keywords are used to return the values from the function.
example:
return a – return 1 variable
return a,b– return 2 variables
return a+b– return expression
return 8– return value
PARAMETERS / ARGUMENTS(refer 2nd unit)

Local and Global Scope

Global Scope

The scope of a variable refers to the places that you can see or access a variable.

A variable with global scope can be used anywhere in the program.

It can be created by defining a variable outside the function.
Example output
a=50

def add():
Global Variable
b=20 70
c=a+b
print© Local Variable

def sub():
b=30
c=a-b 20
print©
print(a) 50

75
Local Scope A variable with local scope can be used only within the function .
Example output
def add():
b=20

c=a+b 70
Local Variable
print©
def sub():
b=30 20

c=a-b
Local Variable

print©
print(a) error
print(b) error

Function Composition:

Function Composition is the ability to call one function from within another function
It is a way of combining functions such that the result of each function is passed as the argument of
the next function.
In other words the output of one function is given as the input of another function is known as
function composition.

find sum and average using function output


composition
def sum(a,b): enter a:4
sum=a+b enter b:8
return sum the avg is 6.0
def avg(sum):
avg=sum/2
return avg
a=eval(input("enter a:"))
b=eval(input("enter b:"))
sum=sum(a,b)
avg=avg(sum)
print("the avg is",avg)

Recursion
A function calling itself till it reaches the base value - stop point of function call. Example:
factorial of a given number using recursion

76
Factorial of n Output
def fact(n): enter no. to find fact:5
if(n==1): Fact is 120
return 1
else:
return n*fact(n-1)

n=eval(input("enter no. to find


fact:"))
fact=fact(n)
print("Fact is",fact)
Explanation

Examples:
1. sum of n numbers using recursion
2. exponential of a number using recursion
Sum of n numbers Output
def sum(n): enter no. to find sum:10
if(n==1): Fact is 55
return 1
else:
return n*sum(n-1)

n=eval(input("enter no. to find sum: "))

sum=sum(n)
print("Fact is",sum)

77

You might also like