[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] >> [CSci201] >> [Lab01] >> index
[Index] [Schedule] [Syllabi] [Glossary] [Labs] [Projects] [Resources] [Grading] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Tue Feb 5 13:55:44 PST 2008

Contents


    CSCI201/lab06 functions

      Goal

      To help you deepen your understanding of how functions work and how to use them.

      Process

      Study, compile, run,.... some programs that demonstrate various techniques:
      1. Functions inside functions
      2. Functions with parameters that return a value
      3. Functions with string parameters
      4. Functions with reference parameters
      5. A broken function with BUGS,

      The last of these will take most time, but the earlier ones may help you to fix the bugs.

      You can compile all the example programs with simple commands like:

       		g++ -o p    p.cpp
      and run like this
       		./p
      where p.cpp is the name of a program.

      A program with functions called inside other functions

      Down load [ fun1.cpp ] and compile and run it.

      One function is "instrumented" to show you what is happening. It outputs something when it starts and something else when it exits/returns.

      Do something similar to all the other functions so that we can get a complete listing of every function. Treat Main as another function as well.

      Compile test and run and learn how control is passed down into a function and then comes out again...

      Functions with parameters and return values

      Download [ fun3.cpp ] and look at it. Work out on a sheet of paper what it will do if you compile and run it.

      Then compile and run it. Were you right or has something unexpected happened? What can you learn from this?

      Functions with string parameters...

      The following example shows (1) strings are OK as parameters, (2) how to make the instrumented outputs show the nesting of the function calls: [ fun2.cpp ]

      Can you explain why the strings move to the right with each level of calling?

      Functions with reference parameters

      Here we use pass-by-reference to change a parameter: [ ref.cpp ]

      Can you predict what will happen?

      Try out the experiments suggested in the comments in the code.

      Debug a bad function

      Here [ bad.cpp ] is a deliberately broken function embedded in a test program. Don't change the main program, but fix the errors in the code.

      Credit

      Show the lab teacher the changes you have made and talk about what you have learned.

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. TBA::="To Be Announced", something I have to do.
  3. TBD::="To Be Done", something you have to do.

End