Working with Functions in Python is a key chapter in Class 12 Computer Science. It teaches students how to define and call functions, pass arguments, return values, and understand variable scope. Mastering functions is essential for writing clean and modular Python programs.
Working with Functions in Python Class 12 Solutions
1. What do you understand by local and global scope of variables?
Answer – A global variable is one that is accessible outside and inside the function; it is also known as a global scope. A local variable is one that is only accessible inside the function.
Example of Global Variable:
x = 20 # Global variable
def my_function():
print(x) # Accessible inside the function
my_function()
print(x) # Accessible outside the function too
Example of Local Variable:
def my_function():
x = 10 # Local variable
print(x) # Accessible inside the function
my_function()
print(x) # This will raise an error because x is local to my_function.
3. Differentiate between the round() and floor() functions with the help of suitable example.
Answer – The function floor() is used to convert to the lowest lower whole number, whereas the function round() is used to convert a fractional number into a whole as the nearest next. For example, round (5.8) = 6, round (4.1) = 5, and floor (6.9) = 6, floor (5.01) = 5.
import math
print(round(5.8)) # Output: 6
print(round(4.1)) # Output: 4
print(math.floor(6.9)) # Output: 6
print(math.floor(5.01)) # Output: 5
4. What is the difference between input() and raw_input()?
Answer – In Python, raw_input() and input() were two functions used for taking input from the users. raw_input() is used in Python to take user input as a string, whereas the input() function takes user input based on the input type. For example, if the user has entered a number, then Python interprets it as an integer rather than a string.
5. What is a function in python?
Answer – A function is a reusable block of code that performs a specific task. The function helps to organise the code efficiently and reduce repetition of code. Instead of writing the same code multiple times, you can define a function, and you can reuse it multiple times.
6. Write and explain the types of functions supported by python.
Answer – Python functions are categorised into two main types: built-in functions and user-defined functions. Built-in functions come with several pre-defined functions which perform various tasks such as input handling, mathematical operations and type conversion, etc. User-defined functions are created by programmers using the def keyword. These functions are helpful for organisation of the code and improving reusability.
7. Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument.
Answer –
def factorial(n):
if n < 0:
return
elif n == 0:
return 1
else:
return n * factorial(n - 1)
n = int(input("Enter a non-negative integer: "))
print(f"Factorial of {n} is {factorial(n)}")
Output:
Enter a non-negative integer: 5
Factorial of 5 is 120
8. Write a Python function to find the Max of two numbers.
Answer –
def max_number(x, y):
if x > y:
return x
else:
return y
print(max_number(2, 5))
Output:
5
9. Write a program to create a function that takes two arguments, name and age, and print their value.
Answer –
def student(name, age):
print(name, age)
student("Amit", 22)
Computer Science Class 12 Questions and Answers
- Working with Functions Class 12 Notes
- Exception Handling in Python Class 12 Notes
- File Handling in Python Class 12 Notes
- Data Structure Class 12 Notes
- Computer Networks Class 12 Notes
- Database Management Class 12 Notes
Disclaimer: We have taken an effort to provide you with the accurate handout of “Working with Functions in Python Class 12 Solutions“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above CBSE study material present on our websites is for education purpose, not our copyrights. All the above content and Screenshot are taken from Computer Science Class 12 NCERT Textbook, CBSE Sample Paper, CBSE Old Sample Paper, CBSE Board Paper and CBSE Support Material which is present in CBSEACADEMIC website, NCERT websiteThis Textbook and Support Material are legally copyright by Central Board of Secondary Education. We are only providing a medium and helping the students to improve the performances in the examination.
Images and content shown above are the property of individual organizations and are used here for reference purposes only.
For more information, refer to the official CBSE textbooks available at cbseacademic.nic.in