B02 SBL Expt No 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Experiment No.

04
A.1 Aim:
a) Python Program to Make a Simple Calculator using functions.
b) Write a Python function that takes a number as a parameter and check the number is prime or not.

A.2 Prerequisite:
Python Basics
A.3 Outcome:
After successful completion of this experiment students will be,
able to write functions in python

A.4 Theory:
Function:
Functions are the most important aspect of an application. A function can be defined as the organized
block of reusable code, which can be called whenever required.
Python allows us to divide a large program into the basic building blocks known as a function. The
function contains the set of programming statements enclosed by {}. A function can be called multiple
times to provide reusability and modularity to the Python program.
The Function helps to programmer to break the program into the smaller part. It organizes the code very
effectively and avoids the repetition of the code. As the program grows, function makes the program more
organized.
There are mainly two types of functions.
User-define functions - The user-defined functions are those define by the user to
perform the specific task.
Built-in functions - The built-in functions are those functions that are pre-defined in
Python.
Creating a function:
Python provides the def keyword to define the function. The syntax of the define function is given below.
The def keyword, along with the function name is used to define the function.
The identifier rule must follow the function name.
A function accepts the parameter (argument), and they can be optional.
The function block is started with the colon (:), and block statements must be at the same indentation.
The return statement is used to return the value. A function can have only one return.
Syntax:
def my_function(parameters):
function_block
return expression
Function calling:
In Python, after the function is created, we can call it from another function. A function must be defined before the
function call; otherwise, the Python interpreter gives an error. To call the function, use the function name followed by
the parentheses.
Syntax:
function_name()

Arguments in Function:
The arguments are types of information which can be passed into the function. The arguments are specified in the
parentheses. We can pass any number of arguments, but they must be separate them with a comma.
There may be several types of arguments which can be passed at the time of function call.
1. Required arguments:
Required arguments are the arguments which are required to be passed at the time of function calling with the exact
match of their positions in the function call and function definition.

2. Keyword arguments:
The name of the arguments is treated as the keywords and matched in the function calling and definition. If the same
match is found, the values of the arguments are copied in the function definition.
3. Default arguments:
If the value of any of the arguments is not provided at the time of function call, then that argument can be initialized
with the value given in the definition even if the argument is not specified at the function call.
4. Variable-length arguments:
Python provides us the flexibility to offer the comma-separated values which are internally treated as tuples at the
function call. By using the variable-length arguments, we can pass any number of arguments.
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the practical. The soft copy
must be uploaded on the Blackboard or emailed to the concerned lab in charge faculties at the end of the practical
in case the there is no Black board access available)

Roll. No: B02 Name: Somesh Ashok Bagal


Class: SE Batch: B1
Date of Experiment: Date of Submission:
1-April-2022 15-April-2022
Grade:

B.1 Software Code written by student:


1. Python Program to Make a Simple Calculator using functions.
2. Write a Python function that takes a number as a parameter and check the
number is prime or not.

B.2 Input and Output:


1) Input & Output :- In this program, we ask the user to choose an operation.
Options 1, 2, 3, and 4 are valid. If any other input is given, Invalid input is
displayed and the loop continues until a valid option is selected.
Two numbers are taken and on if…elif…else branching is used to execute a
particular section. user-defined functions add(), subtract(), multiply(), and divide().
evaluate respective operations and display the output.

2) Input & Output :- In this program, we have checked if num is prime or not. Numbers less
than or equal to 1 are not prime numbers. Hence, we only proceed if the num is greater than 1.
We check if num is exactly divisible by any number from 2 to num - 1. If we find a factor in
that range, the number is not prime, so we set flag to True and break out of the loop.
Outside the loop, we check if flag is True or False.
● If it is True, num is not a prime number.
● If it is False, num is a prime number.
B.3 Conclusion:
1) For 1st program: As you can see in the above program, we have four different
functions defined at the beginning of the program. The add function is for adding two
numbers, the sub function is for subtraction, the mul function is for the multiplication
of two numbers and the div function is for finding out the division.All of these
functions take two numbers as parameters. In this example, we are printing out the
result inside each function. You can also return the result from the functions and print
them out inside the caller class.

2)For 2nd program:


● Return False if the number is 0, 1, a negative number or a multiple of 2.
● Use all() and range() to check numbers from 3 to the square root of the given number.
● Return True if none divides the given number, False otherwise.

B.4 Question of Curiosity


Q.1. What is recursive Function?
Ans:-
1. Recursive functions are functions that calls itself. It is always made up of 2
portions, the base case and the recursive case. The base case is the condition to
stop the recursion. The recursive case is the part where the function calls on itself.
2. Recursion is a common mathematical and programming concept. It means that a
function calls itself. This has the benefit of meaning that you can loop through
data to reach a result.
3. The developer should be very careful with recursion as it can be quite easy to slip
into writing a function which never terminates, or one that uses excess amounts of
memory or processor power. However, when written correctly recursion can be a
very efficient and mathematically-elegant approach to programming.

Q.2 Select which true for Python function


a) A function is a code block that only executes when called and always returns a value.
b) A function only executes when it is called and we can reuse it in a program
c) Python doesn’t support nested function

Ans:- Both a) & b)


Q.3 . What is the output of the following function call
def outerFun(a, b):
def innerFun(c, d):
return c + d
return innerFun(a, b)
return a
result = outerFun(5, 10)
print(result)
a) 5
b) 15
c) (15, 5)
d) Syntax Error

Ans:- b) 15

You might also like