Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings

Fruitful Functions

Syntax, Example Program | Python Programming

There are two types of functions. 1) The functions that return some value 2) The functions that does not return the value. The fruitful functions are the functions that return values.

Fruitful Functions    AU : Jan. 18, Marks 6

There are two types of functions.

1) The functions that return some value

2) The functions that does not return the value. The fruitful functions are the functions that return values.

 

1. Return Values

The value can be returned from a function using the keyword return. For example

Syntax

return (expression_list]

 

Example 3.6.1 Write a function that returns area of circle

Solution :

Step 1 : Write a function for finding out area of circle in Script mode as

functionDemo.py

def area(r):

result = 3.14*r**2

print("The area of Circle ")

return(result)

Step 2 : Now press the key F5 to get the output of the above program. Give the function call by passing some value of radius to it.

The output will be displayed on the shell window as follows:


 

2.  Parameters

We can pass different number of parameters to the function. Following example illustrates the parameter passing to the function

Example 3.6.2 Write a Python program for creating simple calculator,

Solution :

def add(x, y):

return x + y

def sub(x, y):

return x – y

def mult(x,y)

return x * y

def div(x, y):

return x/y

print("Main Menu")

print("1.Add")

print("2.Subtract")

print("3.Multiply")

print("4.Divide")

 

print("Enter your choice")

choice = int(input())

print("Enter first number")

num1 = int(input())

print("Enter second number: ")

num2 = int(input())

 

if choice = = 1:

print(num1,"+", num2,"=", add(num1,num2))

elif choice = = 2:

print(num1,"-", num2,"=", sub(num1,num2))

elif choice = = 3:

print(num1,"*",num2,"=", mult(num1,num2))

elif choice = = 4:

print(num1,"/", num2,"=", div(num1,num2))

else:

Output

Main Menu

1.Add

2.Subtract

3.Multiply

4.Divide

Enter your choice

1

Enter first number

10

Enter second number:

20

10 + 20 = 30

>>> 

 

Example 3.6.3 Write a python program to find the largest among the three numbers.

Solution :

def largest(x, y,z):

if (x > y) and (x > z):

print("First Number is largest")

elif (y > x) and (y > z):

print("Second Number is largest")

else:

print("Third Number is largest")

print("Enter first number")

num1 = int(input())

print("Enter second number: ")

num2 = int(input())

print("Enter third number: ")

num3 = int(input())

print(largest(num1,num2,num3))

 

Example 3.6.4 Write a Python program using function to find the sum of first 'n' even numbers and print the result.

AU : Jan.-18, Marks 6

Solution :

# first n even numbers

# function to find sum of

# first n even numbers

def evensum(n):

step = 2

sum = 0

i = 1

# sum of first n even numbers

while i < = n:

sum + = step

# next even number

step + = 2

i = i + 1

return sum

# Driver Code

print("Enter value of n")

n = int(input())

print("sum of first", n, "even number is: ",evensum(n))

Output

Enter value of n

3

sum of first 3 even number is: 12

 

Example 3.6.5 Write a Python program using function to find the factors of a given number.

Solution :

#define a function

def Find_factors(n):

# This function takes a number and prints the factors

print("The factors of,n,"are:")

for i in range(1, n + 1):

if n %i = = 0:

print(i)

print("Enter a number: ")

num=int(input())

Find_factors(num)

Output

Enter a number :

12

The factors of 12 are:

1

2

3

4

5

6

12

 

Example 3.6.6 Write a Python program using function to find the GCD of two numbers

Solution :

# define gcd function

def gcd(a, b):

while(b):

a, b = b, a % b

return a

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: ")).

print("The G.C.D of", num1,"and", num2,"is", gcd(num1, num2))

Output

Enter first number: 12

Enter second number: 15

The G.C.D of 12 and 15 is 3

 

3. Local and Global Scope

• The global variables are those variables that are declared and defined outside the function and can be used inside the function.

• The local variables are those variables that are declared and defined inside a function.

• A global variable is one that can be accessed anywhere. A local variable is the opposite, it can only be accessed within its frame.

• The difference between the global and local is that global variables can be accessed locally, but not modified locally inherently.

• For example : In the following program, variable a is global variable.

def fun():

print(a)

#global scope

a = 10 fun()

Output

10

Now consider following program, in which we try to change the value declared outside the function

def f():

print(a)

a = 100    #Due to this statement the error is raised

# Global scope

a = 10

f()

print(a)

To make the above program work, we need to use global keyword. We only need to use global keyword in a function if we want to do change that variable.

The corrected version of above program is as follows:

def f():

global a

print(a)

a = 100

# Global scope

a = 10

f()

print(a)

Output

10

100

 

4. Function Composition

• Function composition is a way of combining functions such that the result of each function is passed as the argument of the next function.

• For example, the composition of two functions f and g is denoted f(g(x)). Here x is the argument of g, the result of g is passed as the argument of f and the result of the composition is the result of f.

For example

Step 1: Create a simple function for addition of two numbers.

def add(a,b):

return a+b

Step 2: Create a simple function for multiplication of two numbers

def mul(c,num):

return c*num

Step 3: Create a main function in which the two functions used in above two steps are called.

def mainFun(x,y):

z = add(x,y)

result = mul(z, 10)

return result

The complete program will now look like this


Step 4: Now execute the above program by pressing F5 key. The output can be obtained as follows -


 

Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings : Tag: Engineering Python : Syntax, Example Program | Python Programming - Fruitful Functions