# Class 12: Variable-Arity Procedures

Back to Local Procedures. On to Strings.

Held: Friday, 7 February 2003

Summary: Today we consider how and why to write procedures that can take different numbers of arguments in different situations.

Related Pages:

Assignments:

Notes:

• Sorry about missing Wednesday's class. I was home with a sick child.
• Are there any questions on Laboratory Writeup 2?
• Expect exam 1 (take-home) a week from today.

Overview:

• Definition of arity.
• Why have variable-arity procedures.
• How to write variable-arity procedures.
• Lab.

## Some Terminology

• Definition: Parameter: A value passed to a procedure.
• Formal parameter: A parameter as it appears in the procedure definition.
• Actual parameter: A parameter as it appears in the procedure call.
• Def: Arity: The number of actual parameters a procedure expects.
• Common variants:
• A unary procedure takes one actual parameter.
• A binary procedure takes two actual parameters.
• A variable-arity procedure can take varying numbers of actual parameters.

## Variable-Arity Procedures

• Some procedures naturally take varying numbers of parameters:
• + takes 0 or more parameters.
• display takes 1 or 2 parameters.
• map takes 2 or more parameters.
• It would be nice to be able to write our own procedures of variable arity (that is, varying numbers of parameters).
• Zero or more parameters:
(lambda params
body)

• params is not parenthesized.
• In your body, treat params as a list.
• One or more parameters:
(lambda (first . remaining)
body)
+ In your body, treat remaining as a list.

• Two or more parameters:
(lambda (first second . remaining)
body)
+ In your body, treat remaining as a list.

• Three or more parameters:
• I bet you can figure it out.

## History

Thursday, 15 January 2003 [Samuel A. Rebelsky]

• Created as a mostly-blank outline.

Friday, 7 February 2003 [Samuel A. Rebelsky]

• Filled in the details. Most of the discussion of variable-arity procedures was taken from past classes. The section on terminology is new.

Back to Local Procedures. On to Strings.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:20:26 2003.
The source to the document was last modified on Fri Feb 7 06:33:12 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.12.html.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu