PASSING ARGUMENTS


Arguments provide a means for passing data to functions so that it can be processed or control the function's internal logic. After the arguments are passed to a function, they are mapped to parameters that are defined as part of the function's declaration, as demonstrated here:

 private:Void ShowAge( Int32 intAge ) {   MessageBox::Show( intAge.ToString() ); } 

Trick 

You can set up your functions to process as many arguments as you choose to pass to them, as long as you separate each corresponding argument definition with a comma.

In this example, a function named ShowAge is defined. The function defines a single variable, a parameter named intAge with a data type of Int32. The variable, or function parameter, is then used in the MessageBox::Show method to a value of intAge.

Hint 

A parameter is a declaration within a function that defines variables to be used to store copies of values passed to them as arguments.

Outside of the function, you can use any Int32 as an argument when calling the function. For example, you could invoke the function using any of these means:

 Int32 intCurrentAge = 20; ShowAge(intCurrentAge); ShowAge( 120 ); 

When a function accepts data in the form of a parameter, you are said to be passing by value. This means you are free to make changes to the variable within the function without those changes being reflected outside the function. For example, if you pass intCurrentAge in the previous example as a parameter, you cannot change it within the function.

You can pass as many arguments as required to a function, as long as you separate each argument with a comma when calling the function, as demonstrated here:

 DisplayVitals( intAge, intWeight, intHeightInInches ); 

To better understand this, let's create a short program example:

  1. Start by launching Visual C++ and creating a new project.

  2. Add four labels and three text boxes to the form. Label the first TextBox control txtAge, the second txtWeight, and the last txtHeight.

  3. Add a button to the form. Change its text to Submit.

  4. Name and arrange all elements, as illustrated in Figure 8.8.

image from book
Figure 8.8: Entering data into the Vitals application.

Next, double-click on the button and add the following code:

 private: System::Void button1_Click(System::Object^  sender,\ System::EventArgs^  e) {   Boolean blnInputValid = false;   //Declare variables to hold vitals   Double dblAge,           dblWeight,           dblHeight;   //Confirm proper input   blnInputValid = Double::TryParse( txtAge->Text, dblAge );   if( blnInputValid == false )     return;   blnInputValid = Double::TryParse( txtWeight->Text, dblWeight );   if( blnInputValid == false )     return;   blnInputValid = Double::TryParse( txtHeight->Text, dblHeight );   //If input valid, show vitals   if( blnInputValid == true )     ShowVitals( dblAge, dblWeight, dblHeight ); } 

Below this function, add the following code:

 private: Void ShowVitals( Double dblVital Age, \           Double dblVitalWeight,  \           Double dblVitalHeightInInches ) {   MessageBox::Show( String::Concat( "Your age is ",   dblVitalAge.ToString(), ". Your weight is ",   dblVitalWeight.ToString(), ". Your height is ",   dblVitalHeightInInches.ToString(), " inches." ) ); } 

Press F5 to run your program. After you enter the required information, select the Submit button to see the information displayed. The program works by first validating user input in the button1_Click function. If the user input proves acceptable, the function then calls ShowVitals, passing it three arguments. Within the ShowVitals function, the three arguments are received as parameters. The parameters are concatenated using the String::Concat method and then displayed using the MessageBox::Show method. Figure 8.9 illustrates the results.

image from book
Figure 8.9: A function within the Vitals application shows the information passed to it.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net