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

Working With Functions Notes

The document provides an overview of functions in Python, defining them as blocks of code that perform specific tasks and outlining their advantages such as ease of debugging and reusability. It describes different types of functions including built-in, module, and user-defined functions, along with examples of defining and calling functions. Additionally, it covers concepts related to parameters, arguments, and the distinction between global and local variables.

Uploaded by

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

Working With Functions Notes

The document provides an overview of functions in Python, defining them as blocks of code that perform specific tasks and outlining their advantages such as ease of debugging and reusability. It describes different types of functions including built-in, module, and user-defined functions, along with examples of defining and calling functions. Additionally, it covers concepts related to parameters, arguments, and the distinction between global and local variables.

Uploaded by

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

Working With Functions

XII CBSE NOTES


Define function. Write its advantages

Functions are the block of code written together to perform a


particular task. It gets executed when a call is made to it.

Advantages Of Function:

(i) It is easy to debug a code with the use of a function.

(ii) Function once defined can be called again and again

(iii) Functions get executed when we call them.

(iv) It makes the code looks standard

(v) It is easy to solve problems with the help of a function.

What are the different types of functions:

Functions can be of following types:

(i) Built-in Function: The function that is pre-defined in Python is


called built-in function. Ex: print(),input(),sort(),etc.

(ii) Functions in a Module: The functions that are defined in a


particular module is called Function in module. In order to call
such functions in a program, calling a module is required.

Example:

In order to call the ceil function of the math module the following
statement is required.
import math

x=math.ceil(4.2)

print(x) # 5

(iii) User Defined Function: We can also define our own functions
in Python, such functions are called user defined functions. In
order to define user defined functions, the function header and
function body has to be defined by the user.

USER DEFINED FUNCTIONS

Defining a function:
def functionname(paramterlist): #functionheader

​ Function-body

​ <Return Statement>

(i) def keyword is used to define a function

(ii) functionname is valid identifier

(iii) parameter name is a comma separated variable.

(iv) Function body should define the working of the function

(v) return keyword is used to return the value from the function
body to the calling function. In the absence of a return statement,
None literal is returned.

(vi) The function body is indented from the function header.

Calling a function:
When we call a function, the name of the function along with the
required parameters are passed. If a function is returning a value
then it should be called in a variable or expression (not
mandatory).

Example:

def evenodd(x):

if x%2==0:

print(‘Even’)

else:

print(‘Odd’)

#The above code the definition of the function

Calling:

evenodd(10) #calling with passing literals, it always gives output


Even

x=int(input(‘Enter a number’))

evenodd(x) #calling with a passing variable, it gives output based


on the value of x. If x =21, then output is Odd
Example 2:

def sum(n):

esum=osum=0

for i in range(1,n+1):

if i%2==0:

esum+=i

else:

osum+=i

print(esum,osum)

sum(10)

sum(12)

Example:
def factorial(x):

f=1

for i in range(1,x+1):

f=f*i

return f

x=factorial(5) # Calling 1
print(x)

y=int(input('Enter Factorial'))

print(factorial(y)) # Calling 2

z=factorial(y) #Calling 3

print('Factorial',z,sep='->',end='##')

Q1. What will be the output of Calling 1?

Q2 What is the output at calling 2 if input is 6?

Q3. What will be the value of z and final output?


Answers:

Answer 1: 120

Answer 2: 720

Answer 3: 720

Factorial->720##

Q4.Define a function header named myprog that takes two


parameters x and y.

Q5. Define a function header that takes two parameters x and y


and the function should display the greater of two numbers.If both
are equal it will display the equal message.

Arguments and Parameters


●​ The variable that are passed in a function header is called
Parameter or formal parameter or formal arguments
●​ The variable,values or expression that is passed during
function call is called argument, actual argument or actual
parameters.
●​ The argument supplies values to the parameter and the
parameter works on the received values of the argument.

Example:

def fun(a,b): #Here a and b are parameters

s=a+b

print(s)

fun(10,20) #call1 10,20 are arguments

x,y=20,30

fun(x,y) # call2 x,y are arguments

fun(x+y,x-y) # call3 expression are arguments

Q6. Predict the output at call 1, call 2, call 3.

Types of Parameter

●​ Positional Parameter:
a)​When the parameter in a function header is a variable
then it is called positional parameter.
b)​It is required to pass the values of positional
parameters while calling.
●​ Default Parameter:
a)​When the variable in a function header is assigned with
some value then it is called the default parameter.
b)​It is optional to pass the value of the default parameter.
c)​If a function header has both positional and default
parameters then the order of all positional parameters
come first then followed by default parameters.
●​ Keyword Parameter:
a)​It is associated with function calling
b)​In this, parameter name and value is passed during
calling.
c)​If both positional and keyword parameter are passed,
the positional should written first followed by keyword
parameter,

Example:

def funp(x,y=10): #x is positional and y is default

x=x+y

y=2*x-y

print(x,y)

funp(5) #call1 Only positional call

funp(5,12) #call2 Positional and default call

funp(y=6,x=12) #call3 Keyword call

funp(x=12,5) #call4 Keyword and default call

funp(5,y=12) #call5 positional and default call

Q7. Predict the output in call1, call2, call3, call4, call5


Q8. Which of the following function header are correct/incorrect:

(a)​ define abc(x,yz):


(b)​ def abc(xyz):
(c)​ def abc(x=12,yz):
(d)​ Def abc(x,y,z=10):
(e)​ def abc(x,y,z=100):

Q9. Consider the following header:

def interest(p,r,t=5):

Which of the following function headers are correct/incorrect?

(a)​ interest(10,r=10)
(b)​ interest (p=1000,t=12,r=4)
(c)​ interest(p=10000,r=p//1000,t=r*2)
(d)​ interest(10000,4)
(e)​ interest(10000,5,2):
(f)​ Interest (10000,p=2000,4)

Global and Local Variables

●​ The variables that are defined outside the function body are
called global variables. The global variable is defined using

global variablename

●​ The variables that are defined inside the function body are
called local variables. The parameters are also local to the
function.
Example:

x=10

def globalex(y=20):

z=10

s=y+z

print(s)

k=12

globalex(x+k)

print(x,k)

In the above example,

Local variables are y,z,s

Global Variables are x,k

Q10 Predict the output in the above code segment

In order to access the global variables inside the function ,a global


keyword is used.
x=10

def globalex(y=20):

global x # Statement 1

x=x+y

print(x) #Statement 2

print(x) # Statement 3

globalex(x)

print(x) # Statement 4

Statement 1 defines the variable x is global means the variable x


is same inside and outside the function
(a)​

(a)​

(a)​
(a)​

You might also like