You are on page 1of 6

Function Definition

A function is a small unit of a program that processes the data and often returns a value.

The need for functions


● Easy program handling
● Reduce the size of the program
● Reduce the repeated statements
● Ambiguity can be reduced
● Make the program more readable and understandable
How to create a function in Python?
To create a function in Python consider the following parts of a function:

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:

Save a program and click run or press the F5 button


Now interactive mode will appear with the message RESTART ……
Write a function call statement as shown in the below image
A function call statement is just like a function name with required parameters
Press enter and supply input as per requirements
Structure of Python Program
A Python program is a set of a few statements and blocks. A Python program may have the
following:
Physical line structure: A Python program is divided into no. of logical lines, the logical line
is created from one or more physical lines
Joining two lines: A logical line can be broken into two or more physical lines using a
backslash (\)
Multiple statements on a single line: Semicolon (;) is used to write multiple statements on
a single line
The top-level statement or _main_: Unindented statements
Comments: Begins with a hash symbol (#), python interpreter ignores them, multi-line
comments will be written in “”” (triple-double quotes).
Indentation: White spaces used at the beginning of every line. The indented part is known
as one block.

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

3. How to create a function in python? Explain in detail.


Ans.: To create a function in python follow the given steps:
Start the code with def followed by function name and supply input through the parameters.
Write the set of statements to be executed in the program.
Call the function to invoke or execute the statements.

4. What are the parts of functions? Explain with a suitable example.


Ans.: The arts of functions are as follows:
Function header: It starts with def followed by the function name and required parameters.
Parameters are the input variables written into brackets. These parameters are supplied in
the function calling statement.
Function Body: This is the main part of a program. It contains the main block of the
program. The set of instructions such as calculations, logical comparisons etc is written here
in this part. It ends with a return statement. Indentation is very important for the function
body.
Function calling statement: This is the final part of a function. It invokes the function and
returns the output as instructed into the function body.

5. How to call a function? Write steps.


Ans: Calling a function is an easy task. Here are the steps:
Write the function name.
Supply the values of the parameters, if any.
6. What are the comments? What are the role comments in the program? How to write
single-line comments and multi-line comments?
Ans: Comments are the additional explanatory text written in the function to provide some
description or explanation about the statement or block of code.

Comments play an important role in understanding a program. It provides a descriptive


explanation of the written statements. It makes the intention very clear for what the
statement is used or what process is going to be done.

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 ”’.

7. Explain the physical line structure of a program. Illustrate with an example.


execution in the function call statement.

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

The above statement is same as:


>>> name=’Chapter 3 Working with functions’; print(name)
Ans.:
A function in the python program is called by a function call statement
To call a function, write the function name followed by parameter values in brackets
A block of statements executed in the execution frame
When a function is called, an execution frame is created and controls the transfer
Within the execution frame, the statements written in the function body are executed and
return a value or execute the last statement
Python follows top to bottom approach for executing program
Comments are ignored in execution
If python notices function definition with a def 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
8. Illustrate the flow of execution in the function call statement.
Ans.:
● A function in the python program is called by a function call statement
● To call a function, write the function name followed by parameter values in brackets A
block of statements executed in the execution frame
● When a function is called, an execution frame is created and controls the transfer
● Within the execution frame, the statements written in the function body are executed
and return a value or execute the last statement
● Python follows top to bottom approach for executing program
● Comments are ignored in execution
● If python notices function definition with a def 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

9. Write and explain the types of functions supported by python.


Ans.:
Built-in Functions: Pre-defined functions of python such as len(), type(), input() etc.
Functions defined in modules: Functions defined in particular modules, can be used when
the module is imported. A module is a container of functions, variables, constants, and
classes in a separate file which can be reused.
User-Defined Functions: Function created by the programmer

10. Write the ways of import module in the python program.


Ans.:
The module can be imported in two ways
import statement: Used to import the entire module. EX. import math
from statement : import all functions or the selected one. EX. from random import randint
11. Differentiate between parameters and arguments.

Parameters Arguments

These are specified Values passed during


during the function the function call.
definition.

They are also known as They are also known


formal parameters. as actual parameters.
These variables help in These variables are
the complete execution passed to the function
of the function. for execution.

The values contained by The arguments are


these parameters can accessible throughout
only be accessed from the program
function return depending upon the
statements or if the scope of the variable
scope of these assigned.
parameters is made
global.

The values passed as Every argument is


parameters are local assigned to a
variables and are parameter when the
assigned values of the function is defined.
arguments during the
function call.
12. What are the arguments supported by python? Explain each of them with a suitable
example.
Ans.:Python supports four argument types:

Positional Arguments: Arguments passed to a function in correct positional order, no. of


arguments must match with no. of parameters required.
Default Arguments: Assign a default value to a certain parameter, it is used when the user
knows the value of the parameter, default values are specified in the function header. It is
optional in the function call statement. If not provided in the function call statement then the
default value is considered. Default arguments must be provided from right to left.
Key Word Arguments: Keyword arguments are the named arguments with assigned values
being passed in the function call statement, the user can combine any type of argument.
Variable Length Arguments: It allows the user to pass as many arguments as required in the
program. Variable-length arguments are defined with the * symbol.

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.

2. Keyword arguments should be taken from the required arguments preferably.

3. Value of the argument can’t be specified more than once.

Ex.:

cal_discount(amt=500,rate=10) or cal_discount(rate=10,amt=500)

You might also like