CS 403 Class Notes - Spring 2002: Subprograms, Parameters, Defaults, Overloading, Study notes of Programming Languages

A set of class notes from the spring 2002 cs 403 course. The notes cover topics such as subprograms, parameter matching, default parameters, and overloading. The two major types of subprograms: procedures and functions, and the design issues related to them, including parameter matching, parameter passing methods, and semantics. The notes also cover the concept of default parameters and their implications for parameter matching.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-efw
koofers-user-efw 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Spring 2002 CS 403 Class Notes Page 1
Lecture 21 - Thursday, March 20
Today
Subprograms
Reading Assignments For Next Class
Pp. 288-291, 353-54, 363-64, 382-84
Assignments
Assignment #5 due Tuesday March 25th
Exams:
Exam 2: Thursday, April 3rd
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS 403 Class Notes - Spring 2002: Subprograms, Parameters, Defaults, Overloading and more Study notes Programming Languages in PDF only on Docsity!

Lecture 21 - Thursday, March 20 • Today

  • Subprograms
    • Reading Assignments For Next Class
      • Pp. 288-291, 353-54, 363-64, 382-
        • Assignments
          • Assignment #5 due Tuesday March 25

th

  • Exams:
    • Exam 2: Thursday, April 3

rd

Subprograms – Basic Issues •^

Two major types:– Procedures: No return values– Functions: Have return values– Observation: No procedures in C – everything is a function

-^

Design issues:– Already addressed issues related to local variable

allocation, scope, etc.

  • How are parameters matched?– How are parameter values transmitted?– Is overloading permitted?

Default Parameters •^

Giving a parameter a default value:– int foo(int x = 0);– foo() means that x is initialized to 0.– Parameters are resolved positionally in this case.

-^

What about:– int foo(int x, int y = 0, char c = ‘ ‘);– foo(1,2,’c’); foo(1,2); foo(1) all ok.– foo(1,’c’) is a problem.

-^

What about the following:– int foo(int x, int y = 0, char c)– This won’t compile – makes no sense if resolved positionally

-^

L to R matching means that there must be 3 parameters regardless tomatch with the right one in this case

-^

Keyword parameters would be better in the context of defaultparameters (but not available in C++)

Parameter Passing: Methods vs. Semantics • Methods:

  • Call by value: Pass the actual value– Call by reference: Pass the address of the actual

value

  • Semantics:
    • IN only: Changes to the formal parameter not

reflected in the caller

  • IN OUT: Changes to the formal parameter are

reflected in the caller

  • OUT only (not typically used)

Some Tradeoffs •^

Copy:– Can be expensive for large structures (e.g., arrays)– Best fit with IN semantics

-^

Reference:– Cheap in most cases– Best fit with IN OUT semantics

-^

What if you want IN semantics but without theexpense?– Implement compiler checking scheme to prevent changes

to parameters

  • Let the programmer optionally control this:
    • const with ref parameters achieves IN semantics by reference

Copy vs. Reference Gives Different Results on IN OUTParameters int x;void foo(IN-OUT int y){

x++;y++; } void main(void){

x = 10;foo(x);cout << x << endl; } •^

If IN-OUT implemented by copy, then output is 11.

-^

If IN-OUT implemented by reference, then output is 12.

IN OUT Semantics: Final Comment • IN OUT represents a very procedural way to

view the world

  • A more functional way of viewing the world

would disallow IN OUT entirely– Mathematical functions (e.g., f(x)) don’t change

their parameters

  • OO paradigm would take a similar view:
    • Methods change the objects in their class but not

other parameters

  • Java takes this view and disallows IN OUT

parameters entirely

Overloading •^

Reusing the same function name for multiplefunctions

-^

Functions must have different signatures (i.e.,parameter lists)

-^

Example:– foo(int a, int b, int c)– foo(float x, float y)

-^

Functions disambiguated based on parameter lists