Nothing Special   »   [go: up one dir, main page]

Class 12 CS F 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

SHANTINIKETAN WORLD SCHOOL (10+2, ALIGARH

CLASS XII/COMPUTER SCIENCE/F1

UNIT NAME: Computational Thinking and Programming - 2


Book Name: Computer Science with Python (Sumita Arora)
Topics: Working with Functions
SUBTOPICS: Function, Flow of Execution in a Function call, Passing Parameters,
Returning values from functions, composition.
• Function:
A function is a named sequence of statement(s) that performs a computation. It contains line
of code(s) that are executed sequentially from top to bottom by Python interpreter. Functions
can be categorized as belonging to
i. Modules
ii. Built in
iii. User Defined

Module
A module is a file containing Python definitions (i.e., functions) and statements. Standard
library of Python is extended as module(s) to a programmer.
There are many ways to import a module in your program, the one’s which you should know
are:
i. import
ii. from Import
It is simplest and most common way to use modules in our code.
Its syntax is:
import module name
Example value= math.sqrt (25) # dot notation

From Statement
It is used to get a specific function in the code instead of the complete module file.
Syntax: from module name import function name
Example: from math import sqrt value = sqrt (25)

Built-in function
These are pre-defined functions and are always available for use. Example: len( ), type( ),
input( ) etc.

User defined functions


These are defined by the programmer.
Defining function in Python
A function in python is defined as per following general format:
Example
def sum (x,y):
s=x+y
return s
#print(s)

Page 1 of 6
here,
def keyword
sum function name
(x,y) parameter inside () are x,y
def sum (x,y): function header ends with colon:
return function may or may not have a return statement

Function Header
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.

Parameters
Variables that are listed within the parentheses of a function header.

Function Body
The body of the function gets executed only when the function is called/invoked.
The function body may or may not return any value. A function returns a value through a
return statement.

Indentation
The blank space in the beginning of a statement within a block. All statements within same
block have same indentation.

Eample1:
def sumof3multiplies1(n):
s=n*1+n*2+n*3
return s

Example 2:
def sumof3multiplies1(n):
s=n*1+n*2+n*3
print(s)

in Example 1 and 2, both these functions are doing the same thing but first one is returning the
computed value using return statement and second function is printing the computed value
using print () statement.

• Flow of Execution in a Function call


The flow of Execution refers to the order in which statements are executed during a program
run. Program execution begins with first statement of -main- segment.
Example:
Program to add two numbers through a function
def calcSum(x,y):
s=x+y
return s
num1=int(input("enter first no "))
num2=int(input("enter Second no "))

Page 2 of 6
sum=calcSum(num1, num2) //calling function
print("sum of two given numbers is", sum)

Arguments and Parameters


Parameters are the value(s) provided in the parenthesis when we write function header. These
are the values required by function to work.
Arguments are the value(s) provided in function call/invoke statement. List of arguments
should be supplied in same way as parameters are listed.
Example
def multiply (a,b): //a, b parameters
print (a*b)
y=3
multiply (12, y) # function call 1 //12, y arguments
multiply (y, y) # function call 2 // y,y arguments

x=5
multiply (y, x) # function call 3

with function call 1, the variable a and b in function header will receives value 12 and y
respectively.

with function call 2, the variable a and b in function header will receives value y and y
respectively.
with function call 3, the variable a and b in function header will receives value y and x
respectively.

• Passing Parameters
Python supports three types of formal argument/parameters:
• Positional argument
• Default argument
• Keyword argument

Positional argument
When the function call statement must match the number and order of arguments as defined
in the function definition, this is called the positional argument matching.
For Example
def(a,b,c):
……
Then the possible function calls for this can be:
check (x,y,z) # 3 values (all variables) passed
check(2,x,y) # 3 values (literls+variables) passed
check (2,5,7) # 3 values (all literls) passed

Default Arguments
• A parameter having default value in the function header is known as default parameter.
• A parameter having a default value in function header becomes optional in function call.
Function call may or may not have value for it.
Page 3 of 6
• Non-default argument cannot follow default argument.
Example: function header with default values:
def interest (principle, time, rate=0.10)
function call
si_int=interest (5400, 2) #third argument missing
then the value 5400 is passed to the parameter principle, the value 2 is passed to the second
parameter time and since third argument rate is missing, its default value 0.10 is used for rate.
si_int=interest (6100, 3.0, 0.15) #no argument missing
then the parameter principle gets value 6100, time gets 3 and the parameter rate gets value
0.15.
this means the default values are considered only if no value is provided for that parameter in
the function call statement.

Keyword Argument
Keyword argument are the named arguments with assigned values being passed in the function
call statement.
Example:
interest (principle=2000, time=2, rate=0.10)
principle geta value 2000, time gets value 2 and rate as 0.10.

interest (time=4, principle=2600 rate=0.09)


principle geta value 2600, time gets value 4 and rate as 0.09.

interest (time=2, rate=0.12, principle=2000)


principle geta value 2000, time gets value 2 and rate as 0.12.
all the function calls are valid.

Using Multiple Argument Types Together


Rules for combining all three types of argument:
• An argument list must first contain positional arguments followed by any keyword
argument.
• Keyword arguments should be taken from the required arguments preferably.
• You cannot specify a value for an argument more than once.
• Having a positional argument will result into error.
For example, consider the following function header:
def interest (prin, cc, time=2, rate=0.09):
Return prin *time*rate

It is clear above function definition that values for parameters prin and cc can be provided
either as positional arguments or as keyword but these values cannot be skipped from the
function call.

• Returning values from functions


Functions in Python may or may not return value. They can be broadly two types of functions
in python:
• Functions returning some value (non-void function)
• Functions not returning any value (void function)
Page 4 of 6
Functions returning some value (non-void function)
The function that returns some computed result in terms of a value. There can be computed
value is returned using return statement.
Syntax:
return<value>
Example
return 5
return 6+4
return a
return a**3
return (a+8**2)/b)
return a+b/c

Functions not returning any value (void function)


The functions 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.
Syntax: return

1. void function but no return statement


def greet ():
print(“helloz”)

2. void function with a return statement


defquote();
print (“Goodnesscounts!!”)
return
the void functions do not return a value, but they return a legal empty value of Python i.e.,
None.

Example:
def greet( );
print(“helloz”)
a=greet( )
print(a)
return
output:
helloz
None

Returning Multiple values


To return multiple values from a function, we must ensure following things:
1. The return statement inside a function inside a function body should be of the form
given below:
return<value1/variable1/expression1>, <value2/variable2/expression2>, …………
2. The function call statement should receive or use the returned values in one of the
following ways:

Page 5 of 6
a. Either receive the returned values n from a tuple variable, i.e.,
def square (x,y,z)
return x*x, y*y, z*z #the return statement retuning comma separated
values (expression)
t=squared (2,3,4) # variable that receive the returned values is a tuple
print(t) # Tuple t will be prints as: (4,9,16)

b. Or you can 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
e.g.,
def square(x,y,z):
return x*x, y*y, z*z
v1,v2,v3=squared(2,3,4) #now the receiverd values are in the form of three
different variables, not as a tuple
print(“the returned values are as under: ”)
print(v1,v2,v3)
output:
the returned values are as under:
4 9 16

• Composition
Composition in general refers to using an expression as part of a larger expression, or a
statement as a part of larger statement.

Example 1:
Greater ((4+5), (3+4))
Example 2:
A logical expression
test (a or b)

HOME ASSIGNMENT:
1. Write a program that receive two numbers in a function and returns the results of all
arithmetic operations (+, -, *, /, %) on these numbers (page no. 114).
2. Write a program to add two numbers through function (page no.101).
Soln: def calcSum(x,y):
s=x+y
return s
num1=int(input("enter first no "))
num2=int(input("enter first no "))
sum=calcSum(num1, num2)
print("sm of two given numbers is", sum)

3. Program to calculate simple interest using a function interest () that can receive principal
amount, time and rate returns calculated simple interest. Do specify default values for
rate and time as 10% and 2 years respectively (page no. 110).

Page 6 of 6

You might also like