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

Algorithms and Programs 2

Uploaded by

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

Algorithms and Programs 2

Uploaded by

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

AS Computer Science

Algorithms and programs (2)

Key terms Advantages of subroutines (Modular Code readability


programming)
Term Definition Program code must be written to be clear and
The advantages of breaking a program into subroutines understandable, so that it can be developed
Subroutine A sequence of program include: and maintained by others. Techniques to
instructions that performs a • the decomposition of a complex programming task improve code readability include the use
specific task, packaged as into simpler steps of internal comments, self-documenting
a unit. This unit can then be identifiers for variables and constants,
• the reduction of duplicate code within a program
used in programs wherever indentation, and white space.
that particular task needs to • reusing of code across multiple programs
• allowing distribution of a large programming task Internal comments: comments added
be performed.
among various programmers to program code, that are not translated
Subroutines may be when the program runs, can be used to add
• improving program readability by replacing blocks of
defined within programs, descriptions, or explanations. This is useful
code with subroutine calls
or separately in libraries when working as part of a team, as other
which can be used by many • improving traceability of errors, making the program programmers can get an idea of what the
programs. In different easier to debug. code is doing and why certain decisions have
programming languages, a A return instruction causes execution of the been made. Examples of symbols used to
subroutine may be called a subroutine to cease and execution to resume in the indicate comments include,
procedure, function, method, calling program. A return may be followed by a value, Python $, Visual Studio ‘, JavaScript //
or subprogram. which becomes the result of the subroutine call.
Self-documenting identifiers: using
Parameter Data passed into a variable names that describe what the
subroutine (procedure or Mathematical and logical operations variable contains is important. Poor variable
function), as specified in the names can make it difficult to work out what
subroutine definition. is going on in a program. Sub-program/
Mathematical function names should describe what the
Comparison operators
operators function does. ‘Function2’ is not a suitable
A subroutine or procedure call is made Addition True if the values are name for a function used to calculate an
by stating the subroutine name and listing + == average score. ‘calcAverage’ would be more
equal.
actual parameter names or values, typically appropriate.
Subtraction True if the values are
separated with commas within parentheses. - !=
not equal. Indentation: moving parts XXXXXXXXX
XXXXXXXXX
The number and types of the parameters in * Multiplication > Is greater than of code that form the content XXXXXXXXX

the call must match the number and types of of constructs such as loops XXXXXXXXX
XXXXXXXXX
/ Division < Is less than or if statements to the right
the parameters that were created when the
subroutine was originally defined. Modulus (returns Is greater than or makes it easier to see the overall structure.
% >=
remainder) equal to In some languages, (e.g. Python) indentation
Passing parameters by reference involves is mandatory, missing it out will result in
passing a pointer to the subroutine which Exponent Is less than or equal
** <= syntax errors. It is also much easier to debug
points to the original parameter (the original to
properly indented code.
data item). Any changes to the data made
within the subroutine will also be made to the TRUTH TABLE White space should be used to separate
original parameter. X Y AND OR NOT X XOR different subprograms and functions so that it
is easier to see each module (section) of code.
Passing parameters by value involves 0 0 0 0 1 0
taking a copy of the parameter and passing 0 1 0 1 1 1
that copy to the subroutine. Any changes
to the copy of the parameter inside the 1 0 0 1 0 1
subroutine do not affect the data held in the 1 1 1 1 0 0
original parameter.

You might also like