Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Parts of a function
1.
2.
3.
4.
Function Declarations
Recall
If
This
Function Declarations
Or
Function Declarations
In
declaration, it could
has
Function Name
The
If
You
Function overload
In
Function overload
Another
example
Function overload
Remember
cin.ignore() ?
cin.ignore(void)
cin.ignore(int)
cin.ignore(int,
char)
Function overload
Note:
Modularization
A
This
There
Separation of variables
In
general, the x in
cube is just a dummy
variable, which has
nothing to do with the
variable names in
main or in any other
function.
This
is part of the
reason why we
declare cube above
without a variable
name, just the data
types. It is because
the variable names
are immaterial, and
double cube(double);
int main() {
// These variable names in main dont care
about cube
double x=2.0;
cout<<cube(x);
}
/** Finds the cube
@param x is the number
@returns the cube of x, i.e., x*x*x or x^3
*/
double cube(double x) {
// The variable x is completely independent of
main
return x*x*x;
}
Separation of variables
Even
though I
changed the dummy
variable from x to y,
this is still the exact
same program.
double cube(double);
int main() {
// These variable names in main dont care
about cube
double x=2.0;
cout<<cube(x);
}
/** Finds the cube
@param x is the number
@returns the cube of x, i.e., x*x*x or x^3
*/
double cube(double y) {
// The variable x is completely independent of
main
return y*y*y;
}
Side Effects
It
The
Things
This
Procedure
There
the
return; statement is
void Welcome(string);
int main { }
/** */
void Welcome(string name) {
cout<<Welcome
<<name<<!\n;
return;
}
Predicate
A
into
if(insideCircle(x,y))
reason to
include the return; in a
function is to terminate
it early.
Also
cout<<Well hi there\n;
if(n==10) {
cout<<You are special\n;
return;
}
cout<<This is not going to print if 10 is
input\n;
cout<<I wish I was special :(;
cout<<So very special ... \n;
return;
}
note the
comments do not have
@return because there
is no value returned.
/** */
int sign(double x) {
if(x>0) return 1;
if(x<0) return -1;
// Error. The case x == 0 is not
covered.
}
/** */
int sign(double x) {
if(x>0) return 1;
if(x<0) return -1;
// The case x == 0 is now covered.
return 0;
}
Example: Factorization
We
15 = 3*5
24 = 2*2*2*3
It
Example: Factorization
This
Returns