Save
...
incomplete
computer science
structured programming
Save
Share
Learn
Content
Leaderboard
Learn
Created by
lucy dock
Visit profile
Cards (9)
subroutine
a named
‘out of line’
block of
code
that may be
executed
(called) by simply writing its
name
in a
program statement
the two examples of subroutines
functions
procedures
in general, a function interface includes:
identifier – the
name
of the function (this should be descriptive)
parameters
– these are the function
input
(these must be passed in to the
subroutine
when it is called)
process – the
‘doing’
stuff
output
– the
return
value
advantages of structured programming
easier to understand and modify
modules (sections of code) can also be reused which reduces development time and it is
easier to independently test the individual modules
local variables
variables created within a
subroutine
are called
these are only available for use in the
function
or
procedure
and once the
subroutine
ends, they are
destroyed
global variables
variables created
outside
of a
procedure
or
function
it is best to avoid using these as they take up
memory
and
increase
the chance of naming
clashes
which could lead to
bugs
if we want to get a value INTO a subroutine, we need to pass it in as a
parameter
this is a
value
inside a
bracket
after the subroutine identifier
when we call the
subroutine
, we need to
specify
what these
values
are
if we want to get a local variable OUT OF a subroutine, we need to tell the function to
RETURN
that
value
this is then stored as a
variable
in the program that called the
function
in the same way you would store a
value
from a
user’s input
a function is a type of subroutine that
returns
a value
a procedure is a type of subroutine that does
not
return a value