[Follow here] for daily coding insights.
PREVIOUS CHAPTER QUESTION BANK CLICK BELOW
[NOTE: Kindly cross check answers once from other source also, Quantity and Quality of answers also kindly check before use, specially 5 marks answers are not sufficient kindly add from your side, this gives just quick review]
CBSE/Karnataka PU Board PUC I Chapter 7
Functions in Python:
50 MCQ :
-
What is a function in Python?
A. A variable
B. A named block of code
C. A data type
D. A module
Answer: B -
Which keyword is used to define a user-defined function?
A. func
B. def
C. function
D. define
Answer: B -
Functions help to achieve which of the following?
A. Modularity
B. Redundancy
C. Complexity
D. Error
Answer: A -
What is returned by a function with no return statement?
A. Zero
B. None
C. True
D. False
Answer: B -
Which is NOT an advantage of using functions?
A. Readability
B. Reusability
C. Difficult debugging
D. Code organization
Answer: C -
What is the default value returned by print()?
A. The printed text
B. None
C. 0
D. True
Answer: B -
Which is NOT a standard library function in Python?
A. input()
B. print()
C. sort()
D. user_func()
Answer: D -
What statement calls a function named 'myFunc'?
A. run myFunc()
B. use myFunc()
C. myFunc()
D. call myFunc
Answer: C -
Arguments passed in a function call are assigned to ____ in the definition.
A. parameters
B. values
C. fields
D. variables
Answer: A -
Which statement is true for local variables?
A. Accessible anywhere
B. Only in function defined
C. In all modules
D. In built-in functions
Answer: B -
Function names must follow rules for _______.
A. Constants
B. Identifiers
C. Operators
D. Comments
Answer: B -
Which of the following is optional in a function definition?
A. Colon
B. Parameters
C. Function name
D. Indentation
Answer: B -
In Python, return statement does what?
A. Ends loop
B. Skips value
C. Sends value to caller
D. Deletes variable
Answer: C -
What is a void function?
A. Returns a value
B. Prints output
C. Returns None
D. Has no arguments
Answer: C -
How many values can Python return from a function?
A. Only one
B. Multiple via tuple
C. None
D. One integer
Answer: B -
What does the def keyword do?
A. Calls function
B. Ends function
C. Defines function
D. Returns result
Answer: C -
What happens if you call a function before it's defined?
A. It works
B. NameError
C. SyntaxError
D. None
Answer: B -
Which function calculates the factorial of a number?
A. fact()
B. calcFact()
C. factorial()
D. pow()
Answer: B -
What is the scope of a variable defined inside a function?
A. Global
B. Local
C. Universal
D. Restricted
Answer: B -
Which statement imports a module in Python?
A. define module
B. import
C. add module
D. include module
Answer: B -
What is the name of the parameter receiving an argument?
A. value
B. reference
C. parameter
D. argument
Answer: C -
Which will correctly return area and perimeter from a single function?
A. return area + perimeter
B. return (area, perimeter)
C. print area, perimeter
D. save area, perimeter
Answer: B -
A function with default parameter values can skip arguments:
A. True
B. False
Answer: A -
Module containing math functions is called:
A. maths
B. math
C. statistics
D. numbers
Answer: B -
Which function rounds up a float to the next integer?
A. ceil()
B. round()
C. floor()
D. int()
Answer: A -
What is a module in Python?
A. A number
B. A collection of functions
C. An operator
D. A string
Answer: B -
What does random.randint(1,5) return?
A. A float
B. An int between 1 and 5
C. A string
D. Error
Answer: B -
Which function is used to find the mean in the statistics module?
A. statistics.mean()
B. mean()
C. average()
D. sum()
Answer: A -
What happens if you import a module twice?
A. Loads twice
B. Loads only once
C. Throws error
D. Reloads variables
Answer: B -
What will print(pow(2,3)) output?
A. 8
B. 6
C. 9
D. 7
Answer: A -
How is the floor value found?
A. Using math.ceil()
B. Using math.floor()
C. math.int()
D. floor()
Answer: B -
What does the id() function return?
A. Type of variable
B. Memory address
C. Variable name
D. Reference
Answer: B -
Which operator concatenates strings?
A. +
B. &
C. |
D. ,
Answer: A -
What is the built-in function for input?
A. user()
B. input()
C. read()
D. enter()
Answer: B -
Function header ends with:
A. ;
B. .
C. :
D. ,
Answer: C -
What happens if parameters are missing in a call without defaults?
A. Uses 0
B. Error
C. Ignores
D. Assumes None
Answer: B -
What can a function parameter be?
A. Only int
B. Any data type
C. Only string
D. Only list
Answer: B -
What is the result of print(max(3, 7, 5))?
A. 3
B. 5
C. 7
D. Error
Answer: C -
What does the len() function do?
A. Adds numbers
B. Counts elements
C. Deletes strings
D. Multiplies values
Answer: B -
Which can be used to import only specific functions from a module?
A. from … import …
B. define … functions
C. only … import …
D. func … import …
Answer: A -
If you need only the sqrt and ceil from math module, which is correct?
A. import math.sqrt, math.ceil
B. from math import sqrt, ceil
C. import math
D. math.sqrt, math.ceil
Answer: B -
A function can be called from:
A. Only main
B. Another function
C. Only outside
D. Only inside
Answer: B -
What is the output of print(type("hello"))?
A. str
B. int
C. float
D. list
Answer: A -
What is a function call in Python?
A. Defining function
B. Passing control to function
C. Ending program
D. Raising error
Answer: B -
Function arguments and parameters must be in:
A. Same order
B. Any order
C. Ignored order
D. Only reversed
Answer: A -
What is modular programming?
A. Using many variables
B. Dividing program into blocks
C. Writing long code
D. Ignoring errors
Answer: B -
What is the output of divmod(7,2)?
A. (3, 1)
B. 3.5
C. (1, 3)
D. (2, 5)
Answer: A -
What function sorts a list in ascending order?
A. list.sort()
B. sort()
C. sorted()
D. arrange()
Answer: A -
What does a docstring describe?
A. A module or function
B. A variable
C. An error
D. A loop
Answer: A -
What is a built-in function?
A. Custom written function
B. Predefined function provided by Python
C. Variable
D. Loop
Answer: B
1 mark questions with answers
-
What keyword is used to define a function in Python?
Answer: def -
What is a function in Python?
Answer: A named group of instructions that performs a specific task. -
What is the output if a function doesn’t have a return statement?
Answer: None -
What is modular programming?
Answer: Dividing a program into independent blocks (functions). -
Name the function used to accept user input in Python.
Answer: input() -
Can a function have no parameters?
Answer: Yes -
What does a return statement do in a function?
Answer: Returns value to the caller and exits the function. -
What type of value can be returned by a function?
Answer: Any type (number, string, tuple, etc.) -
Which function returns the length of a list?
Answer: len() -
What is the scope of a variable defined inside a function?
Answer: Local scope -
How do you call a function named myFun?
Answer: myFun() -
What is a default parameter?
Answer: A pre-assigned value for a parameter in a function. -
Name one built-in mathematical module in Python.
Answer: math -
What does print() function do?
Answer: Displays output to the screen. -
What keyword makes a variable refer to the global scope inside a function?
Answer: global -
Which operator is used to concatenate strings?
Answer: + -
What is the docstring in Python?
Answer: Documentation string for function/module. -
Is it possible to return multiple values from a function?
Answer: Yes (using tuple). -
How many times is a module loaded if imported twice?
Answer: Only once -
What is a parameter in Python?
Answer: Variable in function definition that receives argument value.
2 mark questions with answers
What is modular programming?
Answer:
Dividing a program into separate blocks (functions) that perform specific tasks, making the code organized and reusable.Explain the use of the 'def' keyword in Python with an example.
Answer:
'def' starts a function definition. Example:
pythondef greet(): print("Hello")
What is a parameter and what is an argument in a function?
Answer:
A parameter is a variable in the function definition; an argument is the value passed to that parameter during a function call.Can a function return multiple values? How?
Answer:
Yes, by returning a tuple. Example:
pythondef minmax(a, b): return min(a,b), max(a,b)
Write a user-defined function to add two numbers.
Answer:
pythondef add(x, y): return x + y
Describe the use of the return statement in Python functions.
Answer:
It sends the function’s result back to the caller, optionally exiting the function.What are default parameters?
Answer:
Parameters with preset values, used if no argument is given in the function call.Differentiate between global and local variables.
Answer:
Global: Defined outside functions, accessible everywhere. Local: Defined inside functions, accessible only there.Is it necessary for a function to always return a value?
Answer:
No, functions can be void (no return value).What does the len() function do?
Answer:
Returns the number of items in a sequence (e.g. list or string).How do you import the math module in Python?
Answer:
Usingimport math.Show with code how to return the area and perimeter of a rectangle from a function.
Answer:
pythondef rect(l, w): return l*w, 2*(l+w)
Give the function header for a function to calculate compound interest.
Answer:
pythondef compound_interest(principal, rate, time, n):
What is the use of docstring in a function/module?
Answer:
Acts as documentation for describing the function or module.What happens when a function call precedes its definition in code?
Answer:
Raises a NameError, since Python requires functions to be defined before they are called.Describe the scope of a variable inside a function.
Answer:
It is local to that function and not accessible outside.How can you import only sqrt and ceil from the math module?
Answer:from math import sqrt, ceilShow the syntax for a function with no parameters and no return value.
Answer:
pythondef display(): print("Hello")
How do you call a function named 'show' with parameter 'x' as 5?
Answer:show(5)What will
print(max(5, 7, 2))display?
Answer:
7
Here are 20 three-mark (3 marks) questions with detailed answers from "kecs107.pdf" (Functions in Python):
Explain with example the concept of modular programming.
Answer: Modular programming divides code into independent blocks called functions. Example:
pythondef add(x, y): return x + y def subtract(x, y): return x - y
In a program, these functions can be reused and called as needed, improving organization and reusability.
Write a user-defined function to find the mean of a list of values.
Answer:
pythondef myMean(myList): total = sum(myList) count = len(myList) mean = total/count return mean
Call: myMean([2.5, 3.5, 4.0]) returns 3.33.
How does the flow of execution change when a function is called in Python? Illustrate.
Answer: When a function is called, control jumps to its definition, executes statements, then returns to the caller.
Example:
pythonprint("Start") def hello(): print("Hi") hello() print("End")
Order: "Start", "Hi", "End".
What are parameters and arguments? Differentiate with example.
Answer: Parameters are variables in function headers; arguments are actual values supplied in calls.
Example:
pythondef greet(name): print("Hello", name) greet("Ali")
Create a function that calculates both area and perimeter of a rectangle and returns both.
Answer:
pythondef rect(l, w): area = l * w peri = 2*(l + w) return area, peri
Calling rect(3,4) returns (12, 14).
Demonstrate with code how default parameters work in Python functions.
Answer:
pythondef greet(name, msg="Hello"): print(msg, name) greet("Ali") # Prints: Hello Ali greet("Ali", "Welcome") # Prints: Welcome Ali
Describe local and global variables with example. What is the outcome if names are reused?
Answer:
Global: Defined outside any function. Local: within a function.
pythonx = 10 def func(): x = 5 print(x) func() # prints 5 print(x) # prints 10
Local x does not affect global x.
Write a function to compute and return factorial of a number.
Answer:
pythondef calcFact(n): fact = 1 for i in range(n, 0, -1): fact *= i return fact
For n = 5, returns 120.
Explain the use of return statement in a function with an example.
Answer: The return sends a value back to the caller, and ends function execution.
Example:
pythondef square(n): return n*n
Calling square(5) returns 25.
List three advantages of using functions in programming.
Answer:Improves code readability
Enables code reuse
Simplifies testing and debugging
Write a function that swaps two numbers if the first is less than the second, else keeps order.
Answer:
pythondef swap(a, b): if a < b: return b, a else: return a, b
Describe the role of Python standard library modules with examples.
Answer: Standard libraries provide ready-made functions, e.g.,math.sqrt(16)returns 4;random.randint(1,5)returns a random int between 1 and 5.Write a function to return quotient and remainder for given two numbers.
Answer:
pythondef division(a, b): if b != 0: return a // b, a % b else: return None, None
With code, differentiate between built-in and user-defined functions.
Answer:
Built-in:print(),sum()available in Python. User-defined:
pythondef add(x, y): return x + y
Explain the concept and use of docstrings in modules/functions.
Answer: Docstrings document a function/module. Example:
pythondef add(x, y): """Returns the sum of x and y""" return x + y
Accessed by print(add.__doc__)
Create a function for simple authentication and display proper messages.
Answer:
pythondef login(uid, pwd): if uid == "ADMIN" and pwd == "St0rE1": return "Login successful" else: return "Access denied"
Write a program for compound interest calculation using a function.
Answer:
pythondef comp_int(P, R, T, N): CI = P*(1 + R/N)**(N*T) return CI
comp_int(1000, 0.05, 2, 1) returns final amount.
Show how to import only specific functions from a module. Give example.
Answer:
Syntax:from math import sqrt, ceil
Now use:sqrt(25),ceil(4.2)directly.Write a function to simulate a traffic light. Explain its use.
Answer:
pythondef traffic_light(signal): if signal == "RED": return "STOP" elif signal == "YELLOW": return "WAIT" elif signal == "GREEN": return "GO" else: return "Invalid signal"
Used for safety control and decision making.
Explain how a function can be designed to handle and display mixed fractions. Give code.
Answer:
pythondef mixedFraction(num, deno=1): if num % deno != 0: quotient = num // deno remainder = num % deno print(f"Mixed Fraction: {quotient} {remainder}/{deno}") else: print("Whole Number") mixedFraction(17, 2) # Output: Mixed Fraction: 8 1/2
3 marks questions with detailed answers
Explain with example the concept of modular programming.
Answer: Modular programming divides code into independent blocks called functions. Example:
pythondef add(x, y): return x + y def subtract(x, y): return x - y
In a program, these functions can be reused and called as needed, improving organization and reusability.
Write a user-defined function to find the mean of a list of values.
Answer:
pythondef myMean(myList): total = sum(myList) count = len(myList) mean = total/count return mean
Call: myMean([2.5, 3.5, 4.0]) returns 3.33.
How does the flow of execution change when a function is called in Python? Illustrate.
Answer: When a function is called, control jumps to its definition, executes statements, then returns to the caller.
Example:
pythonprint("Start") def hello(): print("Hi") hello() print("End")
Order: "Start", "Hi", "End".
What are parameters and arguments? Differentiate with example.
Answer: Parameters are variables in function headers; arguments are actual values supplied in calls.
Example:
pythondef greet(name): print("Hello", name) greet("Ali")
Create a function that calculates both area and perimeter of a rectangle and returns both.
Answer:
pythondef rect(l, w): area = l * w peri = 2*(l + w) return area, peri
Calling rect(3,4) returns (12, 14).
Demonstrate with code how default parameters work in Python functions.
Answer:
pythondef greet(name, msg="Hello"): print(msg, name) greet("Ali") # Prints: Hello Ali greet("Ali", "Welcome") # Prints: Welcome Ali
Describe local and global variables with example. What is the outcome if names are reused?
Answer:
Global: Defined outside any function. Local: within a function.
pythonx = 10 def func(): x = 5 print(x) func() # prints 5 print(x) # prints 10
Local x does not affect global x.
Write a function to compute and return factorial of a number.
Answer:
pythondef calcFact(n): fact = 1 for i in range(n, 0, -1): fact *= i return fact
For n = 5, returns 120.
Explain the use of return statement in a function with an example.
Answer: The return sends a value back to the caller, and ends function execution.
Example:
pythondef square(n): return n*n
Calling square(5) returns 25.
List three advantages of using functions in programming.
Answer:Improves code readability
Enables code reuse
Simplifies testing and debugging
Write a function that swaps two numbers if the first is less than the second, else keeps order.
Answer:
pythondef swap(a, b): if a < b: return b, a else: return a, b
Describe the role of Python standard library modules with examples.
Answer: Standard libraries provide ready-made functions, e.g.,math.sqrt(16)returns 4;random.randint(1,5)returns a random int between 1 and 5.Write a function to return quotient and remainder for given two numbers.
Answer:
pythondef division(a, b): if b != 0: return a // b, a % b else: return None, None
With code, differentiate between built-in and user-defined functions.
Answer:
Built-in:print(),sum()available in Python. User-defined:
pythondef add(x, y): return x + y
Explain the concept and use of docstrings in modules/functions.
Answer: Docstrings document a function/module. Example:
pythondef add(x, y): """Returns the sum of x and y""" return x + y
Accessed by print(add.__doc__)
Create a function for simple authentication and display proper messages.
Answer:
pythondef login(uid, pwd): if uid == "ADMIN" and pwd == "St0rE1": return "Login successful" else: return "Access denied"
Write a program for compound interest calculation using a function.
Answer:
pythondef comp_int(P, R, T, N): CI = P*(1 + R/N)**(N*T) return CI
comp_int(1000, 0.05, 2, 1) returns final amount.
Show how to import only specific functions from a module. Give example.
Answer:
Syntax:from math import sqrt, ceil
Now use:sqrt(25),ceil(4.2)directly.Write a function to simulate a traffic light. Explain its use.
Answer:
pythondef traffic_light(signal): if signal == "RED": return "STOP" elif signal == "YELLOW": return "WAIT" elif signal == "GREEN": return "GO" else: return "Invalid signal"
Used for safety control and decision making.
Explain how a function can be designed to handle and display mixed fractions. Give code.
Answer:
pythondef mixedFraction(num, deno=1): if num % deno != 0: quotient = num // deno remainder = num % deno print(f"Mixed Fraction: {quotient} {remainder}/{deno}") else: print("Whole Number") mixedFraction(17, 2) # Output: Mixed Fraction: 8 1/2
5 marks questions with detailed answers
Discuss modular programming and demonstrate how functions make programs organized and reusable.
Answer:
Modular programming divides a large program into independent blocks, each performing a specific task called a function. This approach makes code more organized, readable, and maintainable. For example, in a tent manufacturing program, separate functions can calculate canvas area, cost, and tax. These can be reused for new products, reducing effort and errors.Write a Python program using user-defined functions to calculate the area and cost of making a tent as described in the text.
Answer:
pythondef cyl_area(h, r): return 2*3.14*r*h def con_area(r, l): return 3.14*r*l def post_tax(cost): return cost + 0.18*cost h = float(input("Cyl height: ")) r = float(input("Radius: ")) l = float(input("Slant height: ")) canvas_area = cyl_area(h,r) + con_area(r,l) unit_price = float(input("Unit price: ")) total_cost = unit_price * canvas_area net_payable = post_tax(total_cost) print("Total area:", canvas_area) print("Total cost:", total_cost) print("Net payable:", net_payable)
This breaks the problem into logical functions, each handling one part.
Explain with code how arguments and parameters work in Python functions, including passing and receiving values.
Answer:
Arguments are values passed to functions during calls; parameters are variables receiving these values.
pythondef sum_of_n(n): total = 0 for i in range(1,n+1): total += i return total num = int(input("n: ")) print(sum_of_n(num)) # argument num assigned to parameter n
Shows both passing, receiving, and using arguments.
Describe how default parameters work. Give an example where the default is used and where it is overwritten.
Answer:
Default parameters allow functions to have preset values.
pythondef power(base, exp=2): return base**exp print(power(3)) # Uses default exp=2 → 9 print(power(3,4)) # Overwrites default → 81
First call omits exp, so default 2 is used; second provides exp=4.
Explain the difference between local and global variable scope in functions, with detailed code illustrations.
Answer:
Global variables are defined outside functions and accessible throughout; local exist within functions only.
pythonx = 10 def test(): x = 5 print("local:", x) test() # prints 5 print("global:", x) # prints 10 def mod_global(): global x x = 15 mod_global() print("changed global:", x) # prints 15
Local x doesn't affect global x unless specified with "global".
Design a menu-driven calculator using functions for basic arithmetic and trigonometric operations.
Answer:
pythonimport math def add(a, b): return a + b def sub(a, b): return a - b def mul(a, b): return a * b def div(a, b): return a / b if b != 0 else "Error" def trig(x, op): if op=='sin': return math.sin(x) elif op=='cos': return math.cos(x) elif op=='log': return math.log10(x) while True: choice = input("Choose op (+,-,*,/,sin,cos,log,exit): ") if choice == 'exit': break if choice in ('sin','cos','log'): x = float(input("Value: ")) print(trig(x, choice)) else: a = float(input("a: ")) b = float(input("b: ")) if choice=='+': print(add(a,b)) elif choice=='-': print(sub(a,b)) elif choice=='*': print(mul(a,b)) elif choice=='/': print(div(a,b))
Describe how to use docstrings in functions and modules. Write sample code and show how to display the docstring.
Answer:
Docstrings are the first line in a function/module, inside triple quotes, explaining its purpose.
pythondef rectangle_area(l, w): """Calculates area of rectangle""" return l * w print(rectangle_area.__doc__)
Prints: "Calculates area of rectangle"
Write a Python module named basicmath with four arithmetic operations and show how to import and use it.
Answer:
Module basicmath.py:
python"""Basic Math Operations Module""" def add(x, y): return x + y def sub(x, y): return x - y def mul(x, y): return x * y def div(x, y): return x / y if y != 0 else "Division by Zero Error"
Import and use:
pythonimport basicmath print(basicmath.add(5,2)) print(basicmath.div(5,0))
Also, display docstring: print(basicmath.__doc__)
Explain built-in vs user-defined functions with code and use-cases.
Answer:
Built-in functions (input, sum, print) are provided by Python; user-defined are created by the programmer.
Built-in:
pythona = int(input("Enter value: ")) print(abs(a))
User-defined:
pythondef cube(n): return n*n*n print(cube(5))
Built-ins are for common tasks, user-defined for specific needs.
Write a function that simulates a traffic light, returning different messages based on light color input.
Answer:
pythondef traffic_light(color): if color == "RED": return "STOP, your life is precious." elif color == "YELLOW": return "WAIT, till it's green." elif color == "GREEN": return "GO! Thank you for being patient." else: return "Invalid color" signal = input("Enter color: ").upper() print(traffic_light(signal)) print("SPEED THRILLS BUT KILLS")
Write a function to determine if a student has short attendance given working days and absences. Explain logic and output.
Answer:
pythondef short_attendance(total_days, days_present): attendance_perc = (days_present/total_days)*100 return 1 if attendance_perc < 78 else 0 # 1 means short, 0 means sufficient attendance
Function calculates attendance percentage, returns 1 if below 78%.
Create a GK quiz function set with scoring and remarks; display final score and feedback.
Answer:
pythondef score(ans_list, correct_list): return sum([a==b for a,b in zip(ans_list, correct_list)]) def remark(score_val): marks = {5:"Good",4:"Outstanding",3:"Excellent",2:"Read more",1:"Needs interest",0:"Take it seriously"} print(marks.get(score_val, "Invalid")) correct = ['A','B','A','C','D'] user_ans = ['A','B','A','B','D'] s = score(user_ans, correct) remark(s)
Displays remark based on score.
Write a function to swap two numbers only if the first is less than the second. Explain with sample input and output.
Answer:
pythondef conditional_swap(a, b): if a < b: return b, a else: return a, b # Example: conditional_swap(2,5) returns (5,2)
Write a program using functions to display the first n Fibonacci numbers. Explain your solution.
Answer:
pythondef fibonacci(n): a, b = 1, 1 print(a, b, end=' ') for i in range(n-2): a, b = b, a+b print(b, end=' ') fibonacci(10)
Starts with 1, 1 and prints next n-2 terms by summing last two.
Design a function-based menu system to calculate area/perimeter for multiple shapes, using function import.
Answer:
Module shapes.py:
pythondef area_square(a): return a*a def area_rect(l,w): return l*w def area_circle(r): return 3.14*r*r def peri_rect(l,w): return 2*(l+w) def peri_circle(r): return 2*3.14*r
Import and use:
pythonfrom shapes import area_square, peri_circle print(area_square(5)) print(peri_circle(7))
Explain the concept of returning multiple values from a function with code and real-life application.
Answer:
Python can return multiple values as a tuple.
pythondef stats(l): return min(l), max(l), sum(l)/len(l) result = stats([2,4,7]) print("Min:", result[0], "Max:", result[1], "Mean:", result[2])
Design a login function to block account after three failed attempts; show logic and typical output.
Answer:
pythondef login(uid, pwd): attempts = 0 while attempts < 3: if uid == "ADMIN" and pwd == "St0rE1": print("Login successful") return True else: attempts += 1 if attempts == 3: print("Account blocked") return False uid = input("User ID: ") pwd = input("Password: ")
Prompts for credentials, blocks account after 3 failures.
Tell how functions increase team collaboration and parallel work, giving practical examples.
Answer:
Functions divide large tasks between team members; each person writes functions independently for assigned features (billing, reporting, calculations), then integrates into the main program, speeding up development and reducing conflicts.Describe the flow of execution in a function-based Python program, highlighting function calls and returns.
Answer:
Interpreter reads each line; on encountering a function call, jumps to function, executes code, returns result, resumes main code. Order is influenced by call hierarchy; for recursion, functions call themselves until base case met.Explain the composition of functions in Python, showing dependency and nested calls with examples.
Answer:
Function composition is calling one function from another, passing outputs as input.
pythondef square(x): return x*x def cube(x): return x*square(x) print(cube(3)) # cube calls square(3)
.png)
Comments
Post a Comment