CS 214 Lab 6: Subprograms and Parameter Passing


Today's exercise involves studying the syntax of the construct that provides the subprogram abstraction in each of our four languages. Along the way, we will see how parameter passing is handled in each of the languages.

Many programming languages, especially those from the Algol family, make a distinction between two different kinds of subprograms:

As in past weeks, we will solve the same problem in each of our four languages, to compare their subprogram constructs.

This week's problem is this: given a string named aString, and an integer named pos, write a function that computes the two substrings formed by splitting aString about pos.

To illustrate, suppose that in our language, the index of the first character is zero, and that aString is hello. If pos is 3, then our function should compute hel and lo as the two substrings. If pos is 0, then our function should compute "" (the empty string) and hello as the two substrings. In other words, the character at index pos should always be the first character in the second substring.

Since a subprogram that solves this problem must send two substrings back to is caller, our investigation is to examine what mechanism each language provides such problems.

As before, we will provide program "skeletons" that perform much of the work, leaving you free to concentrate on the subprogram being constructed. Each of these skeletons provides a partial "program" to solve this problem, using an implementation of the following simple algorithm:

   1. Get aString and pos.
   2. Call split() to partition aString about pos into part1 and part2).
   3. Display part1 and part2.
To perform step 2 of this algorithm, our "program" will need to use a subprogram that can compute and return (or pass back) two values. The specification of this subprogram is thus:
  Receive: aString, a string,
           pos, an integer.
  Passback: part1, the substring of aString prior to pos,
            part2, the substring of aString following pos.
Our exercise will be to implement such a subprogram in each of our languages.

As usual, we will lead you through the process using Java, after which you will apply similar techiques to solve the problem in the other three languages. As usual, the order in which you do the three exercises does not matter. If you find the system to be slow in a particular section, save your work and switch to another section, and the "slow" section again later. Begin by making a new directory for this lab exercise and changing directory to that new directory.

  1. Java Introduction
  2. Ada Exercise
  3. Clojure Exercise
  4. Ruby Exercise

Turn in. Each of the four parts requires you to create files containing source code and execution traces. When you have completed all four parts, use cat to create a single file that contains all of your results:

   cat script.java script.ada script.clojure script.ruby > lab06-results
Then submit your work by copying that single file into your personal folder in /home/cs/214/current/:
   cp lab06-results /home/cs/214/current/yourUserName
replacing yourUserName with your login name. The grader will access and grade your results from there.


Calvin > CS > 214 > Labs > 06
This page maintained by Joel Adams.