[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 10
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] <10> [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Mon Apr 29 14:21:50 PDT 2013

Contents


    cs201/10 -- More on Functions

      Previous 09 Functions

      [ 09.html ]

      Project 4 Due

      [ projects.html#P4 ]

      Study pages 175-185

        4.6 Side Effects

        4.7 Procedures

        Example on page 177 [ printtime.cpp ] (needs ccc_time -- use Q to compile)

        Functions declared with a non-void type (like "int") return a value. They are used in expressions. But void functions return no value. They are used like statements.

        Syntax of void functions

      1. void_function_definition::= "void" name"(" parameters ")" "{" body "}"
         		void newline() { cout << "\n"; }

      2. void_return::= "return" ";". Do not include an expression. If there is no return in a procedure then the compiler adds one at the end of the body of the procedure.

      3. void_function_call::=name "(" actual_parameters ")" ";".
      4. statement::= assignment | void_function_call | ... .

        4.8 Reference Parameters

        Example from page 179-180 [ raisesal.cpp ]

        Note -- a reference parameter is replaced only by a variable when the function is called.

        A Common Error with Parameters

        If you want a function to change a actual parameter's value then it must be called by reference.

        Forgetting the "&" in a reference parameter leads to nasty little logic errors. The compiler will not help you. Only knowing the syntax and the semantics can help you.

        Syntax 4.4 Reference Parameter

      5. reference_parameter::= type "&" parameter.

        The actual parameter for a reference must be a variable of the right type.

        Advanced Topic 4.2 Const references

        Later.

        Random Fact 4.1 Explosive Growth of personal computation

        Interesting but not tested in this class... Visicalc was a breakthrough -- Computation for accountants.

        4.9 Variable Scope and Global Variables

        Example on pages 184-185 [ global.cpp ]

        Quality Tip 4.2 Minimize Global Variables

        Just say no! Or points will be lost and bugs created.

        Global Constants are a good thing

        Many libraries define useful global constants as well as functions and procedures. Example -- RAND_MAX for rand(). You can and should define global const for mathematical constants like π.

      Review Questions

      R4.7, 4.8, 4.9, 4.10

      Exercises -- What do they do

      [ 10ex0.cpp ] [ 10ex.cpp ] [ 10ref.cpp ] [ sideways.cpp ] [ fun14.cpp ]

      Quiz 4 on if-then-else etc

      Includes functions, booleans, and even some strings.

      Lab 6 next time on functions

      Next 11 Case study

      [ 11.html ]

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Class::=A description of a type of object that includes the data it knows and the functions it can execute.
  3. Function::programming=A selfcontained and named piece of program that knows how to do something.
  4. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  5. OOP::="Object-Oriented Programming", Current paradigm for programming.
  6. Semantics::=Rules determining the meaning of correct statements in a language.
  7. SP::="Structured Programming", a previous paradigm for programming.
  8. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  9. Q::software="A program I wrote to make software easier to develop",
  10. TBA::="To Be Announced", something I should do.
  11. TBD::="To Be Done", something you have to do.
  12. UML::="Unified Modeling Language", industry standard design and documentation diagrams.
  13. void::C++Keyword="Indicates a function that has no return value".

( End of document ) <<Contents | Top