Function Calls: A Handy Trick


Function Calls: A Handy trick

The goal for this exercise is to understand a very useful, and very common, shorthand for calling methods.

Examine the following code. Notice that it 'feeds' the result of the

Console.ReadLine command directly to the Int32.Parse command, without storing it into a local variable. Technically, the computer must store the data temporarily, but in this situation we do not give that temporary data storage an actual name (i.e., we do not give it a local variable name).

This is a useful trick, in order to save lines of code -- you may see this used in the code you find on the Internet, or that the instructor uses in class. class Example

{ public void RunExercise()

{ int x = 8, y = 2;

Console .WriteLine( "input an integer value for x" );

x = Int32 .Parse( Console .ReadLine());

Console .WriteLine( "input an integer value for y" );

y = Int32 .Parse( Console .ReadLine());

Console .WriteLine( " x + y is: " + (x + y));



What you need to do for this exercise: For this exercise, you should try to do something similar with the Double.Parse command

– create a new class (name it

Function_Call_Trick ), and in that class create a method named RunExericse

( public void RunExercise() ) that will ask the user to input any real value for X, and then for Y, and for each variable, translate what the user gave you into a double without first storing it in a local variable. (I.e., notice that in the above code, there is no string local variable to store what the Console.ReadLine returned).