0% found this document useful (0 votes)
2 views

Functions_Procedures

The document provides an overview of string handling, functions, procedures, and file handling in programming, including pseudocode and Python examples. It covers various string operations such as length, substring extraction, and case conversion, as well as the concepts of local and global variables, library routines, linear search, and bubble sort algorithms. Additionally, it explains how to read from and write to files, showcasing practical implementations in Python.

Uploaded by

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

Functions_Procedures

The document provides an overview of string handling, functions, procedures, and file handling in programming, including pseudocode and Python examples. It covers various string operations such as length, substring extraction, and case conversion, as well as the concepts of local and global variables, library routines, linear search, and bubble sort algorithms. Additionally, it explains how to read from and write to files, showcasing practical implementations in Python.

Uploaded by

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

String Handling

Length () – finding the number of characters in the string


Pseudocode
LENGTH(“Computer Science”)  16
MyString = “Computer Science”
LENGTH(MyString)

Python
Print(len(“Computer Science”))
MyString=“Computer Science”
Print(len(MyString))

Substring () – extract part of the string


Pseudocode
SUBSTRING(“Computer Science”, 10, 7)  Science
MyString=“Computer Science”
SUBSTRING(MyString,10,7)

Python
Print (“Computer Science”[9,16])
MyString=“Computer Science”
Print(MyString[9:16])

Upper /Lower – converting all the characters in a string to


upper case or lower case.
Pseudocode
UCASE(“Computer Science”)
LCASE(“Computer Science”)
MyString=“Computer Science”
UCASE(MyString)
LCASE(MyString)

Python
Print(“Computer Science”.upper())
Print(“Computer Science”.lower())
MyString=“Computer Science”
Print(MyString.upper())
Print(MyString.lower())

Functions & Procedures


When writing an algorithm there are similar tasks to perform
that make use of same code.
Instead of repeating same code again and again
programming languages use subroutines (sub programs). This
is called as procedures and functions
Procedures
A set of programming statements grouped together under a single
name.
Eg: Pseudocode
PROCEDURE Stars
OUTPUT “**********”
ENDPROCEDURE
CALL Stars calling the procedure in main program
Eg: Python
Def stars():
Print(“**********”)

Stars() #calling procedure

Procedures with parameters


To find how many stars would be output. This is done by passing an
argument when procedure is called with a parameter.
Eg: Pseudocode
PROCEDURE Stars (Number : INTEGER)
DECLARE Counter : INTEGER
FOR Counter  1 TO Number
OUTPUT “*”
NEXT Counter
ENDPROCEDURE

CALL Stars(7) Or

MyNumber 7
CALL Stars(MyNumber)

Eg: Python
def Stars(Number):
for counter in range(Number):
print(“*”, end=””)

Stars(7)
Functions
Set of programming statements grouped together under a single
name and it will return a value back to the main program.
Eg: Pseudocode
FUNCTION Celcius (Temp:REAL) RETURN REAL
RETURN (Temp-32)/1.8
ENDFUNCTION

MyTemp  Celcius(MyTemp)
Function needs to be assigned to a variable

Eg: Python
Def Celcius(Temp):
Return (temp-32)/1.8

mytemp=45
mytemp=celcius(mytemp)
print(mytemp)

Parameters
Variables that stores the values of the arguments passed to a
procedure or function. It is not a must to have parameters.
Eg: mytemp = celcius (mytemp)
parameter

Local and global variable


Local – can only be used by the part of the program where it is
declared. Scope is restricted to that part of the program.
Global – can be used by any part of the program. Its scope covers
whole program.
Global variables
DECLARE Number1, Number2, Answer : INTEGER
PROCEDURE Test Local variables
DECLARE Number3, Answer : INTEGER
Number1 ← 10
Number2 ← 20
Number3 ← 30
Answer ← Number1 + Number2
OUTPUT "Number1 is now ", Number1
OUTPUT "Number2 is now ", Number2
OUTPUT "Answer is now ", Answer
ENDPROCEDURE

Library routines
Programming languages include library routines that are ready to
incorporate into a program. These are fully tested and ready for use
and perform many types of tasks.
 MOD – returns remainder of a division
Eg. Value1 MOD (10,3)  returns 1
 DIV – returns the whole number part of a division
Eg: Value2  DIV (10,3)  returns 3
 ROUND – returns a value rounded to a given number of
decimal places
Eg: Value3  ROUND(6.97354,2)  returns 6.97

 RANDOM – returns a random number


Eg: Value4  RANDOM()  returns a number between 0
and 1
Python
Value1 = 10%3
Value2 = 10//3
Value = divmod(10,3)
Value3 = round(6.97354, 2)
from random import random
Value4 = random()

File Handling
Files can be used to store data permanently.
File should have a filename.
Can read from and write to files.

Pseudocode
Writing to a file
DECLARE TextLine : STRING
DECLARE MyFile : STRING
MyFile  “MyText.txt”
OPEN MyFile FOR WRITE
OUTPUT “Enter line of text”
INPUT TextLine
WRITEFILE, TextLine
CLOSEFILE(MyFile)

Reading from a file


DECLARE TextLine : STRING
DECLARE MyFile : STRING
MyFile  “MyText.txt”
OUTPUT “file contains the following line of text”
OPEN MyFile FOR READ
READFILE, TextLine
OUTPUT TextLine
CLOSEFILE(MyFile)
Python code

#writing to a text file


myfile=open("Mytext.txt","w")
textline=input("Enter the line of text")
myfile.write(textline)
myfile.close()

#reading from a text file


print("file contains the following text")
myfile=open("Mytext.txt","r")
textline=myfile.read()
print(textline)
myfile.close()

Linear search
used to check if a value is stored in a list, performed by systematically working
through the items in the list.
Inspects each item in a list in turn to see if the item matches the value
searched for

Pseudocode
OUTPUT "Please enter name to find"
INPUT Name
Found ← FALSE
Counter ← 1
REPEAT
IF Name = StudentName[Counter] THEN
Found ← TRUE
ELSE
Counter ← Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize
IF Found THEN
OUTPUT Name, " found at position ", Counter, " in the list."
ELSE
OUTPUT Name, " not found."
ENDIF

Search checks how many people chose ice cream as their favourite dessert.

ChoiceCount ← 0
FOR Counter ← 1 TO Length
IF "ice cream" = Dessert[Counter] THEN
ChoiceCount ← ChoiceCount + 1
NEXT Counter
OUTPUT ChoiceCount, " chose ice cream as their favourite dessert."

Bubble Sort
 Each element is compared with the next element and swapped
if the elements are in the wrong order, starting from the first
element and finishing with next-to-last element.
 Once it reaches the end of the list, we can be sure that the last
element is now in the correct place.
 Other items in the list may still be out of order.
 Each element in the list is compared again apart from the last
one because we know the final element is in the correct place.
 This continues to repeat until there is only one element left to
check or no swaps are made.

First ← 1
Last ← 10
REPEAT
Swap ← FALSE
FOR Index ← First TO Last - 1
IF Temperature[Index] > temperature[Index + 1] THEN
Temp ← Temperature[Index]
Temperature[Index] ← Temperature[Index + 1]
Temperature[Index + 1] ← Temp
Swap ← TRUE
ENDIF
NEXT Index
Last ← Last - 1
UNTIL (NOT Swap) OR Last = 1

You might also like