[ 08.html ]
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.
R4.1, 4.2, 4.3,4.4, 4.5, 4.6.
[ fun0.cpp ]
The "main" function looks just like a general function
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.
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.
[ futval.cpp ]
- function_defition::= returned_type function_name "(" parameters ")".
- parameters::= parameter #("," parameter).
- parameter::= type name.
double abs(double x)
if( x >= 0) return x;
else return -x;
[ 09multi.cpp ]
containing a function to calculate the hypotenuse of a right triangle.
This means documenting (in the code) what the function does.
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
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.
A very useful technique. Possible in gedit and vi.
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")
- return_statement::= "return" expression ";".
Notice that the semicolon ";" is an essential part of the return statement.
Functions are given data in parameters and can use it to calculate results.
You should use the language of your clients to supply names for varaible
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...
We will need this section when we start multifile and object oriented programming.
[ fun.cpp ]
(use "void" functions that return nothing).
[ fun2.cpp ]
[ circ.cpp ]
[ funfun.cpp ]
What does this do?
[ wallpaper.cpp ]
[ lab05/ ]
[ 10.html ]
. . . . . . . . . ( end of section cs201/09 Functions) <<Contents | End>>
- Algorithm::=A precise description of a series of steps to attain a goal,
[ Algorithm ]
- Class::=A description of a type of object that includes the data it knows and the functions it can execute.
- Function::programming=A selfcontained and named piece of program that knows how to do something.
- Gnu::="Gnu's Not Unix", a long running open source project that supplies a
very popular and free C++ compiler.
- OOP::="Object-Oriented Programming",
Current paradigm for programming.
- Semantics::=Rules determining the meaning of correct statements in a language.
- SP::="Structured Programming",
a previous paradigm for programming.
- Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
- Q::software="A program I wrote to make software easier to develop",
- TBA::="To Be Announced", something I should do.
- TBD::="To Be Done", something you have to do.
- UML::="Unified Modeling Language", industry standard design and documentation diagrams.
- void::C++Keyword="Indicates a function that has no return".