Parameters
Procedures allow tasks to be encapsulated for use at another time. Parameters provide a technique for providing inputs to procedures and receiving outputs from them.

Body Mass Computation
The body mass index is used to determine if a person is overweight:
BMI = 4.89weight/height2
where the weight is in pounds, the height is in feet
Converting it to a procedure is straightforward … so volunteer to write it, letting your friend build the GUI

The GUI Built By A Friend

Incompatibility of Names
A problem with names …
Procedure Assumes     Quantity      GUI Assumes
heightIN          height        BMIheight
weightLBS          weight        BMIweight
bodyMass          bmi        BMIndex
Though in this case better communication might have saved this case, the need to associate different names is fundamental – it is essential in making procedures reusable.

Adding Parameters
The body mass problems can be fixed without dieting
Introduce parameters ...
Formal parameters are part of the formal definition
Formal parameters are “declared” in the parenthesized list following the procedure name
To call the procedure, give the actual parameters

Body Mass Index Program

Formal Parameters
The formal parameters are “declared” within the parentheses … the syntax is just like DIM statements
As with other variables, any names can be chosen
Each variable must be given a type: Integer, String, Double
Formal parameter variables are “known” only within the procedure, i.e. they are local to a procedure
They never conflict with variables in the calling context
Different procedures could use the same formal parameter names without confusion or conflict
The technical term for this is “scope”: the scope of the formal parameter is local to the procedure.

Input vs Output
Many programming languages (including VB6) provide several different ways of passing values back and forth between the actual and the formal parameters.
The default in Visual Basic, and the only kind we’ll use in CSE/IMT 100, is pass by reference.
Pass by reference allows information to flow in both directions.
Formal parameters can be used as inputs or outputs or both
Any changes made to a formal parameter will make a change to the corresponding actual parameter.

Actual Parameters
The actual parameters must fulfill these requirements known as the formal/actual correspondence rules
There must be the same number of actual parameters in the call, as there are formal parameters in the proc declaration
The order of the parameters matters --
The 1st actual parameter corresponds to the 1st formal
The 2nd actual parameter corresponds to the 2nd formal
The types of the actuals must match the types of the formals
Any formal used as a procedure output must have a variable as an actual

Review -- Control Flow for Procedures
When we call a procedure, Visual Basic jumps to the code for the procedure.  It runs this code, then returns back to where the procedure was called, and continues on.

x = 5
Call squid()
x = x+1

Private sub squid()
     Print “hi there”
End Sub

Information Flow for Procedures
When we call a procedure, the formal parameter temporarily becomes another name for the actual parameter.
In other words, in Visual Basic the formal parameter temporarily becomes an alias for the actual parameter, for as long as the procedure is executing.
Aliases in real life:
“The Sundance Kid” was an alias for Harry Longabaugh
Two names; one person.

Input parameter example
Remember … the formal parameter becomes an alias for the actual parameter

Output parameter example

Both Input and Output

Expressions as Actual Parameters

Expressions as Parameters -- Caution

Mini-Exercise #1
What does the program print?

Mini-Exercise #1 -- Answer
What does the program print?

Mini-Exercise #2
What does the program print?

Mini-Exercise #2 -- Answer
What does the program print?

Mini-Exercise #3
What does the program print?

Mini-Exercise #3 -- Answer
What does the program print?

Mini-Exercise #4
What does the program print?

Mini-Exercise #4 -- Answer
What does the program print?

Surgeon General’s Warning!
The “Fluency” book uses a different way of explaining parameter passing (as assignment statements into the formal parameters).
For straightforward programs, this always gives the same results as pass by reference.
However, for some messy cases it gives different results.
Ugh!  We’re never going to give you such programs in CSE/IMT 100 (in homework or quizzes).
If you go on to further study of programming, however, you will probably run into this.
The way described in the lecture is how it’s actually done.

Summary
Discussion of parameters for procedures
Parameters link the variables in the calling context with the variables in the procedure context
There is a 1-to-1 relationship between the formal parameters of the procedure definition and the actual parameters of the actual procedure call
The default way of passing parameters in Visual Basic is “pass by reference”.  The formal parameter becomes an alias for the actual parameter.