0% found this document useful (0 votes)
27 views6 pages

Chapter-3 Working With Functions

Uploaded by

hitharesmi07
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)
27 views6 pages

Chapter-3 Working With Functions

Uploaded by

hitharesmi07
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/ 6

ANGELS ARC SENIOR SECONDARY SCHOOL, KAYAMKULAM

COMPUTER SCIENCE
CHAPTER 3: WORKING WITH FUNCTIONS
Functions:
It is a group of statements that exist within a program for the purpose of performing a
specific task.
Function consists of:
• Arguments, i.e., values if needed
• Set of statements to do task
• Result which is returned.
e.g. def calculate(x):
r=2*x**2
return r
where def is the keyword which indicates a function definition.
calculate is the function name
x is the argument
return statement returns the result
Invoking or Calling a function:
Syntax:
functionname(argument)
a=int(input(“Enter a number:”))
print(calculate(a)) # function call

We can pass value or variable inside a function call.


We can write:
(i) calculate(5)
(ii) n=7
calculate(n)
(iii) n=int(input(“Enter a number:”))
calculate(n)
(iv) print(calculate(5))
(v) c=calculate(5)

Function Types:
Functions are classified as 3 types:
i. Built-in functions:
They are pre-defined functions that are already available in Python.
e.g. len(), type(), input(), int(), eval(), abs() etc…
ii. Modules:
They are small segments that splits lengthy program to reduce complexity. A module is
file containing functions and variables, defined in separate files and this module is imported.
They are imported from the libraries.
e.g. sin(), sqrt() are imported from math
import math
math.sqrt(4)

1
iii. User-defined functions:
These are functions which are defined by the programmer.
e.g. calculate()

Defining Functions:
Syntax:
def <function name> (<parameters>):
<statements>
e.g. def area(x,y):
a=x*y
return a

Arguments and Parameters:


✓ Parameters are the values provided in the parenthesis.
✓ An argument is a value that is passed to the function when it is called.
✓ The arguments inside function call is called actual parameters or actual arguments.
✓ The arguments inside function definition is called formal parameters or formal arguments.

The arguments can be literals, variables or expressions.

Passing Parameters:
There are 3 types of arguments:
i. Positional arguments (Required argument)
ii. Default arguments
iii. Keyword or named arguments.
➢ Positional arguments:
Are arguments passed to a function in correct positional order.
The number and position of arguments must be matched. If we change, the result will be changed.
e.g. def sum(a,b,c):
__________
__________
sum(x,y,z) or sum(2,3,4)
➢ Default arguments:
Is an argument that assumes a default value if a value is not provided in the function call.
e.g. def S1(p,t,r=5.2):
_____________

S=S1(1000,2) #function call


def S1(p, t=2, r): #invalid. Default value of given, the rightmost variable
should have a value
def S1(p,t=2,r=1.5): #valid
➢ Keyword or named arguments:
They are the named arguments with assigned values being passed in the function call statement.
If there is a function with many parameters and we want to specify only some of them in function
call, then value for such parameters can be provided by using their name instead of the position order
is not important by no. of arguments must be, matched.
e.g. sum (a=5, b=6, c=10)
sum (b=2, c=4,a=15)

2
Using Multiple Argument types together
e. g. interest (5000, time=5)
# 5000 represent positional argument as it will be assigned to first parameter on the basis
of its position
# time=5 represent keyword argument or named argument
Python states that in a function call statement following:
• An argument list must first contain positional argument then keyword argument
• Keyword arguments should be taken from the required arguments
• Cannot specify a value for an argument more than once.
e.g., def Interest (prin, cc, time=2, rate=0.09):
here prin and cc are positional argument and time and rate are keyword argument.
Having positional argument after keyword arguments will result into error.
e.g., Interest (5000, 3, rate=0.05) # valid
Interest (rate=0.05, 5000, 3) # invalid (keyword argument came before positional argument).
NOTE: Go through the table given in Page no. 109

Returning Values from Functions


On the basis of returning values from functions, python can be of two types:
1. Functions returning some values (non void functions)
2. Functions not returning any value (void function)

1. Functions returning some values (Fruitful functions)


Some functions can return the result that is computed. For this return statement is used.
Syntax: return<value>
It can return either a literal or a variable or an expression
e.g. return 10 # returning literal
return (7*4) #returning expression
return S # returning a variable
e.g. def Sum(x,y):
S=x+y
return S
result= Sum (5,3)
So, 8 will be stored in variable result

We can write the statement in different ways:


(i) add=sum(x,y)
(ii) print(sum(2,3))
(iii) sum(4,6)>17

3
NOTE: The return statement terminates a function execution.
i.e. def sum(x,y)
s=x+y
return s
print(s) #won’t work
sum(x,y)

2. Functions not returning some value (void function)


Some functions execute and performs some tasks but do not return any value.
Syntax: return #no value or expression
e.g. def welcome ()
print(“Hi”)
return
This void function do not return a value but they return a legal empty value of python, i.e.
None
e.g. def greet():
print(“Hello”)
a=greet()
print(a)
O/P:
Hello
None
We can have different function having:
i. Non-void function without any argument.
ii. Non-void function with some arguments.
iii. Void functions without any arguments.
iv. Void function with some arguments.

Returning multiple values:


Python can return multiple values which is not possible by some other languages like C, etc.
e.g. def squa(x,y,z);
return x*x,y*y,z*z
s=squa(9,8,4)
print(s)
O/P:
(81,64,16)
or x1,y1,z1= squa(9,8,4)
print(x1,y1,z1)
Composition:
It means larger expression or statement can be used by an expression.
e.g. greater ((4+5), (3+4))

4
Scope of Variables:
All variables in a program cannot be accessible at all location in that program. This depends
on where you have declared a variable.
There are two kinds of scope in python. They are:
1. Global Scope
• Names assigned at the top level of a module.
• Usable inside the whole program in all blocks.
2. Local Scope:
• Names assigned inside a function definition or loop.
• Can be used only within the function where it declared.
e.g. def sub(x,y)
s=x-y #x,y,z are local
return s
n1=int(input(“Enter a no:”)) #n1, n2 are local
n2=int(input(“Enter a no:”))
s1=sub(n1,n2)
print(“Subtraction=”,s1)

x=2 #global variable


def fun(a):
b=a+1
return b
y=input(“Enter no:”)
z=y+fun(x)
print(z)

NOTE: Go through the example given in textbook page no. 116 to 119

Name Resolution:
When we access a variable from within a program or function, python, follows name
resolution rule known as LEGB rule (Local
Global Built-in environment)
For every name reference, python does:
i. It check with local environment if it has a variable with the same name.
ii. Checks the enclosing environment
iii. Checks the global environment
iv. Check Built in environment
NOTE: Go through the example given in textbook page no. 121

USING RANDOM MODULE


This module provides random number generators. To use this, we need to import module random.
random()
Returns a random number in range(0.0-1.0), Lower range limit is inclusive
Returns floating point number
E.g. import random Output
print(random.random()) 0.8189286

5
randint(a,b)
Returns a random number in given range, Both range limits are inclusive
Returns integer number
e.g. import random
print(random.randint(4,20)) Output 8
import random
S1=random.randint(1,100) Output
S2= random.randint(1,100) 95 75 3
S3=random.randint(1,100)
print(S1, S2, S3)

randrange()
This function generate random numbers from a specified range .
Eg random.randrange(90,100) will generate values from 90 to 99

You might also like