0% found this document useful (0 votes)
3 views9 pages

Exp 5

The document outlines an experiment for a Python programming course at Bharati Vidyapeeth Deemed University, focusing on creating a function to calculate the least common multiple (LCM) of two numbers. It explains the concept of functions, including how to define and call them, as well as handling arguments and variable-length arguments. The document concludes with a sample implementation of the LCM function and a section for assessment of the experiment.

Uploaded by

rockeysehrawat1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Exp 5

The document outlines an experiment for a Python programming course at Bharati Vidyapeeth Deemed University, focusing on creating a function to calculate the least common multiple (LCM) of two numbers. It explains the concept of functions, including how to define and call them, as well as handling arguments and variable-length arguments. The document concludes with a sample implementation of the LCM function and a section for assessment of the experiment.

Uploaded by

rockeysehrawat1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Bharati Vidyapeeth Deemed University

College of Engineering, Pune


Department of Electronics and
Communication Engineering

Title: - Write a function that takes two numbers


as input parameters and returns their least
common multiple.

1
Bharati Vidyapeeth Deemed University
College of Engineering, Pune
Department of Electronics and
Communication Engineering

SUBJECT: Python Programming Sem-II


EXPERIMENT NO – 5

AIM: To study a program using a function that takes two numbers as input parameters and
returns their least common multiple.

THEORY:
Functions are the basic building blocks in programming languages. Suppose you are
working on big projects, and there might be a possibility that you want to perform the same
operation, again and again; in that case, we create functions and write that particular code inside
that function.

Now we can use this function as many times as we want and perform that particular operation. For
Example: in an e-commerce website, you want to do a payment for a product you purchased; in
that case, we can write a payment code in a function. This function will be used again and again
for all purchasing. Let’s look at a few stages on how to call a function in Python.

How to Create Functions?


There are two parts of the function, first, define the function and second calling a function. The
functions which we make manually are called User-defined Functions.

Function declaration without argument

Input:

def makePayment():

print("hello")

print("hi")

print("abcdefg")
2
Bharati Vidyapeeth Deemed University
College of Engineering, Pune
Department of Electronics and
Communication Engineering

Output:

This is how we declare a function, def is the keyword used for defining and “makePayment” is
the function name and colon at the end; now, you can write multiple lines into your functions.
This function does not have any arguments; passing arguments into functions is not mandatory.
The colon indicates that there is some piece of code inside this function.

=> def makePayment(argument1, argument2, ...):

A function declaration with arguments. Arguments are the values or data that will be used in the
code written under the functions.

How do Functions work in Python?

Functions are the piece of code that is not executed automatically until you won’t call it. To call
the function, just write the name of the function. Whenever a function is executed, a new symbol
table is created internally in the memory. All the arguments passed into function stores the values
into a local symbol table. A reference variable first looks into the local symbol table; then it tries
to find the function defined inside a function, then it tries to find the global symbol table and
then at last inbuilt names. A global variable cannot be assigned inside the function because it will
be not accessible throughout the system, but you reference it. Whenever a new function is called
inside another function new symbol table is created.

Whenever you define a variable inside a function that can be accessed inside the function only,
you cannot access that variable out the function. If the variable is defined outside, the function
can be used anywhere or into many functions.

=> makePayment()

Calling function without argument.

3
Bharati Vidyapeeth Deemed University
College of Engineering, Pune
Department of Electronics and
Communication Engineering

=> makePayment(arg1, arg2, ...)

Calling function with arguments

If you passed the argument while defining the function, you have to pass the parameter while
calling the functions; otherwise, it will result in an error. The number of parameters while
defining and calling should be the same. If you perform any calculation or have written business
login into a function, you also have to return the final value from the function.

We can also create a function with arguments but with optional arguments and defining default
values to those arguments.

def exampleFunction(a, b=1, c=”abcd”): print(c)

print(a+b)

Now we can call this function in two ways.

=> exampleFunction(5)

In this case, we are passing a value for argument “a,” an argument “b”, and “c” will take the
default argument.

=> exampleFunction(1,2,”efgh”)

In this case, we have passed are three input arguments.

Logics written in functions are easy to understand and easy to debug them; you know which
function is not working properly or generating errors, so you don’t need to go to throughout the
entire code of the page; you can just debug that functions.

4
Bharati Vidyapeeth Deemed University
College of Engineering, Pune
Department of Electronics and
Communication Engineering

Variable Length Arguments:

Let suppose we have a function, but we don’t know how many arguments we need to while
calling that function; it might change every time. In that case, we can define the variable-length
argument function.

def exampleFunction(*args): for value in args:

print(value) exampleFunction(a,b,c)

This function will take n number of arguments as we have * in the arguments, and it will print all
the incoming arguments.

5
Bharati Vidyapeeth Deemed University
College of Engineering, Pune
Department of Electronics and
Communication Engineering

6
Bharati Vidyapeeth Deemed University
College of Engineering, Pune
Department of Electronics and
Communication Engineering

# defining a function to calculate LCM


def calculate_lcm(x, y):
# selecting the greater number
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm

# taking input from users


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# printing the result for the users
print("The L.C.M. of", num1,"and", num2,"is", calculate_lcm(num1, num2))

7
Bharati Vidyapeeth Deemed University
College of Engineering, Pune
Department of Electronics and
Communication Engineering

8
Bharati Vidyapeeth Deemed University
College of Engineering, Pune
Department of Electronics and
Communication Engineering

Conclusion:

Assessment of the Experiment/Assignment:

Timely Submission Presentation Understanding Total Signature of

(07) (06) (12) (25) Teacher with date

You might also like