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

working with functions computer 12

Presentation of computer science.

Uploaded by

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

working with functions computer 12

Presentation of computer science.

Uploaded by

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

Chapter --> 3

Working with functions.....


Theory and questions....
 Function is a code that has a name and it can be
reused(execute again) by specifying its name in the
program, whenever needed.
What  Functions help break our program into smaller parts.

is ● Program:
def display():
print(“hello welcome to python”)
function print(“you are learning about working with
functions”)
? display()
🤔 ● Output :
hello welcome to python
you are learning about working with functions
Increases readability, particularly for longer
code as by using functions, the program is
better organized and easy to understand.
Advanta Reduces code length as same code is not
ges required to be written at multiple places in a
program. This also makes debugging easier.
Of Increases reusability, as function can be
called from another function or another
program. Thus, we can reuse or build upon
Function already defined functions and avoid
repetitions of writing the same piece of code.
s  Work can be easily divided among team
members and completed in parallel.
Built in function: These are predefined
function in python and are always available
for use.

Types For example: int(), print(), input(), float(), etc.


Function defined in modules: These are also
of predefined function which are part of
particular module.
For example: pow(), sqrt.
functio User defined function: These are the function
ns defined by the programmer as we can create
our own function.
 For Example: sum()
 A function definition begins with def (short for define). The
syntax for creating a user defined function is as follows:
 Syntax:
 def<function name> ([parameter 1, parameter 2, .....]):
Function Header Function body Should be
indented within the
CREATING A  set of instruction to be executed
function header
 return value
USER
Example:
DEFINED def sum():
FUNCTION a= int(input(“enter first no. : ”))
b= int(input(“enter second no. :”))
c=a+b
print (“sum of” ,a, “and”, b, “ is”, c”)
sum()
 The values being passed through a function-
call statement are called arguments (or
actual parameters or actual arguments).
Argumen  The values received in a function definition /
ts header called parameters (or formal
parameters Or common arguments).
and  Example: PARAMETERS (or Formal argument)

def multiply(a,b):
Paramete return a*b
rs
Argument (or Formal parameters)
multiply (4,5)
POSITIONAL /REQUIRED ARGUMENT :
When the function call statement must match the
Positional number and order of arguments as defined in the
function definition, this is called positional argument.
/ Program :
def add(num1, num2,num3):

Required sum = num1 + num2+num3


print(“The sum of “,num1, num2,”and”, num3,”is
“,sum)
Argument add(10,20)
s Output :
Type Error: add() missing 1 required positional
argument: ‘num3’
DEFAULT ARGUMENT/PARAMETER
 Python allows assigning a default value to the
Default parameter. A default value is a value that is pre
decided and assigned to the parameter when the
function call does not have its corresponding
argument.
Program :
Argument def add(num1, num2,num3=30):
s/ sum = num1 + num2+num3
print(“The sum of “,num1,num2,”and”,num3,”is
Parameter “,sum)
add(10,20)
s Output :
The sum of 10 20 and 30 is 60
KEYWORD (NAMED) ARGUMENT/ PARAMETER
 Keyword arguments are the named argument with
assigned values being passed in the function call.
Keyword Program :
def total( maths, chemistry, physics=85):

(Named) sum = maths + chemistry + physics


print(“The sum of maths, chemistry and physics is
:”, sum)
Argumen total(maths=90,physics=90,chemistry=95)

ts Output :
The sum of maths, chemistry and physics is: 275
 Part(s) of program within which a name is legal and
accessible, is called scope of the name.
 Types of scope:
1. Global scope
Scope 2. Local scope
Of ● Global variable: Global variable is a variable
defined main program. Such variables are said to have
variables global scope.
● Local variable: Global variable is the available
defined within a function. Such variable are said to
have local scope
◆ Program:
X=5 Global
variable
def fun(X.):

Global X=3 Local


variable
Y= X**2
and
return y
variable fun()
scope print (X, y)
◆ Output :
5, 9
 The code given below accepts a number as an
argument and returns the reverse number observed
the following court carefully and write it after
removing all sentiments and logical errors underline
all the collections make:
define revNumber (num):
rev = 0 Ans.
Question rem = 0
def revNumber(num):
rev = 0
1. While num > 0 :
rem = 0
while num > 0:
rem == num%10 rem num%10
rev = rev*10
rev = rev*10 + rem num =
num = num // 10 num//10
returned
return rev print (revNumber
print (revNumber (1234)) (1234))
 Write a function called now (PLACES) in python, that
takes the dictionary PLACES as an argument and
displaced the names (in upper case) of the places
whose names are longer than 5 characters. For
example, consider the following dictionary:
PLACES={1:’Delhi’, 2 : ‘London’, 3 : ‘Paris’, 4 : ‘New
York’, 5 : ’Doha’}
Question the output should be :
2. London,
Ans. PLACESNew= York
{1 : ‘Delhi’, 2 : ‘London’, 3 : ‘Paris’, 4 : ‘New
York’, 5 : ‘Dubai’}
def countNow (PLACES) :
for place in PLACES. Values ():
if len (place) > 5:
print (place. upper())
countNow (PLACES)
 Predict the output of the following code :
S = LOST
L = [10,21,33,4]
D={}
for i in range (len (s)) : Ans. 4*L
Question if I%2 ==0 :
3. D [L. pop()] = S [I]
33*4
21*
else : S
D [L. pop()] = I + 3
for K, V In D. items(): 10*6
print(K, V, sep = “*”)
 Write the Python statement for the following tasks
using BUILT-IN functions only :
(a) to insert an element 200 at the 3rd position, in list
L1.
(b) to check whether a string named, message ends
with a full stop/ period or not.

Question Ans. (a) L1.insert (2,200)

4. (b) message. endswith(‘.’)


 Predict the output of the following code :
def Changer(P, Q = 10):
P = P/Q
Q = P%Q Ans. Output: ND_*34
Explanation:
Question return P
Text1 = “IND-23”
A = 200
5. B = 20P
Text2 = “ ”
I=0
while I<len(Text1):
A = Changer (A, B) if Text1 [I]>=“0” and Text1 [I]
<=“9”:
print(A, B, sep = ‘ş’)
B = Changer (B)
print(A, B, sep = ‘ş’, end = ‘###’)

You might also like