Chapter 4
Functions
Follow 'study with jbrtrisea' Youtube Channel to understand in Tamil
FUNCTIONS
A function is a named sequence of statements that perform a specific computation. Functions help programmers
to break the program into small manageable units or modules. Functions may or may not take arguments and
may or may not produce a result.
The Advantages of Function
Increases readability: The program is better organised and easy to understand if the longer code written using
functions.
Reduces code length: The same code is not required to be written at multiple places in a program, it makes
debugging easier.
Increases reusability: The function can be called from another function or another program; we can reuse or
build upon already defined functions and avoid repetitions of writing the same piece of code.
Work can be easily divided among team members and completed in parallel.
HIDING REDUNDANCY
In Python, redundancy refers to the unnecessary repetition or duplication of code, which can make programs longer, more
complex, and harder to maintain or debug. One of the most effective ways to reduce this issue is by using functions, which
centralize reusable blocks of code. Instead of writing the same logic in multiple places, you can encapsulate it in a function and
call it whenever needed.
Functions play a key role in hiding redundancy in several ways:
Encapsulation and Code Reusability
Rather than copying and pasting the same code, wrap it inside a function. This allows the same logic to be reused across the
program without rewriting it. Example: A function that calculates the area of a rectangle can be called whenever needed instead of
repeating the formula.
Abstraction and Hiding Implementation Details
Functions provide a clear interface for a task while hiding the underlying complexity. This focus on what the function does rather
than how it works. Example: A function to send an email can be used without knowing the internal process of connecting to a
mail server or formatting the message.
Modularization and Breaking Down Complexity
Large programs can be split into smaller, focused functions, each handling a specific subtask. This
improves organization, readability, and debugging.
Example: A library management system might have separate functions for adding books, borrowing
books, returning books, and searching the catalog.
Parameterized Operations for Flexibility
By accepting parameters, a single function can handle variations of a task. Passing different
arguments makes it adaptable without creating new functions for each variation. Example: A date-
formatting function can take a format string as a parameter, avoiding separate formatting logic for
each date style.
Benefits of Hiding Redundancy with Functions
Reduced Code Duplication : A single function definition can be reused in multiple places, avoiding repeated code blocks.
Improved Maintainability : Updates or bug fixes only require changes in one place, ensuring consistent behavior across the program.
Better Readability and Organization : Functions make code more structured and easier to navigate.
Increased Reusability : Well-designed functions can be reused not only in different parts of the same project but also in entirely different
projects.
Example:
def fact(n):
if n = = 1:
return n
else:
return n*fact(n1)
num = int(input("Enter a number: "))
print("The factorial of",num,"is",fact(num))
COMPLEXITY
Functions in Python hide complexity through the principle of abstraction. They allow to define a block of code that performs
a specific task, and then call that function by name without needing to know the intricate details of how it achieves its result.
Information Hiding / Data Hiding
While not exclusively a function-related concept, in object-oriented programming, methods can hide internal data and
implementation details of a class.
Information hiding is the practice of concealing the internal details of a module, class, or function so that the outside world
cannot directly access or depend on them.
It protect internal implementation from unintended interference and allow changes without affecting outside code.
By using conventions like leading underscores for "private" attributes, indicates that these elements are internal to the class
and should not be directly accessed from outside, further abstracting the object's complexity.
Data Abstraction
Data abstraction means the process of exposing only essential features of an object or function and hiding unnecessary
implementation details.
In Python abstraction allow users to interact with data or functionality at a high level without worrying about how it works
internally.
TYPES OF FUNCTIONS
In Python, functions are classified into two types. They are,
Built-in- functions
User defined functions.
Built in functions
Python has a very extensive standard library. It is a collection of many built in functions that can be called in the
program as and when required, thus saving programmer’s time of creating those commonly used functions every
time.
Built-in functions are the ready-made functions in Python that are frequently used in programs but cannot
modify them.
Example: Program to calculate square of a number
a = int(input("Enter a number: ")
b=a*a
print(" The square of ",a ,"is", b)
User Defined Functions
In addition to the standard library functions, we can define our own functions while writing the program. Such functions
are called user defined functions. Thus, a function defined to achieve some task as per the programmer's requirement is
called a user defined function. The user can understand the internal working of the function and can be changed and
modified.
ELEMENTS OF USER DEFINED FUNCTIONS
In python the user defined functions contain two elements. They are,
1. Function definition
2. Function call
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Function definition
A function definition begins with def (short for define). A function definition specifies the name of a new function and the
sequence of statements that run when the function is called.
Syntax of function definition
Elements of function definition
The elements of a function definition are,
1. Function header
2. Function body
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Function Header
The first line of a function definition is the function header.
A function header starts with the keyword def, followed by the function name.
The function name is followed by a comma-separated list of identifiers called formal parameters, or simply parameters.
The function header is ended with a colon ( : ).
Function Body
The function body follows the function header.
The function body contains one or more valid Python statements or the function's instructions.
The statements must be indented, relative to the function header.
The statements outside the function indentation are not considered as part of the function.
Example:
def greet(name):
print(“Hello,” + name + “. Good morning!”)
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Function Call
A function call is a statement that calls a function. It consists of the function name followed by an argument list in
parentheses.
Once function is defined, it can be called from another function, program or even the Python prompt.
To call a function, simply type the function name with appropriate parameters.
Example:
>>>greet(‘paul’)
Hello, Paul. Good morning
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Illustration of user defined function
def swap(a,b):
Output:
a,b=b,a
enter the first number:67
print("After swap :")
enter the second number:98
print("First number = ",a)
print("second number = ",b) Before swap:
First number = 67
a=input("\n enter the first number :") second number = 98
b=input("\n enter the second number :") After swap:
print("Before swap :") First number = 98
print("First number = ",a)
second number = 67
print("second number = ",b)
swap(a,b)
JBR Trisea Publishers Follow 'study with jbrtrisea'
Youtube Channel to understand GE3151 in Tamil
PARAMETERS AND ARGUMENTS
Parameter: A name used inside a function to refer to the value passed as an argument. Parameters are specified within the
pair of parenthesis in the function definition and are separated by commas.
Argument: An argument is a value passed to the function during the function call which is received in corresponding
parameter defined in function header.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Some of the functions require arguments. For example, [Link] requires number as an argument.
Some functions take more than one argument. For example, [Link] takes two arguments, the base and the exponent.
Inside the function, the arguments are assigned to variables called parameters. Here is a definition for a function that takes
an argument:
def print_twice(Hello):
print(Hello)
print(Hello)
This function assigns the argument to a parameter named Hello. When the function is called, it prints the value of the
parameter (whatever it is) twice.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
FRUITFUL FUNCTIONS
Functions that return values are called as fruitful functions. In many programming languages, a function that doesn’t
return any value is called a procedure.
The return statement is followed by an expression which is evaluated. Its result is returned to the caller as the “fruit” of
calling function.
The return statement
The calling function generates a return value, which is usually assigned to a variable or used as a part of an expression.
The return statement is also used to exit a function and go back to the place from where it was called.
Syntax:
return[expression]
This statement can contain expression which gets evaluated and the value is returned.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Return None
If there is no expression in the statement or the return statement itself, then the function will return the None object,
where None means nothing.
>>>greet('Ramesh')
Hello, Ramesh. Good morning
None
Here, None is the returned value.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Return values
The return value may or may not be assigned to another variable in the caller. Once the value is returned from the function, it immediately exits
that function. Therefore, any code written after the return statement is never executed.
Program to find area of circle
def area(r):
a = [Link] * r**2
return a
import math
r=int(input("Enter a number: "))
x=area(r)
print("area =",x)
Output:
Enter a number: 2
area = 12.566370614359172
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Return fruitful return
In a fruitful function the return statement includes an expression. Fruitful return means “Return immediately from this function
and use the following expression as a return value.”
Illustration of fruitful return
def area(r):
return [Link] * r**2
import math
r=int(input("Enter a number: "))
x=area(r)
print("area =",x)
Output:
Enter a number: 2
area = 12.566370614359172
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Arguments are used to call a function and there are four main types of defining a function argument. They are,
Required Arguments
Keyword Arguments
Default Arguments
Variable length Arguments
Required Arguments
In the required arguments, the arguments are passed to a function in correct positional order. Here, the number of arguments
in the function call should exactly match with the number of arguments specified in the function definition.
Example1:
def area(length,breadth):
A=length*breadth
print("Area=",A)
area(4,5)
Output: 20
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
FORMAL Vs ACTUAL ARGUMENTS
Formal Arguments
These are the variables defined in the function header.
They act as placeholders for the values that will be passed when the function is called.
Exist only inside the function (local scope).
Example:
def greet(name, age): # 'name' and 'age' are formal arguments
print("Hello {name}, you are {age} years old.")
Actual Arguments
These are the real values (or variables) passed to the function during function call.
They get assigned to the formal arguments in the order they are passed (unless you use keyword arguments).
Example:
greet("Alice", 25) # "Alice" and 25 are actual arguments
Program 4.4:
def add(x, y): # x, y → formal arguments
return x + y
result = add(5, 10) # 5, 10 are actual arguments
print(result)
NAMED ARGUMENTS
Arguments are used to call a function and there are four main types of defining a function argument. They are,
Required Arguments
Keyword Arguments
Default Arguments
Variable length Arguments
Example 2:
def printstring(num,str):
print(num,str)
return
printstring(5,”star”)
Output:
5 star
In this example values passed through arguments are passed to parameters by their position. 5 is assigned to num, star is
assigned to star. So the output is 5 star.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Example 3:
def area(length,breadth):
A=length*breadth
print("Area=",A)
area(4)
Output:
Traceback (most recent call last):
File "C:/Users/TRI SEA/AppData/Local/Programs/Python/Python36-32/[Link]", line 5, in <module>
area(4)
TypeError: area() missing 1 required positional argument: 'breadth'
In this example while executing the program, it produces the error. It needs at least one parameter to prevent syntax errors
to get the required output.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Keyword arguments
•Keyword arguments are used when a function is called with a long parameter list where most of the parameters have default values and we wish to change very few of
them.
•When calling a function, keyword argument specifies both keyword and value.
•Python also allows to skip arguments or the order of arguments can be changed.
•The values are not assigned to arguments according to their position but based on their names. But all the keyword arguments should match the parameters in the
function definition.
Example 1:
def namelist(name,degree,dept):
print ("Name :",name)
print ("Degree :",degree)
print ("Department:",dept)
namelist(name=”Arun”,degree=”B.E”,dept="CSE")
Output:
Name: Arun
Degree: B.E
Department: CSE
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Example 2:
def namelist(name,degree,dept):
print ("Name :",name)
print ("Degree :",degree)
print ("Department:",dept)
namelist(dept=”MECH”,name=”Raja”,degree=”B.E”)
Output:
Name: Raja
Degree: B.E
Department: MECH
Here all the parameters are given as keyword arguments. So arguments need not to maintain the same order.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Example 3:
def namelist(name,degree,dept):
print ("Name :",name)
print ("Degree :",degree)
print ("Department:",dept)
namelist(name=”Mala”,degree=”B.E”,”CIVIL”)
Output:
This will produce a syntax error as non keyword argument after keyword argument.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Default Arguments
Python allows users to specify function arguments that can have default values. This means that a function can be called
with fewer arguments than it is defined to have. That is, if the function accepts three parameters, but function call provides
only two arguments, then the third parameter will be assigned the default value.
The default value to an argument is provided by using the assignment operator (=). Users can specify a default value for
one or more arguments. The default arguments must be written after the non-default arguments. That is non-default
argument can not follow default arguments.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Example 1: Example 2:
def display(name,dept="CSE",rollno=15): def display(name,dept="CSE",rollno=15):
print("Name: ",name) print("Name: ",name)
print("Department: ",dept) print("Department: ",dept)
print("Roll_ No: ",rollno) print("Roll_ No: ",rollno)
display("Arun") display("Arun","MECH")
Output: Output:
Name: Arun Name: Arun
Department: CSE Department: MECH
Roll_ No: 15 Roll_ No: 15
JBR Trisea Publishers Follow 'study with jbrtrisea'
Youtube Channel to understand GE3151 in Tamil
Example 3:
def display(name,dept="CSE",rollno=15):
print("Name: ",name)
print("Department: ",dept)
print("Roll_ No: ",rollno)
display(name="Arun","MECH",25)
Output:
SyntaxError: Non default argument follows default argument.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Variable length Arguments
In some situations, it is not known in advance how many arguments will be passed to a function. In such cases, Python allows programmers to make function calls with arbitrary
number of arguments.
When we use arbitrary arguments or variable-length arguments, then the function definition uses an asterisk (*) before the parameter name.
Example:
def display(name,*friends):
print("Name: ",name)
print("Friends : ",friends)
display("Arun","Bala","Cindia","Dinesh","Elsa","Freeda")
Output:
Name: Arun
Friends : ('Bala', 'Cindia', 'Dinesh', 'Elsa', 'Freeda')
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
RECURSION
A function that calls itself is recursive; the process of executing it is called recursion.
Recursion can be used to solve the problems that can be expressed in terms of similar
problems of smaller size.
Example : Finding factorial of a number
To understand recursion, let us take an example of calculating factorial of a number.
To calculate n!, we have to multiply the number with factorial of a number that is 1 less
than that number.
Factorial of a number is the product of all the integers from 1 to that number. In other
words, n!= n × (n1)!.
The factorial of 6 is 1×2×3×4×5×6 = 720.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Program :
# An example of a recursive function to find the factorial of a number.
def fact(x):
if x = = 1:
return 1
else:
return (x * fact(x1))
n = int(input("Enter a number: "))
print("The factorial of", n, "is", fact(n))
Output:
Enter a number: 4
The factorial of 5 is 24
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
In this program, fact() is a recursive function as it calls itself. When this function is called with a positive integer, it will recursively call
itself by decreasing the number.
Each function call multiples the number with the factorial of the number until the number is equal to one. This recursion ends when the
number reduces to 1. It is called the base condition.
Every recursive function must have a base condition that stops the recursion or else the function would call itself infinitely.
This recursive call can be explained in the following steps.
fact(4) # 1st call with n=4
4 * fact(3) # 2nd call with n=3
4 * 3 * fact(2) # 3rd call with n=2
4 * 3 * 2 * fact(1) # 4th call with n=1
4*3*2*1 # return from 4th call as number=1
4*3*2 # return from 3rd call
4*6 # return from 2nd call
24 # return from 1st call
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Advantages of recursion
A complex task can be broken down into simpler sub-problems using recursion.
Sequence generation is easier with recursion rather than using some nested iteration.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Stack diagrams for recursive functions
The stack diagram represents the state of a program during a function call. It helps to interpret a recursive
function.
Every time a function gets called, Python creates a frame to contain the function's local variables and
parameters. For a recursive function, there might be more than one frame on the stack at the same time.
Example : Stack diagram for fact called with n = 4.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
The top of the stack is the frame for __main__. It is empty because it did not create any
variables in __main__ or pass any arguments to it.
The four countdown frames have different values for the parameter n. The bottom of the
stack, where n=1, is called the base case. It does not make a recursive call, so there are no
more frames.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Infinite recursion
If a recursion never reaches a base case, it goes on making recursive calls forever and the program never terminates. This
is known as infinite recursion, and it is generally not a good idea.
def recurse():
recurse()
In most programming environments, a program with infinite recursion does not really run forever. Python reports an error
message when the maximum recursion depth is reached.
File "<stdin>", line 2, in recurse
File "<stdin>", line 2, in recurse
File "<stdin>", line 2, in recurse
.
.
File "<stdin>", line 2, in recurse
RuntimeError: Maximum recursion depth exceeded
When the error occurs, there are 1000 recurse frames on the stack! If an infinite recursion is encountered by accident,
review the function to confirm that there is a base case that does not make a recursive call.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
LAMBDA FUNCTIONS OR ANONYMOUS FUNCTIONS
In Python, anonymous function is a function that is defined without a name. In Python normal functions are defined using
the def keyword and anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also
called as lambda functions.
Lambda function is mostly used for creating small and one-time anonymous function.
Lambda functions are mainly used in combination with the functions like filter(), map() and reduce().
Lambda function can take any number of arguments and must return one value in the form of an expression. Lambda
function only access global variables and variables in its parameter list.
Syntax:
lambda [arguments]: expression
The arguments contain a comma separated list of arguments and the expression is an arithmetic expression that uses these
arguments. The function can be assigned to a variable to give it a name.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Example:
sum = lambda x,y: x+y
print ('The Sum is :', sum(30,40))
print ('The Sum is :', sum(-30,40))
Output:
The Sum is : 70
The Sum is : 10
In this program, lambda x,y: x+y is the lambda function. x and y are the arguments. x+y is the expression gets evaluated
and returned. Note that the lambda function has no name. It returns a function object which is assigned to the identifier
sum.
lambda x,y : x+y can be written as
def sum(x,y):
return sum
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
SCOPE
Scope of variable refers to the part of the program, where it is visible. Variables in a program may not be accessible at
all locations in that program. This depends on where a variable is declared.
The scope of a variable determines the portion of the program where user can access a particular identifier. There are
two types of scope of variables in Python. They are global scope or local scope.
A global variable is a variable that is declared in the main program while a local variable is a variable that is declared
within the function.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Global scope
A variable defined outside a function body has a global scope. It can be created by defining a variable
outside of any function/block.
A variable, with global scope can be used anywhere in the program.
Any modification to global variable is permanent and visible to all the functions written in the file.
Program :
x=50 (Here, x is the Global variable)
def test(x):
x+= 10 (Here, x is the Local variable)
print('Inside test x is', x)
Output:
print('Main: Value of x is', x) Main: Value of x is 50
Inside test x is 60
test(x)
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
x=50 (Here, x is the Global variable)
def test(x):
x+= 10 (Here, x is the Local variable)
print('Inside test x is', x)
print('Main: Value of x is', x)
test(x)
In this example variable x=50 is global declaration. It can be accessed by the
test function. The ‘x’ used inside the test function is local scope. The changes
made inside the function will not be reflected back to the main function.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Local Scope
A variable defined in a function body has a local scope. It can be created by defining a variable inside a
function definition. They are not related in any way to other variables with the same names used outside the
function, i.e., variable names are local to the function. A local variable only exists while the function is
executing.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Program :
x=50
def test( ):
x=30
print('Value of x inside test is :',x)
x=x+10
print('Value of x inside test is :',x)
print('Value of x is :',x)
test()
print('After function call Value of x in main is :',x)
Output:
Value of x is : 50
Value of x inside test is : 30
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Value of x inside test is : 40
After function call Value of x in main is : 50
In this example variable x=50 is global declaration. The variable x=30 is local declaration.
The ‘x’ used inside the test function is local scope. The changes made inside the function
will not be reflected back to the main function.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Difference between Global Variables and Local Variables
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
COMPOSITION
Composition is the ability to take small building blocks (variables, expressions and statements) and compose them.
Example:
x = [Link](degrees / 360.0 * 2 * [Link])
Here the argument of a function can be any kind of expression, including arithmetic operators and function calls.
Example:
>>>x = [Link]([Link](x+1))
>>>print x 1.86602540378
This statement finds the log of previous value x + 1 and then raises e to that power. The result gets assigned to x.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Composition is the ability to take small building blocks (variables, expressions and statements) and compose them.
Example:
x = [Link](degrees / 360.0 * 2 * [Link])
Here the argument of a function can be any kind of expression, including arithmetic operators and function calls.
Example:
>>>x = [Link]([Link](x+1))
>>>print x 1.86602540378
This statement finds the log of previous value x + 1 and then raises e to that power. The result gets assigned to x.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Example:
Consider two functions fn1 & fn2, such that
a= fn2 (x)
b= fn1 (a)
Then call to the two functions can be combined as
b= fn1 (fn2 (x))
Similarly, the statement composed of more than two functions. In that result of one
function is passed as argument to next and result of the last one is the final result.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Area of circle without composition ([Link])
def distance(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
dsquared = dx**2 + dy**2
result = dsquared**0.5
return result
def area(radius):
b = 3.14159 * radius**2
return b
def area2(xc, yc, xp, yp):
radius = distance(xc, yc, xp, yp)
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
result = area(radius)
Output:
6.2831800000000015
In this example, distance function that takes two points, the center of the
circle ( xc, yc) and a point on the perimeter (xp, yp), and computes distance
between the two points. This is the radius of the circle.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Program : Program to find area of circle with composition ([Link])
def distance(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
dsquared = dx**2 + dy**2
result = dsquared**0.5
return result
def area(radius):
b = 3.14159 * radius**2
return b
def area2(xc, yc, xp, yp):
return area(distance(xc, yc, xp, yp))
print(area2(0,0,1,1))
Output:
6.2831800000000015
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Output:
6.2831800000000015
Composition is used to package the code into modules, which may be used in many
different unrelated places and situations. Also it is easy to maintain the code.
JBR Trisea Publishers Follow 'study with jbrtrisea' Youtube Channel to understand GE3151 in Tamil
Thank You
Follow 'study with jbrtrisea' Youtube Channel to understand in Tamil