g++ -o example example.cpp -lm(l=link library, m=math).
If you use 'Q' to do your comiplations it will do this for you.
6.4 Function Definitions with Multiple Parameters
6.5 Function Prototypes and Argument Coercion
6.6 C++ Standard Library Header Files
. . . . . . . . . ( end of section Prepare) <<Contents | End>>
Deliver a question and P7
Don't for P7 -- to get full credit it must be a program (even with errors)
that includes a class based on your vision (even if not useful today).
Can you change the value returned by a function
If you store the returned value in a variable,
value = function (data);then you can do anything you want with it.
function ( actual1, actual2, ...)The actual parameters can be expressions (normally...). They must give values of the right type to fit the formal parameters.
The function definition has the formal parameters
type function (type formal1, type formal2, ...){...}
Formal parameters are variables. They are never constants or expressions. They are
place holders for the actual parameters.
Why must I type double twice in function parameters
In normal decalarations you may write
double x,y;and get two doubles. But in a function header
type foo(double x, y)is an error. I don't know why. But it is an error.
By the way.... it is better style to have one variable per declaration as well!
How do formal and actual prameters get matched up
The formal and actual parameters are connected up one-to-one by the compiler. Each actual will be
evaluated and passed to be the initial value of the formal parameter. The formal parameter (normally)
then acts as a local varaible.
Do the parameter types have to match precisely
The compiler can convert numeric data as part of the matching process.
This is called
coercion
because it is a forced conversion or cast.
Thus the call
foo(1)will match all of the following
type foo(char c)...
type foo(int i)...
type foo(long l)...
type foo(double x)...I'm not sure about the rules when there are many matching parameters!
If the actual is a string and the formal paramer is a string is it converted properly
No.
Suppose we have
void foo(int n){ cout << n+1 <<"\n"; }
then
foo (string("124"));
will not compile.
More confusing is
foo ( "124" );which prints one more than the address where "124" has been stored, believe it or not. This is a historical accident.
foo( 123, 456, 789);and
void foo( int x);how would you match the 3 actuals to one formal? And do you have the same idea as everybody else?
Can a function change its formal parameter
Yes.
Can a function change the value of an actual parameter
Only under special circumstance do the actions inside a function have any effect on
the actual parameters given to it in a call. The rules are
As a result you write
y = sin(x);not (Java):
y = Math.sin(x);I think this is convenient!
As a rule, classes of objects are created in libraries when there is a clear consensus on an abstract type of data and how it behaves. Strings are an example, streams are an example. Later: vectors.
As a rule, you should create classes for your project when they mirror real world object. In the ATM example: ATM, Account, Withdrawal, ... and so on. In the problem of grading: GradeBook...
Does any C++ library have secant cosecant cotangent
No. You have to do the division your self. You can always define your own function
if you want:
inline double sec(double x) { return 1.0/cos(x); }
Does the STL vary
They are the same for each ditor you use. They may change from machine to machine, and
will change operating system to operating system. They als change form time to time -- irritating but true.
However they all tend to stay close to the standard... and that only changes ever 11 years or so.
Is there a library that has a function that will find the minimum or maximum of an unknown number of numbers
Not like this:
min( a1, a2, a3, a4, a5)
However if the values are placed, or exist in a container then there is are functions in the STL which will search the container for the minimum and maximum elements:
min_element( vector.first(), vector.last() )
max_element( vector.first(), vector.last() )These are not in CS201.
How can I find out what a function like ldexp is used and what it does
I've found the following source
[ ldexp.html ]
for that function with links for many others.
Explain function overloading
You can give the same name to different functions as long as their parameters are of
different types. When the compiler matches a function call with a function declaration it
looks at the name of the function and the types of the actual parameters. It looks for
a function with the same name and formal parameters of the same type.
It is rather like the way that words in natural languages have different meanings depending on the context... TBA
Only do this if the functions have similar purposes.
Are the things in libraries global and what does this mean
They are either global or private (hidden and unusable). Global things can be used any where
in the program. Here are two example global variables from <iostream>
<cmath> defines global functions, <string> defines global operations and the class string, etc.
Why do I have to write a constructor
Because it initializes variables. Uninitialized variables are bugs waiting to happen.
It pays to think about how a new object is set up. A constructor describes this.
The good news.... there are abbreviations for writing constructors that the book
has not mentioned yet.
How are global functions reused in other files
The simplest technique is to put the whole definition in a file and "#include" it in the
files that need to reuse the function.
You are not required to do the following as part of CS201.
function.h
function.cpp
The header file has just the function prototype
type function(formal parameters);
The code file
#include "function.h"
type function(formal parameters) { body }
You then pre-compile the code file:
g++ -c function.cppwhich creates a "function.o" file.
Somebody who want to use your function needs a copy of "function.o" and "function.h" and puts
#include "function.h"in their program. The must also put "function.o" in their compilations.
Do you have to put a function in a header file
No you can put it in the file that needs it -- but this only makes sense if it
is only needed in that one file. Otherwise it pays to put it in a special file
and use the above technique to include it where it is needed.
Can you omit the int in unsigned long int etc
Yes.
When to use enum
See ./15.html
Did programmers invent viruses
Only the electronic ones. This is one reason why computer science degrees include an ethics
course. We have a responsibility to practice out skills and talents for the good
not the bad.
Exercises
Next More on functions
[ 15.html ]
Quiz 6 on Logical operations and bool