// Zig Herzog
// Oct. 5 , 2008
//
// Example for a user supplied function ( vectorLength ) with single
// return value.
// Three items are needed :
// 1) PROTOTYPE appaering before main()
// 2) call to function with actual arguments
// 3) definition of function and formal arguments
//
// NOTES
// 1) Number and types of formal arguments are user's choice but they have
// to be matched by number of types of the actual arguments.
// 2) the type of the return values is user's choice but has to
// be matched.
// 3) The main program got a return-statement. use "echo $status" under csh
#include <iostream>
#include <cmath> // needed for all sqrt-function
using namespace std;
double vectorLength ( double , double , double ) ; // PROTOTYPE
int main ( )
{
double size1 , xComponent1 , yComponent1 , zComponent1 ;
double size2 , xComponent2 , yComponent2 , zComponent2 ;
cout << "Give x, y, and z-component of vector 1 (separated by space) " ;
cin >> xComponent1 >> yComponent1 >> zComponent1 ;
// 1. use of function vectorLength to determine the magnitude
// of the vector 1 and assigning its value to the variable size1
size1 = vectorLength ( xComponent1 , yComponent1 , zComponent1 ) ;
cout << "Length of vector 1 = " << size1 << endl ;
cout << "Give x, y, and z-component of vector 2 (separated by space) " ;
cin >> xComponent2 >> yComponent2 >> zComponent2 ;
// 2. use of function vectorLength to determine the magnitude
// of the vector 2 and assigning its value to the variable size2
size2 = vectorLength ( xComponent2 , yComponent2 , zComponent2 ) ;
cout << "Length of vector 2 = " << size2 << endl ;
return ( 12 ) ;
}
/////////////////////////////////////////////////////////////////////////
//
// Function vectorLength ()
// Returns ( type double ) the length of a vector given it's
// x, y, and z components ( type double ).
//
/////////////////////////////////////////////////////////////////////////
double vectorLength ( double x , double y , double z )
{
double r ; // Declaration of local variable
r = sqrt ( x*x + y*y + z*z ) ;
return r ;
}
Zig Herzog; hgn@psu.edu