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

  CS10 Sec#24, #29 #30 : Topics covered in Lab #8
T.A: Demetris Zeinalipour
(Spring 2002)
Print this Page   
 [  Call By Reference  ::   Call By Value  ::   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 over 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


    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)
    Download numbers.txt (save it in same directory as the program)

    Write a program that uses your program from Lab 7, making the following changes.

    Write a function that takes an integer and an ostream and prints out the value and the digits to the specified ostream. (You will move much of your code from lab 7 into this function). See below for help.

    Now your program should read integers from a user-specified file and use the above function to print out the digits to a specific location. The user should be able to choose to print to the screen (cout) or to a file. If they choose to print to a file, they should be able to specify the name of the file to print to.

    HELP: Printing to a file uses an ofstream, cout is an ostream, thus we can use a function to print to a parameter specified ostream and have it print to the screen or a file (since an ofstream is an ostream). When you pass an ostream as a parameter to a file, it should be passed by reference, so that the information and location of where we are at in the file will remain after coming back from the file. The following function is an example of printing a character to a specified location:

    void PrintChar(char charvalue, ostream &os)
    {
       os << charvalue << endl;
    }

    This function can be called in the following ways:
    PrintChar('s', cout);
    and assuming that outfile is an ofstream that has already been declared and opened:
    PrintChar('s', outfile);


    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