Chapter 4.3 – Parameters in java | Introduction to Programming Using Java

Chapter 4.3 – Parameters in java | Introduction to Programming Using Java

 

4.3 Parameters in java

 

If a subroutine is a black box, then a parameter provides a mechanism for passing information from the outside world into the box. Parameters are part of the interface of a subroutine. They allow you to customize the behavior of a subroutine to adapt it to a particular situation.

As an analogy, consider a thermostat—a black box whose task it is to keep your house at a certain temperature. The thermostat has a parameter, namely the dial that is used to set the desired temperature. The thermostat always performs the same task: maintaining a constant temperature. However, the exact task that it performs—that is, which temperature it maintains—is customized by the setting on its dial.

 

Chapter 4.3 - Parameters in java | Introduction to Programming Using Java

 

4.3.1 Using Parameters

As an example, let’s go back to the “3N+1” problem that was discussed in Subsection 3.2.2. (Recall that a 3N+1 sequence is computed according to the rule, “if N is odd, multiply by 3 and add 1; if N is even, divide by 2; continue until N is equal to 1.” For example, starting from N=3 we get the sequence: 3, 10, 5, 16, 8, 4, 2, 1.) Suppose that we want to write a subroutine to print out such sequences. The subroutine will always perform the same task: Print out a 3N+1 sequence.

But the exact sequence it prints out depends on the starting value of N. So, the starting value of N would be a parameter to the subroutine. The subroutine could be written like this:

 

Chapter 4.3 - Parameters in java | Introduction to Programming Using Java

Chapter 4.3 - Parameters in java | Introduction to Programming Using Java

 

The parameter list of this subroutine, “(int startingValue)”, specifies that the subroutine has one parameter, of type int. Within the body of the subroutine, the parameter name can be used in the same way as a variable name. However, the parameter gets its initial value from outside the subroutine. When the subroutine is called, a value must be provided for this parameter in the subroutine call statement. This value will be assigned to the parameter, startingValue, before the body of the subroutine is executed.

For example, the subroutine could be called using the subroutine call statement “print3NSequence(17);”. When the computer executes this statement, the computer assigns the value 17 to startingValue and then executes the statements in the subroutine. This prints the 3N+1 sequence starting from 17. If K is a variable of type int, then when the computer executes the subroutine call statement “print3NSequence(K);”, it will take the value of the variable K, assign that value to startingValue, and execute the body of the subroutine.

The class that contains print3NSequence can contain a main() routine (or other subroutines) that call print3NSequence. For example, here is a main() program that prints out 3N+1 sequences for various starting values specified by the user:

 

Chapter 4.3 - Parameters in java | Introduction to Programming Using Java

 

Remember that before you can use this program, the definitions of main and of print3NSequence must both be wrapped inside a class definition.

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java

Chapter 3.6.4 – Definite Assignment | Introduction to Programming Using Java

Chapter 3.7.2- try..catch | Introduction to Programming Using Java

 

13 thoughts on “Chapter 4.3 – Parameters in java | Introduction to Programming Using Java”

Leave a Comment