COS 333: Chapter 9, Part 1

Willem van Heerden
14 Sept 202136:31

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

00:00

πŸ“š 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.

05:02

πŸ” 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.

10:05

🏭 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.

15:05

πŸ”„ 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.

20:07

🀝 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.

25:09

πŸ”„ 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.

30:10

🌐 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.

35:14

πŸ›  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

Sub-programs, also known as functions or methods, are reusable blocks of code that perform a specific task. Defined by a single entry point, they are invoked by a calling program whose execution is temporarily suspended until the sub-program completes. This concept is central to the video's theme of discussing programming structures, with examples given in various programming languages to illustrate their usage.

πŸ’‘Abstraction

Abstraction in programming is a fundamental concept that allows programmers to manage complexity by hiding implementation details and exposing only necessary information. The video emphasizes two types of abstraction: process abstraction, which is the focus of the chapter on sub-programs, and data abstraction, which is supported by abstract data types and object-oriented programming, discussed in a later chapter.

πŸ’‘Subprogram Definition

A subprogram definition includes the interface and actions of a sub-program. The interface specifies how the sub-program is called, including its name, parameter types, and return type, while the actions define what the sub-program does. This term is crucial for understanding how sub-programs are structured and used within programming languages, as it lays out the necessary components for their creation and invocation.

πŸ’‘Executable Sub-program Definitions

Executable sub-program definitions are created at runtime, allowing for different definitions of the same sub-program based on execution conditions. This concept is illustrated with an example in the script where an 'if' statement determines which definition of 'fun' is executed. This flexibility impacts how programming languages evaluate and execute code.

πŸ’‘Formal Parameters

Formal parameters are placeholders for the actual values or addresses that will be passed to a sub-program during a call. They are defined in the sub-program's header and used within its body. The script explains that formal parameters are essential for mapping actual parameters to the correct variables within the sub-program, ensuring the correct execution of code.

πŸ’‘Actual Parameters

Actual parameters are the concrete values or variable addresses that are passed to a sub-program when it is called. They correspond to the formal parameters defined in the sub-program. The script discusses how actual parameters are used in conjunction with formal parameters to ensure that the correct data is passed to and used within the sub-program.

πŸ’‘Parameter Profile

The parameter profile, often referred to as a signature, includes the number, order, and types of parameters for a sub-program. It is part of the sub-program's interface and is essential for understanding how to correctly call a sub-program with the appropriate arguments. The script uses the parameter profile to illustrate the structure of sub-programs and their invocation.

πŸ’‘Subprogram Call

A subprogram call is an explicit request to execute a sub-program, providing the necessary parameters for it to perform its task. The script describes how a subprogram call is made, including the use of actual parameters that correspond to the formal parameters defined in the sub-program's header.

πŸ’‘Positional Parameters

Positional parameters are bound to formal parameters based on their order in a sub-program call. The script explains that this is a common approach in many high-level programming languages, providing safety due to the predefined order. However, it also prompts viewers to consider potential disadvantages, such as the risk of mixing up the order of parameters.

πŸ’‘Keyword Parameters

Keyword parameters allow the binding of actual parameters to formal parameters based on the names of the formal parameters, rather than their position. This approach, used in some programming languages as discussed in the script, enables parameters to be specified in any order, reducing the risk of correspondence errors. However, it also has potential disadvantages, such as increased complexity in mapping parameter names.

πŸ’‘Default Parameters

Default parameters provide default values for formal parameters that can be assumed if no actual parameter is passed during a sub-program call. Supported in languages like C++, Python, Ruby, and PHP, default parameters offer flexibility in how sub-programs are called. The script raises questions about their placement and usage, highlighting their role in programming language design.

πŸ’‘Variable Number of Parameters

Some programming languages allow sub-programs to accept a variable number of parameters, which are processed as a list of values. The script explains how this is achieved in C# using an array preceded by the 'params' keyword, and it prompts viewers to consider why all parameters must be of the same type. This feature expands the versatility of sub-programs in handling different numbers of inputs.

πŸ’‘Procedures vs. Functions

Procedures and functions are two categories of sub-programs with distinct characteristics. Procedures perform computations and can produce results by modifying global variables or parameters passed by reference. Functions, on the other hand, are modeled on mathematical functions and return a value, ideally producing no side effects. The script discusses the semantic differences and usage within programming languages.

πŸ’‘Static vs. Dynamic Allocation

Static allocation refers to the allocation of local variables at compile time, while dynamic allocation occurs at runtime. The script discusses the advantages and disadvantages of each method, noting that most modern programming languages use stack dynamic allocation for local variables, which supports recursion but may incur runtime allocation costs.

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

play00:01

in the previous lecture we finished off

play00:03

our discussion on chapter 8 of the

play00:05

textbook which dealt with statement

play00:08

level control structures

play00:10

in this lecture we will be beginning

play00:13

with chapter 9 which discusses sub

play00:16

programs

play00:19

these are the topics that we'll be

play00:20

discussing in today's lecture

play00:23

we'll begin with a quick introduction

play00:25

where we'll discuss where sub programs

play00:28

fit into the broader picture of

play00:31

programming languages

play00:33

then we'll look at some of the

play00:34

fundamental concepts that relate to

play00:37

sub-programs which we need to know about

play00:39

for the rest of the discussion

play00:41

then we'll look at design issues related

play00:44

to sub-programs and we'll finish this

play00:46

lecture off by looking at local

play00:49

referencing environments which are very

play00:51

important for test and exam purposes

play00:57

now the concept of abstraction is very

play01:00

central to modern programming and there

play01:03

are two fundamental facilities that

play01:06

support abstraction

play01:08

firstly we have process abstraction and

play01:10

this was emphasized in the early days of

play01:13

programming but is still very important

play01:15

today as well

play01:17

and process abstraction is what we will

play01:19

be discussing in this chapter

play01:22

secondly we have data abstraction and

play01:25

this was emphasized in the 1980s

play01:28

and data abstraction is supported by

play01:30

means of abstract data types or adts and

play01:34

object-oriented programming or oop

play01:38

so data abstraction is discussed at

play01:41

length in chapter 11 of the textbook

play01:46

so let's now move on to the fundamentals

play01:49

of sub-programs

play01:50

you will know sub-programs as functions

play01:53

or methods although we'll be going into

play01:55

a lot more detail on the topic in this

play01:58

lecture and the following two lectures

play02:01

so each sub-program has a single entry

play02:04

point

play02:05

and we have a calling program that will

play02:08

invoke or execute a sub program

play02:12

so during the execution of the call sub

play02:15

program the calling program's execution

play02:17

is suspended and effectively stops at

play02:20

that point and only once the call sub

play02:24

program finishes its execution then

play02:26

control will always return back to the

play02:29

caller where execution will then once

play02:32

again resume

play02:36

now that we know what a sub-program is

play02:39

and how it is called we need to go

play02:41

through a couple of basic definitions of

play02:44

terms that we'll be using throughout the

play02:47

rest of this discussion

play02:48

now it's very important that you

play02:51

carefully study these terms when

play02:53

preparing for a test or an exam

play02:56

and the reason is that it's imperative

play02:58

that you use the correct terminology so

play03:01

that i know what it is that you're

play03:03

talking about when discussing any aspect

play03:06

of this work

play03:08

so the first basic definition we'll look

play03:10

at is the subprogram definition and this

play03:13

defines two things

play03:15

firstly the interface of the sub-program

play03:18

and secondly the actions of the

play03:20

sub-program

play03:21

so the interface specifies how the

play03:24

sub-program is actually used in other

play03:27

words how the sub-program is called

play03:29

and it includes details such as the name

play03:33

of the sub program the types of the

play03:35

parameters possibly the return type and

play03:38

so on and so forth we'll get to more

play03:41

detail on what the interface actually

play03:43

entails in a moment

play03:46

then in terms of the sub program's

play03:49

actions this is a specification of what

play03:52

the sub program actually does

play03:54

in other words to use terminology that

play03:56

you're used to it's the body of the sub

play03:59

program

play04:02

so now we'll look at some ways that

play04:04

subprogram definitions are dealt with in

play04:08

programming languages particularly where

play04:10

the handling of these definitions

play04:12

differs somewhat from what you will be

play04:14

used to

play04:16

in python subprogram definitions are

play04:18

said to be executable so what this means

play04:21

is that the subprogram definition

play04:24

only comes into being when execution

play04:28

reaches that sub-program definition

play04:31

so what this means is we can have

play04:33

different definitions for one particular

play04:36

sub-program depending on the execution

play04:39

of our program code so that's exactly

play04:42

what we have in this example over here

play04:44

you can see that we have an if statement

play04:47

and if the condition for the if

play04:49

statement is true

play04:51

then we execute this definition for the

play04:54

sub-program fun

play04:56

alternatively if the condition for the

play04:59

if is false then we enter the else

play05:02

portion

play05:03

of our selection statement and then we

play05:06

reach this definition for the

play05:08

sub-program fun so what this means then

play05:12

is that in either case we will have a

play05:15

definition for the sub program fun

play05:19

but

play05:20

the definition will perform different

play05:23

tasks depending on the outcome of the if

play05:27

statement

play05:28

so what i would like you to do at this

play05:29

point is to pause the video and try to

play05:32

explain what effect these executable

play05:35

sub-program definitions have on the

play05:37

program language evaluation criteria

play05:40

we've been using in this course

play05:47

in ruby function definitions can appear

play05:50

in two places either within the

play05:53

definition of an object-oriented class

play05:57

or outside of any class definition

play06:00

so if a function definition appears in a

play06:03

class definition then the function is a

play06:06

member of that class exactly as you

play06:09

would expect

play06:10

however if the function definition

play06:13

appears outside of any class

play06:16

then it is a method of the object class

play06:19

the object class being the topmost class

play06:22

in the class hierarchy

play06:25

however it is possible to call

play06:28

these methods without an object in which

play06:32

case the execution looks like a function

play06:35

call in c

play06:40

lua has functions that are anonymous so

play06:44

anonymous in this context means that the

play06:46

functions don't have names

play06:48

so in order to use a function we must

play06:51

assign the definition of the function

play06:54

which is anonymous to a variable and

play06:57

that's exactly what we do in this

play06:59

example over here so here we've created

play07:02

a function that doesn't have a name

play07:05

associated with it we specify that it

play07:08

receives a single parameter called x

play07:11

and then in the body of our functions

play07:14

definition we return the cube of the

play07:17

parameter value

play07:19

what we then do is we assign this

play07:21

function definition to a variable named

play07:24

cube and this means that the cube

play07:26

variable now stores this anonymous

play07:29

function that cubes its parameter

play07:32

so from this point on we can then use

play07:34

this variable in order to perform a call

play07:38

to the anonymous function in this case

play07:41

we are calling the anonymous function by

play07:44

means of the name cube and passing

play07:46

through a parameter of 2 to it we will

play07:50

then print out the result of that

play07:52

operations execution

play07:54

now there is also a shorthand notation

play07:57

that lure provides for us which you can

play07:59

see in this example over here and this

play08:02

looks much more conventional

play08:04

and closer to what you will be used to

play08:08

in your experience with programming so

play08:10

far so here we can see that the name of

play08:13

the function appears within the function

play08:15

definition

play08:16

the function also performs exactly the

play08:19

same operation as in the previous

play08:22

example what's important to understand

play08:24

here though is that this is no different

play08:27

to our first example over here

play08:30

we've still

play08:31

created an anonymous function which

play08:34

we've then assigned to a variable named

play08:37

cube so this is just a shorthand

play08:39

notation but it performs the same

play08:42

operation that we saw before

play08:46

now that we know what a subprogram

play08:48

definition is we get to the next two

play08:52

important basic definitions of

play08:55

terminology related to sub-programs

play08:58

the first is the sub-program header and

play09:01

this is the first part of the

play09:03

sub-program definition and includes the

play09:06

name of the sub-program the kind of sub

play09:09

program that we're dealing with which

play09:11

may be a procedure or a function and

play09:14

we'll discuss the difference between

play09:16

procedures and functions later on in

play09:18

this lecture and then also the formal

play09:21

parameters of the sub program which

play09:24

we'll talk about in a moment

play09:27

the next basic definition is for a

play09:30

subprogram call and this is an explicit

play09:33

subprogram execution request in which we

play09:36

actually invoke the subprogram and get

play09:39

it to perform a task

play09:42

so here we have a simple example

play09:45

illustrating the header and the sub

play09:48

program call so over here we have our

play09:52

subprogram definition

play09:54

and this entire first line of our

play09:58

subprogram definition is the subprogram

play10:01

header so it includes the name of the

play10:04

sub program as well as the formal

play10:08

parameters

play10:09

and then additionally it has the return

play10:12

type here which indicates that we are

play10:14

dealing with a function

play10:16

down here we have a sub program called

play10:19

so this is where we actually call the do

play10:21

something sub program and pass through

play10:25

two parameter values to it which gives

play10:27

the thing to actually perform a concrete

play10:30

task for us

play10:34

when we are talking about the parameters

play10:36

for a sub program there are two concepts

play10:40

that you need to be aware of

play10:42

formal parameters and actual parameters

play10:45

so a formal parameter is a dummy

play10:47

variable that is listed in the header of

play10:50

the sub-program

play10:52

and the formal parameter can be used in

play10:55

the sub-program itself within the body

play10:58

that has been defined for the sub

play11:00

program

play11:01

the actual parameters on the other hand

play11:04

are values or addresses that are used

play11:07

within a subprogram call

play11:10

so here we have the same example that we

play11:13

used on the previous slide and we can

play11:16

see that the formal parameters are the

play11:18

dummy variables first and second

play11:22

and first and second can be used in the

play11:24

body of the sub program in order to

play11:28

perform computations

play11:30

here we have the sub program call and

play11:33

the actual parameters are then the

play11:35

values that are synced through to the

play11:37

parameters

play11:39

of the sub program so the actual

play11:41

parameters in this example are 1 and

play11:44

23.7

play11:48

the next two concepts that we need to

play11:51

look at are the parameter profile and

play11:54

the protocol of a sub-program

play11:57

the parameter profile is often referred

play12:00

to as a signature

play12:02

and it includes the number order and

play12:06

types of the parameters for the

play12:08

sub-program

play12:10

so in our previous example we saw that

play12:12

the parameter first had a type of int

play12:16

and the parameter second had a type of

play12:18

float so therefore we can say that the

play12:20

parameter profile of our example is two

play12:24

parameters an int followed by a float

play12:29

now the protocol of a sub program

play12:32

includes the parameter profile but

play12:34

additionally if the sub program is a

play12:37

function then it also includes the

play12:40

return type of that function

play12:43

now we saw that the return type in our

play12:46

previous example was void so therefore

play12:49

the protocol for our sub program is two

play12:53

parameters an int followed by a float

play12:57

with a void return

play13:01

the final basic definition that we need

play13:04

to be aware of is the sub program

play13:07

declaration

play13:08

so the subprogram declaration provides

play13:11

the sub program protocol

play13:14

but not the body of the sub program so

play13:17

in other words it incorporates the

play13:20

number the order and the types of the

play13:23

parameters as well as the return type

play13:26

but not the actual implementation in the

play13:29

body of the sub program

play13:31

now in cnc plus plus the sub program

play13:34

declarations are referred to as

play13:37

prototypes

play13:38

the prototypes will usually appear in a

play13:41

header files over here we have an

play13:43

example of a prototype in c or c

play13:49

and we can see that it specifies the

play13:52

number

play13:53

the order and the types of the

play13:55

parameters

play13:56

here we have an int followed by a float

play14:00

and then also the return type which is

play14:02

void but we can see that it ends with a

play14:04

semicolon in other words there is no

play14:08

body present for this function

play14:12

now the function definition then goes in

play14:14

a separate implementation file and we

play14:17

can see an example of that over here

play14:19

where we again then have the header of

play14:23

our function that appears on the first

play14:27

line and we can see that we also then

play14:29

have the

play14:31

types of the two parameters specified

play14:34

int and float respectively we also have

play14:38

the formal parameters

play14:41

as first and second specified in the

play14:44

header and the return type of void as

play14:47

well but then additionally we also then

play14:51

have the body of our function where the

play14:54

actual implementation will go

play14:59

next we'll consider how the

play15:01

correspondence between actual and formal

play15:03

parameters

play15:05

can be handled in a high-level

play15:07

programming language

play15:08

so whenever we have a sub-program call

play15:11

then we need to match the actual

play15:13

parameters which appear in the

play15:15

subprogram call to the formal parameters

play15:19

which appear in the sub program

play15:21

definition

play15:22

the reason this is necessary is we must

play15:26

correctly map the values from the call

play15:29

into the actual sub-program itself

play15:33

and this has to be done correctly

play15:36

to ensure that the values are correctly

play15:38

used in the body of the sub program

play15:41

now there are two ways that this

play15:43

correspondence can be ensured and the

play15:46

first approach is referred to as the use

play15:50

of positional parameters

play15:52

so here we bind the actual parameters to

play15:56

the formal parameters by means of their

play15:59

position

play16:00

so the first actual parameter is bound

play16:02

to the first formal parameter the second

play16:05

actual parameter is bound to the second

play16:07

formal parameter and so on and so forth

play16:11

now most high-level programming

play16:13

languages use this approach and you will

play16:15

be familiar with it from java and c

play16:20

now the main advantage with this

play16:21

approach is that it is very safe because

play16:24

the order is predefined

play16:27

and it definitely does work as

play16:30

illustrated by the fact that it is used

play16:32

in the majority of high-level

play16:34

programming languages now what i would

play16:36

like you to do at this point is to pause

play16:38

the video and try to think of at least

play16:41

one disadvantage associated with

play16:44

positional

play16:48

parameters the second approach to

play16:51

ensuring an accurate match between the

play16:54

actual parameters and the formal

play16:56

parameters of a sub-program

play16:58

uses what are referred to as keyword

play17:01

parameters

play17:02

so when keyword parameters are used then

play17:05

the binding between the actual and

play17:07

formal parameters is based on the names

play17:10

of the formal parameters

play17:12

and these names of the formal parameters

play17:15

are specified with the actual parameters

play17:18

in a sub-program call so over here we

play17:21

have an example of such a call where we

play17:25

are calling a sub-program named add and

play17:29

we are passing through

play17:30

three parameter values namely 10

play17:34

12 and 3 these are the actual parameters

play17:38

however we must also then specify the

play17:41

names of the formal parameters

play17:44

for each of these actual parameters

play17:46

namely first

play17:48

second and finally third

play17:52

so the advantage associated with the use

play17:54

of keyword parameters is that the

play17:56

parameters can appear in any order

play17:59

for example with the add sub program

play18:03

call we could have specified seconds

play18:06

value first and first value last for

play18:09

instance any particular order is

play18:12

allowable because we have those names of

play18:15

the formal parameters that are specified

play18:18

within the call and so this avoids the

play18:22

parameter correspondence errors

play18:25

so if we were using positional

play18:27

parameters it is possible that the

play18:29

programmer might mix up the order of the

play18:31

parameters and this isn't a problem when

play18:35

we are working with keyword parameters

play18:37

because we always have to explicitly

play18:39

state which of the formal parameters we

play18:42

are referring to

play18:44

so what i would like you to do at this

play18:45

point is again pause the video and try

play18:48

to think of a disadvantage associated

play18:51

with the use of keyword parameters

play18:59

it is also possible to provide default

play19:01

values for formal parameters and what

play19:04

this allows us to do is leave out some

play19:07

of the actual parameters in a subprogram

play19:11

call

play19:12

so in this case if we leave out some of

play19:16

the actual parameters then the default

play19:20

values are assumed for these parameters

play19:22

that have been left out

play19:25

but the defaults are used only if no

play19:28

actual parameter is passed through for

play19:32

the parameter in question

play19:35

so default parameters are supported in c

play19:38

plus python ruby ada and php

play19:42

and in c plus default parameters must

play19:45

appear last in the list of parameters

play19:49

so two questions then related to this

play19:53

why must the default parameters appear

play19:56

last in c plus plus

play19:59

and secondly how can languages allow for

play20:02

default parameters to appear anywhere as

play20:06

opposed to only last as is the case in c

play20:10

plus so what i would like you to do is

play20:12

to pause the video at this point and try

play20:14

to answer these two questions

play20:22

it is also possible in some programming

play20:24

languages for a variable number of

play20:27

parameters to be allowed for another

play20:30

program

play20:30

so in this case a sub program can then

play20:33

have one or more actual parameters

play20:36

and usually to all intents and purposes

play20:39

there's no upper limit to how many

play20:41

parameters can be passed through

play20:44

now it's important to understand that a

play20:46

variable number of parameters is not the

play20:48

same thing as default parameters which

play20:51

we discussed on the previous slide

play20:54

default parameters are parameters that

play20:56

can be left out from an existing set of

play21:00

parameters that are specified for the

play21:02

sub program whereas a variable number of

play21:05

parameters are parameters that will be

play21:08

processed like a list of values

play21:13

c sharp supports a variable number of

play21:16

parameters for its methods and it

play21:19

achieves this by means of specifying a

play21:22

formal parameter that is an array and is

play21:25

also preceded by the special word params

play21:28

now the parameters all have to be of

play21:31

exactly the same type in order for a

play21:34

variable number to be allowed

play21:36

so what i would like you to do at this

play21:38

point is once again pause the video

play21:41

and try to explain why the parameters

play21:44

must all be of the same type in c-sharp

play21:53

so let's look at an example of how a

play21:56

variable number of parameters for a

play21:58

method would be implemented in c sharp

play22:02

we are going to assume that we have a

play22:04

class called myclass

play22:06

and within it we have a method defined

play22:10

called display list

play22:13

we can see then that we have defined a

play22:16

single

play22:17

formal parameter

play22:19

for the displaylist method we have

play22:22

called this list and it is an integer

play22:25

array and additionally we have specified

play22:28

the special word params

play22:31

we can then use this list just like a

play22:34

normal array and here we use a for each

play22:37

loop to process through each of the

play22:40

elements in this array and then print

play22:43

these values out to the screen

play22:46

now there are two ways that we can use

play22:49

the display list method

play22:51

both of these require us to define an

play22:54

instance of my class which we do over

play22:56

here

play22:57

so the instance here is called my object

play23:01

we can then create an array

play23:05

which contains a series of values so

play23:07

here we've created an array of integer

play23:10

values which is called my list

play23:13

we've specified that a number of values

play23:15

are contained within my list and then we

play23:18

can simply call the displaylist method

play23:22

and pass through the parameter of my

play23:26

list as the actual parameter

play23:29

the second approach that we can use is

play23:31

again to call the displaylist method

play23:34

over here but then we just simply pass

play23:37

through a series of parameter values as

play23:41

the actual parameters

play23:43

and we can see then that as long as

play23:45

these are all integer values then they

play23:49

will be handled as an array containing

play23:52

these values which the display list

play23:54

method will then process through

play23:58

ruby and python both use similar

play24:01

approaches to supporting a variable

play24:04

number of parameters

play24:05

in ruby the actual parameters are seen

play24:09

as elements of an array

play24:12

and the array can contain mixed types so

play24:16

the formal parameter is preceded by an

play24:19

asterisk and this indicates that we are

play24:22

then sending an array through

play24:24

for that parameter

play24:26

so over here we have an example of a

play24:29

method that has been defined in ruby the

play24:32

method is called tester and we can see

play24:35

that we have three parameters specified

play24:37

p1 p2 and p3

play24:40

p1 and p2 will be passed as normal

play24:43

parameters however p3 will be passed as

play24:47

an array and we can then use the

play24:50

elements within that array inside the

play24:52

body of the taster method now python

play24:56

uses a similar approach however it uses

play24:59

a list

play25:00

so what i would like you to do at this

play25:02

point is to again pause the video

play25:05

and try to explain why both ruby and

play25:08

python allow for a mixture of types

play25:12

within their variable number of

play25:14

parameters

play25:16

the reasons are different for the two

play25:18

programming languages

play25:26

finally lua also supports a variable

play25:28

number of parameters for its

play25:31

sub-programs

play25:32

now in order to achieve this in lua a

play25:35

sub-program must receive a table you

play25:39

should recall that a table is used as a

play25:42

general purpose data structure in lua

play25:45

and can be used to represent a list of

play25:48

values

play25:50

so the notation then that is used in

play25:53

terms of the syntax is that the formal

play25:56

parameter

play25:58

of three periods represents a table

play26:01

which contains a variable number of

play26:04

parameters

play26:05

so the two ways that the parameter

play26:08

values can be accessed the first

play26:10

approach which we see in this example

play26:12

over here uses a for statement so here

play26:16

we are defining a function called

play26:18

multiply and we can see that we have

play26:20

received as a formal parameter three

play26:24

periods this indicates that the multiply

play26:28

function

play26:29

will then receive a table which contains

play26:32

a sequence of values

play26:35

now we can inside the body of our

play26:38

multiply function

play26:40

provide a call to ipairs which

play26:44

receives as a parameter the three

play26:47

periods and what this does is it creates

play26:50

an

play26:50

iterator the iterator will be iterated

play26:54

over by means of this for loop over here

play26:58

and at each iteration i will take on the

play27:02

index value for the

play27:05

parameter value that is currently being

play27:07

processed and next we'll actually take

play27:10

on then the parameter value

play27:13

we can then use next as you can see over

play27:16

here in the body of our for loop and we

play27:20

use next in order to multiply all of the

play27:23

values within the table with one another

play27:27

we then return eventually this product

play27:31

which we have computed

play27:34

now the second approach that we can use

play27:37

is to use a multiple assignment from the

play27:39

three periods which we do in this

play27:41

example over here

play27:43

so here we have a function called do it

play27:46

and once again it receives three periods

play27:48

as a formal parameter

play27:51

this means once again that a table of

play27:54

values is passed through to the do it

play27:57

function

play27:58

so over here we then have an assignment

play28:01

that takes place we are assigning from

play28:03

the three periods to a set of variables

play28:07

namely a b and c which are all local

play28:11

variables what this means is that the

play28:14

first value in the table that is passed

play28:16

through will be assigned to a the second

play28:19

value will be assigned to b and the

play28:21

third value will be assigned to c

play28:26

another very important distinction to

play28:28

make is the difference between

play28:30

procedures and functions which are the

play28:33

two categories of sub-programs that

play28:36

exist

play28:37

so procedures are collections of

play28:40

statements that define parameterized

play28:42

computations and they can produce

play28:45

results in two ways firstly by modifying

play28:48

variables that are visible from an

play28:50

enclosing scope so for example they may

play28:54

modify a global variable

play28:57

secondly a result can also be produced

play28:59

by modifying the formal parameters that

play29:02

transfer data back to the caller so in

play29:05

other words you would have a parameter

play29:07

that is passed by reference and then

play29:10

inside the body of the procedure

play29:12

you would then modify that parameter

play29:15

which would then modify the value for

play29:18

the caller

play29:19

so as such procedures then don't have

play29:22

any return types they only return values

play29:26

in inverted commas by modifying

play29:28

variables that are visible within the

play29:30

body of the procedure

play29:33

functions on the other hand resemble

play29:35

procedures but semantically they are

play29:38

modeled on mathematical functions and we

play29:41

discussed these in quite a lot of detail

play29:43

in chapter 15.

play29:46

so they produce results then by

play29:48

returning a value

play29:50

and they are expected in general

play29:53

semantically at least to produce no side

play29:56

effects but in practice

play29:59

program functions do actually have side

play30:02

effects in the majority of high level

play30:05

programming languages a notable

play30:07

exception would be pure functional

play30:10

programming languages

play30:14

now we get on to design issues related

play30:17

to sub programs there are quite a number

play30:20

of design issues that need to be

play30:22

considered if some programs are to be

play30:25

supported in a high level programming

play30:28

language now we'll only be looking at

play30:30

the first two in this lecture

play30:33

so the first design issue is our local

play30:36

variable allocations static or dynamic

play30:39

and we've already touched on this in a

play30:41

little bit of detail previously in the

play30:43

course

play30:44

then secondly can sub-program

play30:47

definitions be nested now if sub-program

play30:51

definitions can be nested then it means

play30:53

we can place a nested sub-program within

play30:57

a containing sub-program

play30:59

now generally speaking nesting is used

play31:03

so that the nested sub program

play31:06

is defined only for the containing sub

play31:09

program and as such a nested sub program

play31:13

usually performs a task that is very

play31:15

specific

play31:16

to the containing sub program

play31:20

we'll revisit nested sub programs a

play31:22

little bit later on in our discussion on

play31:25

this chapter

play31:27

the next design issue that we need to

play31:29

consider is what parameter parsing

play31:32

methods are provided and we'll discuss

play31:34

that at the start of the next lecture

play31:38

then our parameter types checked

play31:42

and if not then what kind of drawbacks

play31:45

will this

play31:46

entail for our programming language

play31:49

then if sub-programs can be passed as

play31:52

parameters and sub-programs can be

play31:54

nested then what is the referencing

play31:56

environment of a past sub-program this

play32:00

is a fairly complex question and we'll

play32:02

be looking at it in some detail in the

play32:05

next lecture

play32:07

then are functional side effects allowed

play32:10

we've discussed functional side effects

play32:11

in quite a lot of detail in chapter 15

play32:15

so we won't go into any further detail

play32:17

on that here

play32:19

then what return types are allowed from

play32:23

functions

play32:24

and how many values can a function

play32:27

return

play32:29

certain programming languages allow

play32:31

functions to return multiple values

play32:33

together as a single unit and other

play32:36

programming languages only allow a

play32:38

single value to be returned

play32:41

then can sub-programs be overloaded

play32:45

and does our programming language

play32:48

support generic sub-programs

play32:51

you will have had a little bit of

play32:53

experience with generic sub programs in

play32:56

terms of template programming in c plus

play32:59

plus

play33:01

and then finally if nested sub programs

play33:04

are legal then our closures supported

play33:07

and again we'll be going into quite a

play33:08

lot of detail on what closures actually

play33:11

are in the third lecture on this chapter

play33:16

let's now look at the first of our

play33:19

design issues which relates to where the

play33:22

local variables are statically or

play33:24

dynamically allocated

play33:26

so recall from chapter 5 that local

play33:29

variables are very often stacked dynamic

play33:31

in nature and this means that the scope

play33:34

and the storage allocation of the

play33:37

variable go from variable elaboration

play33:40

time to the end of the scope that the

play33:43

variable appears within

play33:45

the advantages associated with these

play33:48

stack dynamic variables is that they

play33:50

support recursion and also the storage

play33:54

for local variables is shared amongst

play33:56

all of the sub programs and this is

play33:58

because the storage gets de-allocated

play34:01

once the sub-program terminates

play34:04

the disadvantages associated with

play34:07

stackdynamic variables is that there is

play34:10

some time spent for the allocation

play34:13

because the allocation doesn't happen

play34:15

statically prior to runtime and

play34:18

therefore the allocation takes up some

play34:20

run time

play34:22

also initialization and de-allocation

play34:26

take up some additional time as well

play34:30

also access to the stack stackdynamic

play34:33

variables is handled by means of

play34:36

indirect addressing so this means that

play34:38

access is slower than it would be for a

play34:42

static variable

play34:44

and then also sub-programs cannot be

play34:46

history sensitive

play34:48

because we need support for static

play34:51

variables in order to create history

play34:53

sensitive variables within sub programs

play34:57

now local variables can also be static

play35:00

in nature and the advantages and

play35:02

disadvantages associated with static

play35:05

local variables are the opposite of

play35:07

those four stack dynamic local variables

play35:14

so let's look at how static and dynamic

play35:17

variables are dealt with in a number of

play35:20

programming languages

play35:21

so in most of the modern day programming

play35:24

languages that exist local variables are

play35:27

stack dynamic

play35:29

in the c based languages local variables

play35:32

are by default stack dynamic however

play35:35

they can be explicitly declared as

play35:38

static with the static keyword in which

play35:40

case they are then static local

play35:43

variables

play35:44

the methods in java python and c-sharp

play35:48

only have stack dynamic local variables

play35:52

and in lua if we have an implicit

play35:55

declaration of a variable then that

play35:58

variable will be global in nature

play36:02

however local variables which must be

play36:04

declared with the special word local

play36:07

explicitly

play36:09

are stack dynamic variables

play36:12

all right so that concludes our

play36:14

discussion for this lecture in the next

play36:16

lecture we will continue our discussion

play36:19

on the chapter we will talk about

play36:21

parameter parsing methods parameters

play36:24

that are sucker programs and calling

play36:27

sub-programs indirectly

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Sub-programsProgramming ConceptsControl StructuresAbstractionParameter PassingFunction DesignRecursion SupportStatic VariablesDynamic VariablesLocal Variable Allocation