0% found this document useful (0 votes)
14 views17 pages

Ch2a Study Material

The document provides an overview of functions in programming, covering types of functions, user-defined functions, flow of execution, parameters, and variable scope. It includes definitions, differences between arguments and parameters, and examples of function usage. Key points emphasize the importance of function definition order, parameter types, and the distinction between global and local variables.

Uploaded by

falak.pasha7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

Ch2a Study Material

The document provides an overview of functions in programming, covering types of functions, user-defined functions, flow of execution, parameters, and variable scope. It includes definitions, differences between arguments and parameters, and examples of function usage. Key points emphasize the importance of function definition order, parameter types, and the distinction between global and local variables.

Uploaded by

falak.pasha7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

CH-2a FUNCTIONS - Basics

CHAPTER-2 : FUNCTIONS
WEIGHTAGE: 10 Marks
SYLLABUS OF FUNCTIONS CHAPTER
● Types of function (built-in functions, functions defined in module,
user defined functions),

● creating user defined function

● flow of execution,

● arguments and parameters,

● Types of parameters: default parameters, positional parameters,

● function returning value(s),

● scope of a variable (global scope, local scope)


1. Format /Syntax of FUNCTION Parameters- refer to the
def functionname(parameters) : variables which receive
Code1
values(i.e inputs)
Code2
Code3

Function header
Function body

Function call statement


2a. Define FUNCTION
b. Difference between arguments and parameters.

Ans:
a. Function refers to a set of codes (or) statements which perform a particular task.

b. variables present in the function header statement


are referred as parameters.

data/variables present in the function call statement are


referred as arguments.
3. Write flow of execution for the following program:

Ans:
stmt1 → stmt6 —> stmt2,3,4,5 —->stmt7—> stmt2,3,4,5→stmt8
4. Difference between Default and Positional parameters.

Ans:
Default parameters are those parameters for which the values are already assigned.

Positional parameters are those parameters for which the inputs to be passed in correct order.
5. Can a function return three values.

Ans:
Yes.

Function can return any number of values.


6. Can we write return statement without any value.

Ans:
Yes.

Working of return statement:


return statement makes the control to come from out a
function and then it returns the specified value(s) to the
calling statement.

Note: return statement should be the last line in a function


7. Identify the invalid Function Header(s) from the following options:
Options:
(a) def Calculate(principal, rate=0.08, time):
(b) def Calculate(principal=10000, rate=0.08, time):
(c) def Calculate(principal, rate, time=3):
(d) def Calculate(principal=10000, rate, time=3):

Ans:
Option c

We know the rule that: “default parameters should be at the last in parameter list”
8. What is the output of the following program?
def say(message, times = 1):
print(message * times , end ='')
say(‘Hello and’)
say('World', 5)

Options:
(a) Hello and WorldWorldWorldWorldWorld
(b) Hello and World 5
(c) Hello and World,World,World,World,World
(d) Hello and HelloHelloHelloHelloHello

Ans:
Option (a) Hello and WorldWorldWorldWorldWorld
9. Identify the valid program out of the following 2 programs:
#Program -1 #Program -2
helloPython() def helloPython():
def helloPython(): print("I love Programming")
print("I love Programming") helloPython()
helloPython() helloPython()

Ans:
Program-2 is valid
In Program-1, function defintion is present after the function call statement. So it is invalid.
Rule: In Python programming, function definition should be present before all the function call
statements
10. The following question is Assertion-Reasoning based, answer the
questions by choosing one of the following responses:
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and R are true but R is not the correct explanation of A.
(c) A is true but R is false.
(d) A is false but R is true.
Assertion(A) : A function can have either zero or any number of parameters

Reasoning(R) :Parenthesis are not mandatory to call a function with zero parameters

Ans:
(c) A is true but R is false
11. The following question is Assertion-Reasoning based, answer the
questions by choosing one of the following responses:
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and R are true but R is not the correct explanation of A.
(c) A is true but R is false.
(d) A is false but R is true.
Assertion(A) : Number of parameters passed to a function may not match the
number of parameters in the function definition.

Reasoning(R) :Function parameters can have default values

Ans:
(a) Both A and R are true and R is the correct explanation of A.
12. Define Scope of a variable.

Ans:
def f1(N=10):
N*=20
print(N)

f1()
print(N) —--------It is error. N is created inside f1(). So it is a local variable. It can’t be accessed
outside f1()

Scope of a variable:
The part of a program where a variable is accessible is referred as Scope of a variable.
13. Difference between global and local variable.

Ans:
Global variable Local variable
The variables which are created outside a function are The variables which are created inside a function are
referred as Global variables referred as Local variables

Global variables can be accessed throughout the Local variables can be accessed and modified only
program but inorder to modify it inside a function, within the function.
global declaration is mandatory.

global declaration syntax:


global a
global b=50 invalid
14. Explain the types of functions

Ans:
built-in functions module functions user-defined functions
The functions which are already The functions which are already The functions which are developed
developed and available in the developed and stored in a by the users are referred as user-
python library are referred as built- particular python file are referred defined functions
in functions as module functions

Ex: Ex: Ex:


print() sleep() def f1():
input() randint() print(“-”*30)
int() sqrt() etc…….
float() f1- is a user defined function
str()
range() etc….
KEY POINTS TO REMEMBER
➔ Function refers to a set of statements which performs a particular task

➔ Functions can be created without (or) without parameters

➔ Function definition (i.e function) should be placed before the function call statement(s)

➔ Arguments - data (or) variables present in the function call statement


Parameters - variables present in the function header statement

➔ Types of parameters: positional and default

➔ In Python programming, “default parameters should be at the last in parameter list”

➔ Global variables : can be accessed throughout the program but inorder to modify it inside a
function, global declaration is mandatory.

➔ Local variables : can be accessed and modified only within the function.

➔ return statement makes the control to come from out a function and then it returns the specified
value(s) to the calling statement.

➔ Types of functions: built-in functions, module functions, user defined functions

You might also like