Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

12 Computer Science-Functions-Notes

computer class 12

Uploaded by

myshaathersadiq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

12 Computer Science-Functions-Notes

computer class 12

Uploaded by

myshaathersadiq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CLASS –XII

COMPUTER SCIENCE
FUNCTIONS
INTRODUCTION : A function is a subprogram that acts on data and often
returns a value. It is named sequence of statements that perform a computation. It
contains line of codes that are executed sequentially from top to bottom by python
interpreter. They are the most important building block for any software in Python.
For working in script mode, we need to write the python code in functions and
save it in the file having .py extension. Python functions can belong to one of the
following categories :

 Built-in functions : These are predefined functions and are always available
for use. Ex: len(), int(),input() etc.
 Functions defined in modules : These are predefined in particular modules
and can only be used when the corresponding module is imported. Ex:
sin(),cos() etc.
 User defined functions : These are defined by the programmer.

1.Built in Function:

They are the functions that are built into python and can be accessed by
programmer. These are always available and for using them, we don’t have to
import any file. Python has a small set of built-in-functions as most of the
functions have been partitioned to modules.

2.Module :

A module is a file containing python definition and statements. Standard library of


Python is extended as module to a programmer. Definitions from the module can
be used into code of programs. To use these modules in a program , programmer
needs to import the modules. Once we import a module we can reference to any of
its functions and variables in our code . There are two ways to import a module in
a program. They are :

 import
 from

import : It is the simplest and most common way to use modules in our code:

Syntax: import modulename1[,modulename2,……………….]

Example : Input any number to find square and square root.

import math

X = input (“ Enter any number”)

Y=math.sqrt(X)

A=math.pow(X,2)

print(“square root is = ”, Y)

print(“square value is = ” , A)

output:

Enter any number 25

Square root is = 5.0

Square value = 625.0

from statement : It is used to get a specific function in the code instead of


complete file. If we know beforehand which function , we will needing, then we
may use ‘from’ . For modules having large number of functions, it is recommended
to use from instead of import.

Syntax : >>>from modulename import function[,functionname……….]

from modulename import

will import everything from the file.

Example : Input any number to find square and square root.

from math Import sqrt,pow

X = input (“ Enter any number”)


Y=sqrt(X) #without using math

A=pow(X,2) #without using math

print(“square root is = ”, Y)

print(“square value is = ” , A)

output:

Enter any number 25

Square root is = 5.0

Square value = 625.0

3. User defined functions: In python it is also possible for the programmer to


write their own functions . These functions can then be combined to form module
which can be used in other programs by importing them . To define function ,
keyword ‘def’ is used. After the keyword comes an identifier i.e.name of the
function followed by parenthesized list of parameters and the colon which ends up
the line, followed by the block of statement that are the part of function.A function
in python is defined as per following general format :

def <function name> ([parameters]) :

[“ “ “<function’s docstring> ” ” ” ]

<statement>

[<statements>]

……
For example consider the following python program using a function func1( ).
Let us define these terms formally:

Function Definition : The first line of function definition that begins with
keyword def and ends with a colon, specifies the name of the function and its
parameters.

Function Call : To use a function that has been defined earlier, you need to write a
function call statement in python.Whenever a function call statement is
encountered, an execution frame for the called function is created and the control is
transferred to it, the statements in function body are executed and we get function
output.

Example : To find simple interest using function .

def SI(P,R,T) :

return(P*R*T)

output:

>>>SI(1000,2,10)

20000

Arguments and Parameters :

Arguments are the values provided in function call/invoke statement. List of


arguments should be supplied in the same way as parameters are listed. Bounding
of parameters to arguments is done 1:1, and so there should be same number and
type of arguments as mentioned in parameter list.

Example: arguments in function call

>>>SI(1000,2,10)

1000,2,10 are arguments. An argument can be constant, variable, or expression.

Example : write the output from the following function :

def SI(p,r=10,t=5) :

return(P*R*T)
if we use following call statement :

SI(10000)

SI(20000,5)

SI(50000,7,3)

Output

>>>SI(10000)\

5000

>>>SI(20000,5)

5000

>>>SI(50000,7,3)

10500

Parameters are the value provided in the parenthesis when we write function
header. These are the values required by function to work. If there is more than one
value required by the function to work on then all of them will be listed in
parameter list separated by comma.

Example:

def SI(P,R,T)

We can pass values to the function. For this, you define variables to receive values
in function definition and send values via a function call statement. For example
consider the following program :
Here we are passing 4 as a parameter to the function square . The variable x will be
assigned the value 4 and calculate the result as 16.

Flow of Execution in a function call :

The flow of execution refers to the order in which statements are executed during a
program run.

Whenever a function call statement is encountered, an execution frame for the


called function is created and the control is transferred to it. Within the function’s
execution frame, the statements in the function-body are executed, and with the
return statement or the last statement of function body, the control returns to the
statement wherefrom the function was called.

For example here the function call statement square(4) will send the control-flow
to the function definition which is def square(x) .
Last statement of the function definition i.e. return x*x will send the control back
to wherefrom the function was called and print the result 16.

Example : program to print the sum of two numbers using function

def sum( x, y) :

s= x+y

return(s)

num1 = float(input(“ enter first number ” )

num2 = float(input(“enter second number ”)

sum= sum(num1,num2)

print(“the sum of the numbers is” ,sum)

Passing parameters

Python supports three types of formal arguments/parameters:

1. Positional arguments
2. Default arguments
3. Keyword (or named ) arguments

Positional arguments :

In Positional arguments the function call statement must match the number and
order of arguments as defined in the function definition.

For example , if a function definition header is like:

def check(a,b,c)

Then possible function calls for this can be :

check(x,y,z)

check(2,x,y)

check(2,5,7)
See, in all the above function calls, the number of passed values has matched with
the number of received values. Also, the values are given position wise or order
wise, i.e. , the first parameter receives the value of first argument, second
parameter, the value of second argument and so on.

Thus, through such function calls,

 The arguments must be provided for all parameters.


 The values of arguments are matched with the parameters, position
wise.

Default Arguments :

Python allows us to assign default value to a function’s parameter which is useful


in case a matching argument is not passed in the function call statement. The
default values 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) :

Now if any function call appears as follows :

Si_int = interest(5400, 2)

then the value 5400 is passed to the 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 below :

Si_int = interest(6100,3,0.15)

Then the parameter principal will gets value 6100, time gets 3 and the parameter
rate gets value 0.15

IMPORTANT NOTE : In a function header , any


parameter cannot have a default value unless all
parameters appearing on its right have their default
values .
Keyword Arguments :

They are the named arguments with assigned values being passed in th function
call statement.

For example :

Interest(prin=2000, time =2, rate =0.10)

Interest(time =4,prin =2600, rate = 0.09)

Interest(time =2,rate= 0.12, prin =2000)

All the above function calls are valid now, even if the order of arguments does not
match the order of parameters as defined in the function header.

Returning values from functions :

Functions in Python may or may not return a value. There can be broadly two
types of functions in python:

 Functions returning some value (non-void function)


 Functions not returning any value(void function)

1. Functions returning some value :

The functions that return some computed result in terms of a value, fall in this
category . The computed value is returned using return statement as per syntax :

return <value>

The value returned can be any one of the following:

(i) a literal (ii) a variable (iii) an expression

For example :

def sum(x,y) :

s= x+y
return(s)

And we are invoking this function as :

result = sum(5, 3)

After the function call to sum( ) function is successfully completed, the returned
value will internally substitute the function call statement. That is, now the above
statement will become:

result= 8

2. Functions not returning value :

The function that perform some action or do some work but do not return any
computed value or final value to the caller are called void functions .A void
function may or may not has a return statement. If a void function has a return
statement, then it takes the following form :

return

that is, keyword return without any value or expression.

For example:

void function but has no return statement

(i) def greet(): (ii) def greet1():

print( “hello”) print( “welcome”)

void function with a return statement

(i)def quote() (ii) def printsum(a,b,c)

print(“hello”) print(a+b+c)

return return

Function returning multiple values

Unlike other programming languages , Python can return more than one value from
a function .
To return multiple values from a function , you have to ensure following things:

(i)The return statement inside a function body should be of the form given below:

return <value1/variable1/expression1>,<value2/varaiable2/expression2>,…….

(ii)The function call statement should receive or use the returned values in one of
the following ways :

(a) Either receive the returned values in form a tuple variable , i.e. as shown
below :
def squared(x,y,z):
return x*x,y*y,z*z

Here return statement returning comma separated multiple values.


Now to call this function the statement will be like this :

t = squared(2,3,4)

print(t)

tuple t will be printed as : (4,9,16)

(b)Or directly unpack the received values of tuple by specifying the same

number of variables on the left hand side of the assignment in function

call . For example :

def squared(x,y,z) :

return x*x, y*y, z*z

u,v,w = squared(2,3,4)

print(u,v,w)

The output will be as follows : 4 9 16

Scope of variables :

Parts of program within which a variable is legal and accessible, is called


scope of variable.
There are broadly two types of scopes in python :

1.Global scope : A variable declared in top level segment of a program is said to


have a global scope and is usable inside the whole program and all blocks
contained within the program.

2.Local scope : A variable declared in a function-body is said to have local scope


i.e., it can be used only within this function and the other blocks contained under it.

For example :Consider the following Python program :

def calcsum(x,y):

z= x+y

return z

num1= int(input( “enter first number”))

num2=int(input( “enter second number”))

sum=calcsum(num1,num2)

print( “the sum of given two number is”,sum)

Here num1,num2 and sum is declared in the main program and three variables x,y
and z are declared in function calcsum. So as per definition given above
num1,num2 and sum are global variable and x,y and z are local variable

You can also take the help of following videos :

For Python Inbuilt functions: http://youtube/r9hZkEBWwEw

To understand Python modules : http://youtube/j_xrFVEYwAg

How to create user defined functions: http://youtube/yaWX2Fc91v


MEMORY MAP

KEY POINTS OF FUNCTION:


* A Function is a subprogram.

* Function makes program handling easier by breaking it into subparts.

* Python Functions can belong to one of the following three categories:

--Built-in function which are pre-defined and always available.

--Functions defined in module which are predefined in a particular

module and can only be used when the corresponding module is


imported.

--User defined function which are defined by the programmer to perform a

specific task.

* The values being passed through a function call statement is called arguments.

* The values received in the function definition are called parameters.

* A function is executed in an execution frame.

* A function may or may not return value.

* A parameter having default value in the function header is known as a default

parameter.

* A default parameter can be skipped in the function call statement.

* A void function internally returns legal empty value none.

You might also like