0% found this document useful (0 votes)
8 views

Function Notes

Chapter 2 discusses functions in programming, defining a function as a named group of statements that perform a specific task when invoked. It explains the structure of user-defined functions, including function headers, parameters, function bodies, and indentation. Additionally, it highlights the advantages of using functions, types of user-defined functions, and concepts of positional and default arguments with examples.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Function Notes

Chapter 2 discusses functions in programming, defining a function as a named group of statements that perform a specific task when invoked. It explains the structure of user-defined functions, including function headers, parameters, function bodies, and indentation. Additionally, it highlights the advantages of using functions, types of user-defined functions, and concepts of positional and default arguments with examples.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapter -2 Functions

Q-1 Explain function with the help of an example?


Ans-Function: A function is a named group of statements/instructions that perform some specific task
when it is invoked.
Example:
def display():
print(“Hello”)
display()
Q-2 Explain the Structure of function?
Ans-The syntax for creating a user defined function is as follows:
def <function name> ( [parameter1,parameter2,…..]):
<set of statements to be executed>
[ return <value> ]
 Item(s) inside angle brackets < > has to be provided by the programmer
 Items(s) inside square brackets [ ] is optional i.e. can be omitted.
 Items/words/punctuators outside < > [ ] have to be written as specified.
Function Header-The first line of the function definition that begins with keyword def and ends with a
colon : specifies the name of the function and its parameters.
Parameters-Variables which are listed within the parenthesis of a function header.
Function Body-The block of statements beneath function header that defines the action performed by the
function. The function body may or may not return any value. A function returns a value through a return
statement.
Indendation-The blank statements in the beginning of a statement(convention is 4 spaces) within a
block.All statements within same block have same indentation.n it is invoked.

Q-3 Write some advantages of functions in a program?


Ans- Advantages of using functions in a program:
• Increases readability , particularly for longer code as by using functions, the program is better organized
and easy to understand.
• Reduces code length as same code is not required to be written at multiple places in a program. This
also makes debugging easier.
• Increases reusability, as function can be called from another function or another program. Thus, we can
reuse or build upon already defined functions and avoid repetitions of writing the same piece of code.
• Work can be easily divided among team members and completed in parallel.
Q-4 Give examples of different types User-defined functions?

Ans- Types of User-defined functions


(i) Function with no argument and no return value
def display() :
print(‘Welcome to python programming’)

display()
(ii) Function with argument(s) but no return value
def calc_square(b):
c=b**2
print(c)

a=4
cal_square(a)
(iii) Function with no argument but returns value(s)
def calc_cube():
n=3
c=n**3
return c

m=calc_cube()
print(m)
(iv) Function with argument(s) and returns value(s)
def calc_square(b):
c=b**2
return c

a=4
s=cal_square(a)
print(s)
Q-5 Explain Positional arguments with the help of an example?
Ans- Positional Arguments ( Required Arguments / Mandatory Arguments): When the function call
statement must match the number and order of arguments as defined in the function definition , this is
called the positional argument matching. In such function calls .
 The arguments must be provided for all parameters(Required)
 The values of arguments are matched with the parameters, position (order) wise (Positional)
This way of parameter and argument specification is called Positional arguments or Required arguments or
Mandatory arguments as no value can be skipped from function call or order cannot be changed e.g., you
cannot assign value of the first argument to third parameter.e.g,. If function definition is like
def display(a , b , c ) :
……
..….
Then possible function calls for this can be:
display ( x , y , z) # 3 values ( all variables ) passed
display ( 2 , y , z) # 3 values ( literal + variables ) passed
display ( 2 , 5 , 7) # 3 values ( all literals ) passed

Q-6 Explain Default arguments with the help of an example?


Ans- A parameter having default value in the function header is known as a default parameter.
Python allows us to assign default value(s) to a function’s parameter(s) which is useful in a case matching
argument in the function call statement. The default vales are specified in the function header of function
definition .Following is an example of function header with default values :
def interest ( principal, time , rate = 0.10):
…………
si= interest(5400 , 2) # third argument missing
si= interest(6100, 3, 0.15) # no argument missing

In the first function call value 5400 is passed to parameter principal, the value 2 is passed to the second
parameter time and since the third argument rate is missing, its default value 0.10 is used for rate. But if a
function call provides all three arguments as shown in second function call then the principal gets value
6100, time gets 3 and the parameter rate gets value 0.15. That means default values assigned in a function
header are considered only if no value is provided for that parameter in the function call.

You might also like