[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 09
[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]
Thu Apr 26 14:41:42 PDT 2012

Contents


    cs201/09 Functions

      Previous 08 Loops

      [ 08.html ]

      Study pages 159-175

        Most real programs are made up of many functions. All have at least one function. It is called main. It is where the program starts running.
         		int main()
         		{
         			...
         		}
        The "main" function looks just like a general function
         		Type name(parameters)
         		{
         			...
         		}

        The "main" function is special because it is where the program start executing.

        Functions are useful because they stop us typing the same code (with small changes) many times. They help us follow The DRY Principle (Don't Repeat Yourself).

        THey are vital when you wish to share some code with other people -- either because you are working on the same project together, or because you want to sell or give away your code.

        4.1 Functions as Black Boxes

        Functions get data from their parameters and most return a single value as a result. Some do things and don't return a value at all.

        A function always needs to be tested by a different function to see if it produces the right result.

        4.2 Implementing Functions

        [ futval.cpp ]

        Syntax 4.1 Function definition

      1. function_defition::= returned_type function_name "(" parameters ")".
      2. parameters::= parameter #("," parameter).
      3. parameter::= type name.
         	double abs(double x)
         	{
         		if( x >= 0) return x;
         		else return -x;
         	}

        Example program [ 09multi.cpp ] containing a function to calculate the hypotenuse of a right triangle.

        Productivity Hint 4.1 Write functions with reuse in mind

        This means documenting (in the code) what the function does.

        Refactoring with functions -- DRY

        The DRY principle -- "Don't Repeat Yourself" -- often leads to discovering a function.

        When you get a program working -- check to see if there are any repeated pieces of code. You can then put the code inside a function and give it a meaningful name. Then replace the repeated pieces by calls to the new function and test.

        4.3 Function Comments

        The book's comments are inspired by the "javadoc" tool used with Java. In C++ and in this class you can simplify them. Use this template [ function.cpp ] in this class.

        Productivity Hint 4.2 Global Search and Replace

        A very useful technique. Possible in gedit and vi.

        Productivity Hint 4.3 Regular Expressions

        The text has the right idea but the examples don't work as shown on pages 168 and 169. They should be typed like this
         		egrep "[0-9]+" homework.cpp
         		egrep "[^A-Za-z][0-9]+" homework.cpp
        (The "+" is understood by "egrep" but not "grep")

        4.4 Return Values

        Syntax 4.2 return Statement

      4. return_statement::= "return" expression ";".

        Notice that the semicolon ";" is an essential part of the return statement.

        Common error 4.1 Missing Return Value

        4.5 Parameters

        Functions are given data in parameters and can use it to calculate results.

        Quality Tip 4.1 Use meaningful Names for parameters

        You should use the language of your clients to supply names for varaible whenever possible.

        So if you are writing code for mathematicians and scientists then you can use tradition (short) varaibles. You can also spell out greek letters.

        But if you are writing code that is for an accountant then use accounting words...

        Common Error 4.2 Type Mismatch

        Advanced Topic 4.1 Function Declarations

        We will need this section when we start multifile and object oriented programming.

      Review Questions fo class 09

      R4.1, 4.2, 4.3,4.4, 4.5, 4.6.

      Exercises on simple functions

      [ fun0.cpp ]

      [ fun.cpp ] (use "void" functions that return nothing).

      [ fun2.cpp ]

      [ circ.cpp ]

      [ funfun.cpp ]

      Understanding code with Functions and loops

      What does this do? [ wallpaper.cpp ]

      Project 4 Due Next time

      Quiz 4 next time

      Lab 5 on advanced loops

      [ lab05/ ]

      Next 10 -- more on functions

      [ 10.html ]

    . . . . . . . . . ( end of section cs201/09 Functions) <<Contents | End>>

    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".

End