12 Computer Science-Functions-Notes
12 Computer Science-Functions-Notes
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 :
import
from
import : It is the simplest and most common way to use modules in our code:
import math
Y=math.sqrt(X)
A=math.pow(X,2)
print(“square root is = ”, Y)
print(“square value is = ” , A)
output:
print(“square root is = ”, Y)
print(“square value is = ” , A)
output:
[“ “ “<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.
def SI(P,R,T) :
return(P*R*T)
output:
>>>SI(1000,2,10)
20000
>>>SI(1000,2,10)
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.
The flow of execution refers to the order in which statements are executed during a
program run.
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.
def sum( x, y) :
s= x+y
return(s)
sum= sum(num1,num2)
Passing 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.
def check(a,b,c)
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.
Default Arguments :
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
They are the named arguments with assigned values being passed in th function
call statement.
For example :
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.
Functions in Python may or may not return a value. There can be broadly two
types of functions in python:
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>
For example :
def sum(x,y) :
s= x+y
return(s)
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
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
For example:
print(“hello”) print(a+b+c)
return return
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
t = squared(2,3,4)
print(t)
(b)Or directly unpack the received values of tuple by specifying the same
def squared(x,y,z) :
u,v,w = squared(2,3,4)
print(u,v,w)
Scope of variables :
def calcsum(x,y):
z= x+y
return z
sum=calcsum(num1,num2)
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
specific task.
* The values being passed through a function call statement is called arguments.
parameter.