Home >  T.A >  cs10 ( Fall 2001 ) >  Lab 9

  CS10 Sec#24 : Topics covererd in Lab #9
T.A: Demetris Zeinalipour
(Fall 2001)

Print this Page   
 [  Call By Reference  ::   Call By Value  ::   Exercise #9 ]



Attention:
1) Please check your email to see your grades. If you can't remember how to access your email account please visit Lab 1 for instructions. If you still have a problem I will help on the next lab to work this out. Please have in mind that any problems regarding grades needs to be addressed with Professor Lick before the last class lecture.
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);
    }
    Top


    Lab 9 Assignment - Exercise
    New! - Download lab9.exe (win32) here and see how your program should correspond to various inputs.
    Download triangle.txt (save it in same directory as the program)
    See Solution - C++ solution here (will be available on Saturday since labs are running until friday)


    Write a program that uses your program from Lab Assignment 6.

    Write a function that takes as parameters 3 sides and 2 angles. One side and one angle should already be known and have values, the other sides and angle should be calculated in this fu nction. Since you will need to be calculating more than one piece of information, you will be returning the information through reference parameters (instead of a return statement).

    Write a function that will take as parameters 3 sides and 2 angles and will print a picture of a triangle and output the value of each side and angle pictorially (note, no need to be to scale, just output the value of the side near a side).

    Now write a program to use these two functions. From a file called triangle.txt read the length of one side and the amount of one angle. Then use the first function to calculate the remaining information. Using the results of the first function, call the second function to output the triangle pictorially.



    Before running your program, you will need to create this file called triangle.txt. Follow the directions below:

    1. Start Microsoft Word
    2. Type in a length of a side and the amount of an angle (separated by whitespace).
    3. Go to "File", then "Save".
    4. For the "Save as Type", choose Text Only.
    5. For the "File Name", type triangle.
    6. Be sure to make it save in the folder in your account that contains your .cpp file.
    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