Home >  T.A >  cs10 ( Winter 2002 ) >  Lab 8

  CS10 Sec#25, # 29 : Topics covererd in Lab #8
T.A: Demetris Zeinalipour
(Winter 2002)
Print this Page   
 [  Call By Reference  ::   Call By Value  ::   Formatting Output  ::   Exercise #8 ]



Attention:
1) Please come on time and bring a #2 Pencil with You, since you 'll be asked to fill out the TA evaluation scantron.
2) You will receive your mid-terms along with the grade.
3) I will go through the solutions of the midterm and explain any points that you haven't understand.

Top


Call By Value

When an argument is passed by value, a copy of the argument's value is created and passed to the called function

Advantage:
There are no security risks for the data that is passed to a function. The function of course may corrupt the value of the passed value but the initial value won't be affected.

Disadvantage:
  • If a large data item is being passed (e.g a large string), copying that data can take a considerable amount of time and memory.


  • Example : (Lab assignments 6, 8)

    /* This function calculates the other angle with a call-by-value */
    double calculateAngle (double angle1)
    {
       return (90-angle1);
    }
    Top


    Call By Reference

    Advantages:
  • When we want to retrieve MORE than 1 parameters from a function the parameters are passed by reference, modified within the function and there is no need to return these values since there already modified.
  • It is good for performance reasons, because it eliminates the overhead of copying large amounts of data.


  • Disadvantages:
  • It is bad for security reaasons since the called function can actually corrupt the caller's data.


  • Example :

    /* This function calculates in a WRONG way the other angle call-by-reference */
    double calculateAngle (double &angle1)
    {
       angle1 = 14; /* change passed value - corrupt it!*/
       return (90-angle1);
    }

    View a full example that uses parameters by reference
    Top


    Formatting Output

    #include <iomanip>
    In order to print a floating point value with lets say 2 decimal point precision you have to make use of setiosflags.

    Example:
    cout << setprecision(2) << setiosflags(ios::showpoint | ios::fixed) << 201.5;

    RESULT: ==> 201.50

    Top


    Lab 8 Assignment - Exercise
    Download lab8.exe (win32) here and see how your program should work to various entries.
    See Solution - C++ solution here (will be available on Saturday since labs are running until friday)


    Write a function that finds and returns the distance between two points. The functions should take as parameters the x and y coordinates of point1 and the x and y coordinates of point2. The coordinates should be floating-point values. The following is the formula to find the distance between two points:

    Now write a program that allows the user to find the distance between as many points as they want. Output each distance as a floating-point value showing 3 digits to the right of the decimal.

    Advise : Remember to use good passing techniques for each parameter. If something needs to be changed or calculated, then it should be passed by reference. If something needs to simply be looked at, then it should be passed by value.

    Notice: Be sure to put all your information (login, lab section, …) at the top of your program. Your lab programs DO NOT get turned in electronically. I will come around and check you off individually.

    Top