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

chapter_8_-_part1

Uploaded by

mayare800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

chapter_8_-_part1

Uploaded by

mayare800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 8 : Programming concepts –Data

Structures
Variables
• Variable in a computer program , Constants
is a named data store than contains a value,that
and Arrays
may change during
the execution of a program. Putting data into variable is done using an Assignment Statement
Declaring Variable :
Syntax : DECLARE variable name : Data type
Example : DECLARE X : STRING
DECLARE Y : INTEGER
DECLARE Z : INTEGER

• Constant in a computer program is a named data store than contains a value that does not change
during the execution of a program.
Declaring the constants :
Syntax : CONSTANT constant name = value
Example : CONSTANT Pi= 3.14

• An array is a data structure containing several elements(List ) of the same data type; these elements
can be accessed using the same identifier name. The position of each element in an array is identified
using the array’s index.
Chapter 8 : nested statements

Nested statements : is one or more selection and /or iteration statements inside another
selection/iteration statement .
Could be :
• IF statement inside IF statement
• Or Loop inside IF statement
• Or IF inside Loop statement
• Or Loop inside Loop statement.
Chapter 8 : Subroutine
A Subroutine is a piece of code which is written within given program which has an
identifier (name). The Subroutine can be called from the same main program but can not
be called from other programs. In other words subroutines are local to program. Two forms
Procedures and functions .

Why ? When writing an algorithm, there are often similar repeated statements Instead of
repeating these statements and writing new code every time they are required, many
programming languages make use of subroutines.
It is helpful when dividing the system into subsystems
Chapter 8 :
Procedures, functions and parameters
Subroutine
A procedure is a set of programming statements grouped together under a single
name (identifier) that can be called to perform a task at any point in a program.

A function is a set of programming statements grouped together under a single


name (identifier ) that can be called to perform a task at any point in a program. In contrast
to a procedure, a function will return a value back to the main program.

A parameter : is a value that is sent from the main program to the subroutine ( procedure or
function ) . Parameters are declared inside the brackets after the subroutine name .

When procedures and functions are defined, the first statement in the definition is a header,
which contains:
» the name of the procedure or function (identifier )
» any parameters passed to the procedure or function, and their data type
» the data type of the return value for a function.
Chapter 8 : Subroutine – procedure
Functions
Procedures :
• it is defined once and can be called
• it is defined once and can be called
many times within a program.
many times within a program.
• it can be defined with or without
• it can be defined with or without
parameters
parameters.
• A function always returns a value.
• a procedure doesn’t return a value.
• Function calls are made as part
• Procedure calls are single standalone
of an expression, on the right-hand
statements.
side.
Chapter 8 : Subroutine – procedure
Procedures : procedure it is defined once and can be called many times within a program. it can be defined with or
without parameters. Procedure calls are single standalone statements.
Example : 1
************
Procedure without parameter : 2
PROCEDURE Stars ************
3
OUTPUT"************" ************
ENDPROCEDURE 4
************
5
FOR C1 TO 6 ************
OUTPUT C 6
CALL Stars ************
NEXT C

Procedure with parameter : *


PROCEDURE Stars (Number : INTEGER) *
FOR Counter ← 1 TO NUMBER *
OUTPUT "*" *
NEXT Counter *
ENDPROCEDURE *
CALL Stars(7) *
Chapter 8 : Subroutine functions

For example, here is a function written in pseudocode to convert a temperature from Fahrenheit
to Celsius:

FUNCTION Celsius (Temperature : REAL) RETURNS REAL


RETURN (Temperature – 32) / 1.8
ENDFUNCTION
MyTemp ← Celsius(20) call the function using the variable MyTemp

FUNCTION ConvertToCm(Inches: REAL) RETURNS REAL


RETURN Inches * 2.4 call the function using the variable Inches
ENDFUNCTION
Chapter 8 : Subroutine – scope
Scope of a variable : is the area within a program that it can be accessed
Local and global variables
A global variable can be used by any part of a program – its scope covers the whole program.
A local variable can only be used by the part of the program it has been declared in – its scope is
restricted to that part of the program. when that part finishes the variable will not be declared and
cant be used anymore
For example, in this algorithm the variables Number1, Number2 and Answer are declared both
locally and globally, whereas Number3 is only declared locally.
Chapter 8 : Library routines
Library routines:
• routines that are ready to be used into a program.
• These routines are fully tested and ready for use.
• includes functions and procedures.

Examples :
» MOD – returns remainder of a division takes 2 parameters Ex. MOD(9,4)----1
» DIV – returns the quotient (i.e. the whole number part) of a division takes 2 parameters Ex. DIV(9,4)----2
» ROUND – returns a value rounded to a given number of decimal places takes one parameter
Ex. ROUND(6.97354, 2) returns the value rounded to 2 decimal places
» RANDOM – returns a random number.
Ex. two parameters : RANDOM(Integer1 : INTEGER, Integer2 : INTEGER) RETURNS INTEGER generates a random integer in
the range from Integer1 to Integer2 inclusive. For example: RANDOM(10, 12) returns either: 10, 11 or 12
» INT – returns the integer part of a number Ex. INT(9.6)-------9
Chapter 8 : Library routines
Library routines : String manipulating
» upper – converting all the letters in a string to uppercase. For example, the string "Computer Science" would become
"COMPUTER SCIENCE".
» lower – converting all the letters in a string to lowercase. For example, the string "Computer Science" would become
"computer science".
» length – finding the number of characters in the string. For example, the length of the string "Computer Science" is 16
characters as spaces are counted as a character.
Ex. Charlen  Length(string)

» substring – extracting part of a string. For example, the substring "Science" could be extracted from "Computer Science".
Substring(string ,start , length) Ex. Value  Substring(“Computer Science”,10,7)------- value “Science”
Chapter 8 : Library routines
Library routines : String manipulating
Convert between data types :
• Str(number) returns the value as string ------------
name str(3) returns “3” as string
• Int(string consists of numbers) returns the integer value
number  int(“3”) returns 3 as number
• Float(string consists of numbers) returns the real value
Number  float(“3.14”) returns 3.14 as number

Example : output each letter of a string each character at a time


INPUT “ Enter a message” , StringInput
FOR Count 1 to LENGTH(StringInput)
Character  SUBSTRING(StringInput, Count,1)
OUTPUT Character
NEXT Count

You might also like