Functions_Procedures
Functions_Procedures
Python
Print(len(“Computer Science”))
MyString=“Computer Science”
Print(len(MyString))
Python
Print (“Computer Science”[9,16])
MyString=“Computer Science”
Print(MyString[9:16])
Python
Print(“Computer Science”.upper())
Print(“Computer Science”.lower())
MyString=“Computer Science”
Print(MyString.upper())
Print(MyString.lower())
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
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
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)
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