Functions

1. Explain Python functions.
Python functions is a block of statements that return the specific task.
2. A key benefit of using functions is enhancing readability. Elaborate on this benefit.
  • Functions enhance code readability by providing a clear structure and encapsulating specific functionality.
  • Breaking down a program into smaller, purposeful units makes the codebase more comprehensible.
  • Clearly named functions act as self-documenting elements, enabling developers to understand different parts of the code more easily.
3. A key benefit of using functions is reusability. Elaborate on this benefit.

A function can be reused in different parts of your program or even in other projects. This..

  • avoids duplicating code and code redundancies.
  • simplifies the maintenance as we only need to update he code within the function. The changes will automatically propagates to all instances.
4. A key benefit of using functions is abstraction. Elaborate on this benefit.
Functions abstract away the implementation details, allowing you to use a function without needing to understand its internal workings.
5. Provide the Python function declaration structure.
def function_name(parameters):
	<statements>
	return <expression>
6. Explain the print() function.
The print() function in Python is used to output text or other data to the console, making it visible to the user.
7. Explain the return statement.
Return is a keyword used to exit from a function and return the value or data item to the caller.
8. Explain the key differences between a print function vs return statement.
  • print is used for displaying content to the console and does not affect the flow of the program.
  • return is used within functions to specify the value that the function should produce and send back to the caller. The return statement will change the flow of the program as it effectively terminates the function’s execution.
9. Explain the concept of first class functions (FCF).

All functions in python can be passed around and manipulated like any other objects.

This indicates that functions can be..

  • used as parameters
  • used as a return value
  • assigned to variables
  • stored in data structures such as lists and tuples.
10. Explain the term higher order functions.
A function that can take one or more functions as argument.
11. Provide the syntax to return the list of valid attributes of a function.
dir(my_func)
12. Explain the term callable.
In Python, a “callable” refers to an object that can be called as a function. In other words, it’s an object that can be invoked or called using parentheses ().
13. Provide the syntax to evaluate whether an object is an callable.
callable(my_func)
Last updated on 19 Nov 2023