You are on page 1of 3

Class 8

Chapter 7 UNDERSTANDING THE USE OF FUNCTIONS IN PYTHON


A. Tick (/) the correct answers.
1, A lambda function in Python can take ......
(a) only one argument
(b) only two arguments
(c) only three arguments
d) any number of arguments

2. What will be the output of the following Python code?


(i) deff(x, y, z): return x +y+z
(ii) f(2,30, 400)
(a) 432
(b) 24000
(c) 430
(d) None of these

3. Which of the following is not an advantage of defining a function in a Python program?


(a) We can track a large Python program very easily when the given program is split into
multiple functions inside the code.
(b) Python functions are not reusable and hence we have to define them again and again while
using them.
(c) When we use functions in our Python program, we can avoid writing the same logic again
and again and call the function every time.
(d) We can call a function many times inside the program and we can call it wherever we want
inside the function.

4. Which function returns the absolute value of a number passed on to it as an argument?


(a) abs()
(b) wax()
(c) win()
(d) round ()

5. Which function returns the smallest integer value greater value greater than the number passed
on to it as an argument?
(a) min()
(b) ceil()
(c) floor()
(d) None of these ()

B.Write T' for true and F for false statements.


1. Functions are like 'mini programs'. True
2. Functions help in the division of a program's data into smaller, modular portions. True
3. Euler's number (e) is the base of natural logarithm. True
4. Lambda contains a block of statements. False
5. The function ascii () is a built-in function in Python . True
C. Fill in the blanks.
1. A function is like a mini program that performs a specific task in a program.
2. The keyword def is used to create a user-defined function.
3. The 'math.nan' constant returns a floating point nan value.
4. The function that computes value of log a with base 10 is the log function.

D. Answer the following questions.


1. Name any 2 trigonometric functions available in Python.
Ans- trigonometric functions in Python are:1)sin(x) function ,2)cos(x)function.

2. What is the advantage of using functions?


OR
What is the significance of having a function in a program?
Ans- 1. Simpler Code: A program's code seems to be simpler and easier to understand when it is
broken down into functions. Several small functions are much easier to read than one long
sequence of statements.
2. Code Reuse : Functions also reduce the duplication of code within a program. If a specific
operation needs to be performed in several places in a program, a function can be written once to
perform that operation, and then be executed any number of times. This benefit of using functions
is known as code reuse because you are writing the code to perform a task once and then reusing
it each time you need to perform that task.
3. Better Testing: When each task of the program is coded in terms of functions, testing and
debugging will be simpler. Programmers can test each function in a program individually, to
determine whether it is correctly performing its operation.
4. Faster Development : The entire program is divided into tasks. Each task is assigned to an
individual programmer or a team, so that it is easy to accomplish the program in a faster manner.

3. Explain the use of the following functions in Python:


(a) floor()
Ans- This function returns the greatest integer value smaller than the number. If the
number is already an integer, the same number is returned.

(b) fabs()
Ans- This function returns the absolute value of the number.

4. How do we define a function?


Ans- In Python, you define a function using the def keyword. The basic syntax is as follows:
def function_name(parameters):
# Function body (code block)
# ...
# Optionally, you can use the "return" statement to specify a return value
return result

Let's break down the components:


1. def: It is a keyword that signifies the beginning of a function definition.
2. function_name: This is the name you give to your function. Choose a descriptive
and meaningful name that follows Python naming conventions.
3. parameters: These are placeholders for values that you can pass to the function.
They are optional, and if your function doesn't require any input, you can leave the
parentheses empty.
4. Function body: This is the indented block of code that constitutes the function. It
contains the statements to be executed when the function is called.
5. return statement: This is optional and is used to specify the value that the function
should return. If a function doesn't include a return statement, it implicitly returns
None.

5. What is lambda function?


Ans- A lambda function is a small anonymous function. Python lambda function doesn't have any
return statement. It has only a single expression which is always returned by default. It can take
any number of arguments. The Python lambda function is without a def keyword and name. To
create a Python lambda function, we have got to use the lambda keyword. The basic syntax of
python lambda is
Lambda arguments: expression

6.What is function?
Ans- A Function is a block of program statements that perform a single, specific and well-defined
task. The function that calls another function is known as Calling Function", and the function that
is being called by another function is known as "Called
Function".

7.Which are two different types of Functions?


Ans- There are two different types of functions:
1)User-defined functions : These are defined by the programmer.
2)Built-in functions: These are the functions which don't require any other (external) code
file. These are a part of the Python code and are just built within the Python compiler.
Hence, there is no hassle of importing these modules/libraries into our code.

8.Write the syntax of User-defined Functions.


Ans-The syntax of User-defined functions:

You might also like