Chapter 3 - Working With Functions
Chapter 3 - Working With Functions
A function is a small unit of a program that processes the data and often returns a value.
Parts of Function
Function Header
Always starts with the “def” keyword followed by the function name and its parameters,
ends with a colon (:)
Parameters
Variables Supplied in brackets of the function header
Function Body
Block of statements/instructions that define the action performed by the function, indentation
must be followed
Indentation
White space at the beginning of every statement with the same block
Function Calling
writing function name including ()
The following five basic steps are used to create and invoke a function.
After writing the function it must be invoked through calling by following these steps:
statement it just executes the function header line and skips all statements in the function
body these statements execute when a function will be called
Theory-based questions
1. What is a function?
Ans.:
A function is a set of instructions or subprograms that are used to fulfil the user’s need.
In other words, a function is a bunch of code which performs a specific task.
A function is used to do a specific task and divide the large program into smaller blocks.
A function is a small unit of a program that processes the data and often returns a value.
2. Why do programmers need functions in python programming?
Ans.:
To make the program easy
Divide the large program into a small block of codes
Reduce the lines of code
Easy to update
The single-line comment is written by # followed by the text. The multiline comments start
with ”’ and are followed by the text then ends with ”’.
Ans.: The physical line structure of a program contains the lines of codes. Physical lines are
the lines which you can see on the screen in a python program.
In other words, a physical line contains a set of characters that ends with an EOL character.
The EOL character is newline generally ‘\n’ in python.
Example:
>>> name=’Chapter 3 Working with functions’
>>> print(name)
Chapter 3 Working with functions
Parameters Arguments
13. What is the local variable and global variable? Explain with an example.
Ans.:
Global Variable: A variable that is declared in top-level statements is called a global variable.
To access the value of a global variable user need to write a global keyword in front of the
variable in a function.
Local Variable: A name declared in a specific function body is called a local variable.
In the next section of Working with functions Class 12 questions, I will cover some output
and error-based questions. Here we go!
14. What are the rules for combining three types of arguments in a Python function?
Ans.: The following rules need to be followed for combining three types of arguments in a
python function.
1. An argument list must contain positional arguments followed by any keyword argument.
Ex.:
cal_discount(amt=500,rate=10) or cal_discount(rate=10,amt=500)