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

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



Attention:
1) if you haven't receive your Mid-Term back go and see Pr. Kelsey.
2) You can also request your midterm scantron from her.

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
  • Review the previous lab's assignment solution
  • Top


    Lab 9 Assignment - Exercise
    Download lab9.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 program that will generate random test questions (for those studying addition).
  • Write a function that will randomly generate two operands ranging between 1-1000. This function should return through parameters the two operands.
  • Write a function that takes as parameters, the two operands and will output the question nicely to the screen. Each question should line up with future outputted questions at the addition operator and the equal sign.
  • Write a function that will take as a parameter an integer representing some number of questions and generate that many test questions.
  • Write a program that will allow the user to specify some number of questions and then will show that many addition questions nicely on the screen. For example if the user wanted 4 questions, your program might print:

    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.
    If you think that you have missed some point regarding functions ask me to go over them again since they are EXTREMELY important as you might already realized

    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