Exp 5
Exp 5
1
Bharati Vidyapeeth Deemed University
College of Engineering, Pune
Department of Electronics and
Communication Engineering
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.
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.
A function declaration with arguments. Arguments are the values or data that will be used in the
code written under the functions.
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()
3
Bharati Vidyapeeth Deemed University
College of Engineering, Pune
Department of Electronics and
Communication Engineering
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.
print(a+b)
=> 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”)
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
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.
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
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: