COS 333: Chapter 9, Part 1
Summary
TLDRThis lecture delves into the concept of sub-programs, exploring their role in programming languages and their abstraction capabilities. It covers fundamental aspects such as sub-program definitions, headers, calls, and parameters, including formal and actual parameters, as well as parameter profiles and protocols. The lecture also touches on various programming languages' handling of sub-programs, including executable definitions, anonymous functions, and default parameters. It discusses the distinction between procedures and functions, and raises design issues like local variable allocation and the possibility of nested sub-program definitions.
Takeaways
- π The lecture introduces Chapter 9 focusing on sub-programs, explaining their role in programming languages and fundamental concepts.
- π Abstraction is central to modern programming, with process abstraction being discussed in Chapter 9 and data abstraction in Chapter 11.
- π΅οΈββοΈ Sub-programs, also known as functions or methods, have a single entry point and suspend the execution of the calling program until they complete.
- π The lecture emphasizes the importance of understanding and using correct terminology when discussing sub-programs for test and exam purposes.
- π Sub-program definitions include the interface, detailing how the sub-program is called, and the actions, specifying what the sub-program does.
- π Executable sub-program definitions in languages like Python can vary depending on execution conditions, affecting program language evaluation criteria.
- π·οΈ The lecture distinguishes between sub-program headers, which include the name and parameters, and sub-program calls, which are requests to execute a sub-program.
- π The correspondence between actual and formal parameters can be ensured by positional or keyword parameters, each with its advantages and disadvantages.
- π― Default parameters and variable numbers of parameters offer flexibility in how sub-programs can be called, with specific language requirements and behaviors.
- π οΈ The lecture discusses design issues related to sub-programs, including local variable allocation (static or dynamic) and the possibility of nested sub-program definitions.
- βοΈ Different programming languages handle sub-programs and local variables differently, with implications for recursion, storage sharing, access speed, and history sensitivity.
Q & A
What is the main focus of chapter 9 in the textbook?
-Chapter 9 focuses on sub-programs, discussing their role in programming languages, fundamental concepts, design issues, and local referencing environments.
What are the two fundamental facilities that support abstraction in modern programming?
-The two fundamental facilities that support abstraction are process abstraction and data abstraction, with the latter being supported by abstract data types (ADTs) and object-oriented programming (OOP).
What is the difference between a sub-program and a calling program during execution?
-A sub-program is a part of the code that can be called by another part of the program, the calling program. When a sub-program is called, the execution of the calling program is suspended until the sub-program completes its execution and control returns to the caller.
What are the two main components defined by a sub-program definition?
-A sub-program definition defines the interface of the sub-program, which includes its name, parameter types, and return type, and the actions of the sub-program, which is its body or the operations it performs.
How do executable sub-program definitions affect the evaluation criteria of a programming language?
-Executable sub-program definitions allow for different definitions of the same sub-program to exist based on the execution path, which can affect the predictability and evaluation of the program's behavior in a language.
What is the difference between a sub-program header and a sub-program call?
-A sub-program header is the first part of the sub-program definition that includes the name, type, and formal parameters of the sub-program. A sub-program call is an explicit request to execute the sub-program, which includes the actual parameters passed to the sub-program.
What are formal parameters and actual parameters in the context of sub-programs?
-Formal parameters are the variables listed in the header of the sub-program and used within its body. Actual parameters are the values or addresses passed to the sub-program during a call.
What is the purpose of a sub-program declaration?
-A sub-program declaration provides the protocol of the sub-program, which includes the number, order, and types of parameters, as well as the return type, without providing the body of the sub-program.
How do positional parameters ensure the correspondence between actual and formal parameters?
-Positional parameters ensure correspondence by binding the actual parameters to the formal parameters based on their position in the sub-program call.
What are the advantages and disadvantages of using keyword parameters?
-The advantage of keyword parameters is that they allow parameters to be specified in any order, reducing the chance of errors due to incorrect order. A potential disadvantage is that they may require more code to specify the parameter names, which could make the code more verbose.
Why must default parameters appear last in the list of parameters in C++?
-In C++, default parameters must appear last to ensure that if the function is called with fewer arguments than there are parameters, the correct default values are used without ambiguity.
What is the difference between default parameters and a variable number of parameters?
-Default parameters allow for some parameters to be omitted from a call, using predefined values, while a variable number of parameters allows a sub-program to accept any number of arguments, typically managed as an array or list.
Why must all parameters be of the same type when using a variable number of parameters in C#?
-In C#, the use of the 'params' keyword requires that all parameters be of the same type to ensure consistency when they are treated as an array within the method.
What is the distinction between procedures and functions in terms of sub-programs?
-Procedures are sub-programs that perform actions and may produce results by modifying global variables or parameters passed by reference. Functions are sub-programs that return a value and are generally expected to produce no side effects, although in practice they often do.
What are some design issues related to sub-programs in high-level programming languages?
-Design issues include whether local variables are statically or dynamically allocated, whether sub-program definitions can be nested, the parameter parsing methods provided, parameter type checking, the referencing environment of passed sub-programs, allowing functional side effects, the return types allowed from functions, sub-program overloading, support for generic sub-programs, and closure support for nested sub-programs.
Outlines
π Introduction to Sub-programs and Abstraction Concepts
This paragraph introduces the transition from discussing statement-level control structures to sub-programs in chapter 9 of the textbook. It emphasizes the importance of sub-programs in programming languages and their role in abstraction, alongside process and data abstraction. The lecture will cover the fundamentals of sub-programs, including their definitions, interfaces, and actions. It also introduces the concept of local referencing environments, crucial for testing and examination purposes.
π Fundamentals of Sub-programs and Their Definitions
The paragraph delves into the fundamental concepts of sub-programs, explaining their structure, entry points, and the suspension of the calling program's execution. It introduces key terminology such as sub-program definition, interface, and actions. The paragraph also discusses the concept of executable sub-program definitions in languages like Python, where the definition can vary depending on execution conditions, and the implications this has on program evaluation.
π Variations in Sub-program Definitions Across Languages
This section explores how different programming languages handle sub-program definitions. It discusses Ruby's approach to class member functions and object class methods, Lua's support for anonymous functions, and shorthand notations for defining functions. The paragraph highlights the importance of understanding sub-program headers, formal parameters, and sub-program calls, which are essential for grasping the structure and execution of sub-programs in various languages.
π Understanding Parameters, Profiles, and Protocols in Sub-programs
The paragraph focuses on the concepts of formal and actual parameters within sub-programs, explaining their roles and how they are used in computations. It introduces the parameter profile, often referred to as a signature, and the protocol of a sub-program, which includes the parameter profile and the return type for functions. The paragraph also discusses the sub-program declaration, which provides the protocol without the body, and how different programming languages, such as C and C++, utilize prototypes and implementation files.
π€ Matching Actual and Formal Parameters in Sub-program Calls
This paragraph discusses the importance of correctly matching actual parameters to formal parameters in sub-program calls to ensure accurate value mapping. It outlines two primary methods for ensuring this correspondence: positional parameters, which bind by position and are commonly used in languages like Java and C, and keyword parameters, which bind based on the names of formal parameters, allowing for more flexible parameter ordering and reducing correspondence errors.
π Default Parameters and Variable Number of Parameters
The paragraph examines the concepts of default parameters, which allow for the omission of certain actual parameters with preset values, and variable number of parameters, which enable sub-programs to accept an unspecified number of arguments. It explores how languages like C++, Ruby, and Python support these features, with a focus on the restrictions and requirements, such as the need for parameters to be of the same type in C++ and the allowance for mixed types in Ruby and Python.
π Variable Number of Parameters in Lua and the Concept of Procedures vs. Functions
This section explains how Lua supports a variable number of parameters through the use of tables and the syntax notation of three periods. It also discusses the distinction between procedures and functions, two types of sub-programs, highlighting that procedures may produce results by modifying external variables or parameters passed by reference, whereas functions are modeled on mathematical functions, expected to produce results through return values and minimize side effects.
π Design Issues in Sub-program Support for High-level Programming Languages
The paragraph addresses various design considerations for supporting sub-programs in high-level programming languages. It touches on local variable allocation, whether static or dynamic, the possibility of nested sub-program definitions, and the implications of each approach. The discussion sets the stage for further exploration of parameter parsing methods, parameter types, and the referencing environment of passed sub-programs in subsequent lectures.
π¦ Static vs. Dynamic Allocation of Local Variables
This final paragraph of the script contrasts static and dynamic allocation of local variables, discussing the advantages and disadvantages of each. It explains that modern programming languages typically use stack dynamic local variables, which support recursion and shared storage but may incur runtime allocation costs. The paragraph also notes that static local variables, while not commonly used, offer a different set of trade-offs and are explicitly declared in some languages like C.
Mindmap
Keywords
π‘Sub-programs
π‘Abstraction
π‘Subprogram Definition
π‘Executable Sub-program Definitions
π‘Formal Parameters
π‘Actual Parameters
π‘Parameter Profile
π‘Subprogram Call
π‘Positional Parameters
π‘Keyword Parameters
π‘Default Parameters
π‘Variable Number of Parameters
π‘Procedures vs. Functions
π‘Static vs. Dynamic Allocation
Highlights
Introduction to Chapter 9 on sub-programs, emphasizing their importance in programming languages.
Fundamental concepts of sub-programs are discussed, including their role in abstraction and programming.
Explanation of sub-programs as functions or methods, detailing their single entry point and execution suspension.
Importance of correct terminology when discussing sub-programs for test and exam purposes.
Sub-program definition includes interface and actions, affecting how sub-programs are called and utilized.
Executable sub-program definitions in languages like Python, allowing different definitions based on execution.
Ruby and Lua's approach to function definitions, including class members and anonymous functions.
Sub-program header and call, explaining the structure and invocation of sub-programs.
Formal and actual parameters distinction, crucial for understanding sub-program calls.
Parameter profile and protocol of a sub-program, including their role in function signatures and return types.
Sub-program declaration providing protocol without body, common in C and C++ prototypes.
Correspondence between actual and formal parameters through positional and keyword parameters.
Default parameters and their use in languages like C++, Python, Ruby, Ada, and PHP.
Variable number of parameters support in languages like C#, Ruby, and Python, allowing flexible argument lists.
Difference between procedures and functions, focusing on their respective results and side effects.
Design issues in sub-program allocation, such as static vs. dynamic local variable allocation.
Nested sub-program definitions and their implications for scoping and specific tasks.
Overview of how different programming languages handle local variable allocation and sub-program definitions.
Transcripts
in the previous lecture we finished off
our discussion on chapter 8 of the
textbook which dealt with statement
level control structures
in this lecture we will be beginning
with chapter 9 which discusses sub
programs
these are the topics that we'll be
discussing in today's lecture
we'll begin with a quick introduction
where we'll discuss where sub programs
fit into the broader picture of
programming languages
then we'll look at some of the
fundamental concepts that relate to
sub-programs which we need to know about
for the rest of the discussion
then we'll look at design issues related
to sub-programs and we'll finish this
lecture off by looking at local
referencing environments which are very
important for test and exam purposes
now the concept of abstraction is very
central to modern programming and there
are two fundamental facilities that
support abstraction
firstly we have process abstraction and
this was emphasized in the early days of
programming but is still very important
today as well
and process abstraction is what we will
be discussing in this chapter
secondly we have data abstraction and
this was emphasized in the 1980s
and data abstraction is supported by
means of abstract data types or adts and
object-oriented programming or oop
so data abstraction is discussed at
length in chapter 11 of the textbook
so let's now move on to the fundamentals
of sub-programs
you will know sub-programs as functions
or methods although we'll be going into
a lot more detail on the topic in this
lecture and the following two lectures
so each sub-program has a single entry
point
and we have a calling program that will
invoke or execute a sub program
so during the execution of the call sub
program the calling program's execution
is suspended and effectively stops at
that point and only once the call sub
program finishes its execution then
control will always return back to the
caller where execution will then once
again resume
now that we know what a sub-program is
and how it is called we need to go
through a couple of basic definitions of
terms that we'll be using throughout the
rest of this discussion
now it's very important that you
carefully study these terms when
preparing for a test or an exam
and the reason is that it's imperative
that you use the correct terminology so
that i know what it is that you're
talking about when discussing any aspect
of this work
so the first basic definition we'll look
at is the subprogram definition and this
defines two things
firstly the interface of the sub-program
and secondly the actions of the
sub-program
so the interface specifies how the
sub-program is actually used in other
words how the sub-program is called
and it includes details such as the name
of the sub program the types of the
parameters possibly the return type and
so on and so forth we'll get to more
detail on what the interface actually
entails in a moment
then in terms of the sub program's
actions this is a specification of what
the sub program actually does
in other words to use terminology that
you're used to it's the body of the sub
program
so now we'll look at some ways that
subprogram definitions are dealt with in
programming languages particularly where
the handling of these definitions
differs somewhat from what you will be
used to
in python subprogram definitions are
said to be executable so what this means
is that the subprogram definition
only comes into being when execution
reaches that sub-program definition
so what this means is we can have
different definitions for one particular
sub-program depending on the execution
of our program code so that's exactly
what we have in this example over here
you can see that we have an if statement
and if the condition for the if
statement is true
then we execute this definition for the
sub-program fun
alternatively if the condition for the
if is false then we enter the else
portion
of our selection statement and then we
reach this definition for the
sub-program fun so what this means then
is that in either case we will have a
definition for the sub program fun
but
the definition will perform different
tasks depending on the outcome of the if
statement
so what i would like you to do at this
point is to pause the video and try to
explain what effect these executable
sub-program definitions have on the
program language evaluation criteria
we've been using in this course
in ruby function definitions can appear
in two places either within the
definition of an object-oriented class
or outside of any class definition
so if a function definition appears in a
class definition then the function is a
member of that class exactly as you
would expect
however if the function definition
appears outside of any class
then it is a method of the object class
the object class being the topmost class
in the class hierarchy
however it is possible to call
these methods without an object in which
case the execution looks like a function
call in c
lua has functions that are anonymous so
anonymous in this context means that the
functions don't have names
so in order to use a function we must
assign the definition of the function
which is anonymous to a variable and
that's exactly what we do in this
example over here so here we've created
a function that doesn't have a name
associated with it we specify that it
receives a single parameter called x
and then in the body of our functions
definition we return the cube of the
parameter value
what we then do is we assign this
function definition to a variable named
cube and this means that the cube
variable now stores this anonymous
function that cubes its parameter
so from this point on we can then use
this variable in order to perform a call
to the anonymous function in this case
we are calling the anonymous function by
means of the name cube and passing
through a parameter of 2 to it we will
then print out the result of that
operations execution
now there is also a shorthand notation
that lure provides for us which you can
see in this example over here and this
looks much more conventional
and closer to what you will be used to
in your experience with programming so
far so here we can see that the name of
the function appears within the function
definition
the function also performs exactly the
same operation as in the previous
example what's important to understand
here though is that this is no different
to our first example over here
we've still
created an anonymous function which
we've then assigned to a variable named
cube so this is just a shorthand
notation but it performs the same
operation that we saw before
now that we know what a subprogram
definition is we get to the next two
important basic definitions of
terminology related to sub-programs
the first is the sub-program header and
this is the first part of the
sub-program definition and includes the
name of the sub-program the kind of sub
program that we're dealing with which
may be a procedure or a function and
we'll discuss the difference between
procedures and functions later on in
this lecture and then also the formal
parameters of the sub program which
we'll talk about in a moment
the next basic definition is for a
subprogram call and this is an explicit
subprogram execution request in which we
actually invoke the subprogram and get
it to perform a task
so here we have a simple example
illustrating the header and the sub
program call so over here we have our
subprogram definition
and this entire first line of our
subprogram definition is the subprogram
header so it includes the name of the
sub program as well as the formal
parameters
and then additionally it has the return
type here which indicates that we are
dealing with a function
down here we have a sub program called
so this is where we actually call the do
something sub program and pass through
two parameter values to it which gives
the thing to actually perform a concrete
task for us
when we are talking about the parameters
for a sub program there are two concepts
that you need to be aware of
formal parameters and actual parameters
so a formal parameter is a dummy
variable that is listed in the header of
the sub-program
and the formal parameter can be used in
the sub-program itself within the body
that has been defined for the sub
program
the actual parameters on the other hand
are values or addresses that are used
within a subprogram call
so here we have the same example that we
used on the previous slide and we can
see that the formal parameters are the
dummy variables first and second
and first and second can be used in the
body of the sub program in order to
perform computations
here we have the sub program call and
the actual parameters are then the
values that are synced through to the
parameters
of the sub program so the actual
parameters in this example are 1 and
23.7
the next two concepts that we need to
look at are the parameter profile and
the protocol of a sub-program
the parameter profile is often referred
to as a signature
and it includes the number order and
types of the parameters for the
sub-program
so in our previous example we saw that
the parameter first had a type of int
and the parameter second had a type of
float so therefore we can say that the
parameter profile of our example is two
parameters an int followed by a float
now the protocol of a sub program
includes the parameter profile but
additionally if the sub program is a
function then it also includes the
return type of that function
now we saw that the return type in our
previous example was void so therefore
the protocol for our sub program is two
parameters an int followed by a float
with a void return
the final basic definition that we need
to be aware of is the sub program
declaration
so the subprogram declaration provides
the sub program protocol
but not the body of the sub program so
in other words it incorporates the
number the order and the types of the
parameters as well as the return type
but not the actual implementation in the
body of the sub program
now in cnc plus plus the sub program
declarations are referred to as
prototypes
the prototypes will usually appear in a
header files over here we have an
example of a prototype in c or c
and we can see that it specifies the
number
the order and the types of the
parameters
here we have an int followed by a float
and then also the return type which is
void but we can see that it ends with a
semicolon in other words there is no
body present for this function
now the function definition then goes in
a separate implementation file and we
can see an example of that over here
where we again then have the header of
our function that appears on the first
line and we can see that we also then
have the
types of the two parameters specified
int and float respectively we also have
the formal parameters
as first and second specified in the
header and the return type of void as
well but then additionally we also then
have the body of our function where the
actual implementation will go
next we'll consider how the
correspondence between actual and formal
parameters
can be handled in a high-level
programming language
so whenever we have a sub-program call
then we need to match the actual
parameters which appear in the
subprogram call to the formal parameters
which appear in the sub program
definition
the reason this is necessary is we must
correctly map the values from the call
into the actual sub-program itself
and this has to be done correctly
to ensure that the values are correctly
used in the body of the sub program
now there are two ways that this
correspondence can be ensured and the
first approach is referred to as the use
of positional parameters
so here we bind the actual parameters to
the formal parameters by means of their
position
so the first actual parameter is bound
to the first formal parameter the second
actual parameter is bound to the second
formal parameter and so on and so forth
now most high-level programming
languages use this approach and you will
be familiar with it from java and c
now the main advantage with this
approach is that it is very safe because
the order is predefined
and it definitely does work as
illustrated by the fact that it is used
in the majority of high-level
programming languages now what i would
like you to do at this point is to pause
the video and try to think of at least
one disadvantage associated with
positional
parameters the second approach to
ensuring an accurate match between the
actual parameters and the formal
parameters of a sub-program
uses what are referred to as keyword
parameters
so when keyword parameters are used then
the binding between the actual and
formal parameters is based on the names
of the formal parameters
and these names of the formal parameters
are specified with the actual parameters
in a sub-program call so over here we
have an example of such a call where we
are calling a sub-program named add and
we are passing through
three parameter values namely 10
12 and 3 these are the actual parameters
however we must also then specify the
names of the formal parameters
for each of these actual parameters
namely first
second and finally third
so the advantage associated with the use
of keyword parameters is that the
parameters can appear in any order
for example with the add sub program
call we could have specified seconds
value first and first value last for
instance any particular order is
allowable because we have those names of
the formal parameters that are specified
within the call and so this avoids the
parameter correspondence errors
so if we were using positional
parameters it is possible that the
programmer might mix up the order of the
parameters and this isn't a problem when
we are working with keyword parameters
because we always have to explicitly
state which of the formal parameters we
are referring to
so what i would like you to do at this
point is again pause the video and try
to think of a disadvantage associated
with the use of keyword parameters
it is also possible to provide default
values for formal parameters and what
this allows us to do is leave out some
of the actual parameters in a subprogram
call
so in this case if we leave out some of
the actual parameters then the default
values are assumed for these parameters
that have been left out
but the defaults are used only if no
actual parameter is passed through for
the parameter in question
so default parameters are supported in c
plus python ruby ada and php
and in c plus default parameters must
appear last in the list of parameters
so two questions then related to this
why must the default parameters appear
last in c plus plus
and secondly how can languages allow for
default parameters to appear anywhere as
opposed to only last as is the case in c
plus so what i would like you to do is
to pause the video at this point and try
to answer these two questions
it is also possible in some programming
languages for a variable number of
parameters to be allowed for another
program
so in this case a sub program can then
have one or more actual parameters
and usually to all intents and purposes
there's no upper limit to how many
parameters can be passed through
now it's important to understand that a
variable number of parameters is not the
same thing as default parameters which
we discussed on the previous slide
default parameters are parameters that
can be left out from an existing set of
parameters that are specified for the
sub program whereas a variable number of
parameters are parameters that will be
processed like a list of values
c sharp supports a variable number of
parameters for its methods and it
achieves this by means of specifying a
formal parameter that is an array and is
also preceded by the special word params
now the parameters all have to be of
exactly the same type in order for a
variable number to be allowed
so what i would like you to do at this
point is once again pause the video
and try to explain why the parameters
must all be of the same type in c-sharp
so let's look at an example of how a
variable number of parameters for a
method would be implemented in c sharp
we are going to assume that we have a
class called myclass
and within it we have a method defined
called display list
we can see then that we have defined a
single
formal parameter
for the displaylist method we have
called this list and it is an integer
array and additionally we have specified
the special word params
we can then use this list just like a
normal array and here we use a for each
loop to process through each of the
elements in this array and then print
these values out to the screen
now there are two ways that we can use
the display list method
both of these require us to define an
instance of my class which we do over
here
so the instance here is called my object
we can then create an array
which contains a series of values so
here we've created an array of integer
values which is called my list
we've specified that a number of values
are contained within my list and then we
can simply call the displaylist method
and pass through the parameter of my
list as the actual parameter
the second approach that we can use is
again to call the displaylist method
over here but then we just simply pass
through a series of parameter values as
the actual parameters
and we can see then that as long as
these are all integer values then they
will be handled as an array containing
these values which the display list
method will then process through
ruby and python both use similar
approaches to supporting a variable
number of parameters
in ruby the actual parameters are seen
as elements of an array
and the array can contain mixed types so
the formal parameter is preceded by an
asterisk and this indicates that we are
then sending an array through
for that parameter
so over here we have an example of a
method that has been defined in ruby the
method is called tester and we can see
that we have three parameters specified
p1 p2 and p3
p1 and p2 will be passed as normal
parameters however p3 will be passed as
an array and we can then use the
elements within that array inside the
body of the taster method now python
uses a similar approach however it uses
a list
so what i would like you to do at this
point is to again pause the video
and try to explain why both ruby and
python allow for a mixture of types
within their variable number of
parameters
the reasons are different for the two
programming languages
finally lua also supports a variable
number of parameters for its
sub-programs
now in order to achieve this in lua a
sub-program must receive a table you
should recall that a table is used as a
general purpose data structure in lua
and can be used to represent a list of
values
so the notation then that is used in
terms of the syntax is that the formal
parameter
of three periods represents a table
which contains a variable number of
parameters
so the two ways that the parameter
values can be accessed the first
approach which we see in this example
over here uses a for statement so here
we are defining a function called
multiply and we can see that we have
received as a formal parameter three
periods this indicates that the multiply
function
will then receive a table which contains
a sequence of values
now we can inside the body of our
multiply function
provide a call to ipairs which
receives as a parameter the three
periods and what this does is it creates
an
iterator the iterator will be iterated
over by means of this for loop over here
and at each iteration i will take on the
index value for the
parameter value that is currently being
processed and next we'll actually take
on then the parameter value
we can then use next as you can see over
here in the body of our for loop and we
use next in order to multiply all of the
values within the table with one another
we then return eventually this product
which we have computed
now the second approach that we can use
is to use a multiple assignment from the
three periods which we do in this
example over here
so here we have a function called do it
and once again it receives three periods
as a formal parameter
this means once again that a table of
values is passed through to the do it
function
so over here we then have an assignment
that takes place we are assigning from
the three periods to a set of variables
namely a b and c which are all local
variables what this means is that the
first value in the table that is passed
through will be assigned to a the second
value will be assigned to b and the
third value will be assigned to c
another very important distinction to
make is the difference between
procedures and functions which are the
two categories of sub-programs that
exist
so procedures are collections of
statements that define parameterized
computations and they can produce
results in two ways firstly by modifying
variables that are visible from an
enclosing scope so for example they may
modify a global variable
secondly a result can also be produced
by modifying the formal parameters that
transfer data back to the caller so in
other words you would have a parameter
that is passed by reference and then
inside the body of the procedure
you would then modify that parameter
which would then modify the value for
the caller
so as such procedures then don't have
any return types they only return values
in inverted commas by modifying
variables that are visible within the
body of the procedure
functions on the other hand resemble
procedures but semantically they are
modeled on mathematical functions and we
discussed these in quite a lot of detail
in chapter 15.
so they produce results then by
returning a value
and they are expected in general
semantically at least to produce no side
effects but in practice
program functions do actually have side
effects in the majority of high level
programming languages a notable
exception would be pure functional
programming languages
now we get on to design issues related
to sub programs there are quite a number
of design issues that need to be
considered if some programs are to be
supported in a high level programming
language now we'll only be looking at
the first two in this lecture
so the first design issue is our local
variable allocations static or dynamic
and we've already touched on this in a
little bit of detail previously in the
course
then secondly can sub-program
definitions be nested now if sub-program
definitions can be nested then it means
we can place a nested sub-program within
a containing sub-program
now generally speaking nesting is used
so that the nested sub program
is defined only for the containing sub
program and as such a nested sub program
usually performs a task that is very
specific
to the containing sub program
we'll revisit nested sub programs a
little bit later on in our discussion on
this chapter
the next design issue that we need to
consider is what parameter parsing
methods are provided and we'll discuss
that at the start of the next lecture
then our parameter types checked
and if not then what kind of drawbacks
will this
entail for our programming language
then if sub-programs can be passed as
parameters and sub-programs can be
nested then what is the referencing
environment of a past sub-program this
is a fairly complex question and we'll
be looking at it in some detail in the
next lecture
then are functional side effects allowed
we've discussed functional side effects
in quite a lot of detail in chapter 15
so we won't go into any further detail
on that here
then what return types are allowed from
functions
and how many values can a function
return
certain programming languages allow
functions to return multiple values
together as a single unit and other
programming languages only allow a
single value to be returned
then can sub-programs be overloaded
and does our programming language
support generic sub-programs
you will have had a little bit of
experience with generic sub programs in
terms of template programming in c plus
plus
and then finally if nested sub programs
are legal then our closures supported
and again we'll be going into quite a
lot of detail on what closures actually
are in the third lecture on this chapter
let's now look at the first of our
design issues which relates to where the
local variables are statically or
dynamically allocated
so recall from chapter 5 that local
variables are very often stacked dynamic
in nature and this means that the scope
and the storage allocation of the
variable go from variable elaboration
time to the end of the scope that the
variable appears within
the advantages associated with these
stack dynamic variables is that they
support recursion and also the storage
for local variables is shared amongst
all of the sub programs and this is
because the storage gets de-allocated
once the sub-program terminates
the disadvantages associated with
stackdynamic variables is that there is
some time spent for the allocation
because the allocation doesn't happen
statically prior to runtime and
therefore the allocation takes up some
run time
also initialization and de-allocation
take up some additional time as well
also access to the stack stackdynamic
variables is handled by means of
indirect addressing so this means that
access is slower than it would be for a
static variable
and then also sub-programs cannot be
history sensitive
because we need support for static
variables in order to create history
sensitive variables within sub programs
now local variables can also be static
in nature and the advantages and
disadvantages associated with static
local variables are the opposite of
those four stack dynamic local variables
so let's look at how static and dynamic
variables are dealt with in a number of
programming languages
so in most of the modern day programming
languages that exist local variables are
stack dynamic
in the c based languages local variables
are by default stack dynamic however
they can be explicitly declared as
static with the static keyword in which
case they are then static local
variables
the methods in java python and c-sharp
only have stack dynamic local variables
and in lua if we have an implicit
declaration of a variable then that
variable will be global in nature
however local variables which must be
declared with the special word local
explicitly
are stack dynamic variables
all right so that concludes our
discussion for this lecture in the next
lecture we will continue our discussion
on the chapter we will talk about
parameter parsing methods parameters
that are sucker programs and calling
sub-programs indirectly
5.0 / 5 (0 votes)