Introduction To Programming Using C++: Lecture Three: Functions
Introduction To Programming Using C++: Lecture Three: Functions
Carl Gwilliam
[email protected]
http://hep.ph.liv.ac.uk/~gwilliam/cppcourse
Mathematical Functions
// calculate poisson probability
float poisson = (exp(-mean) * pow(mean,r))/fct;
abs/fabs
cos/sin/tan
log/log10
floor
Function Declaration
return_type function_identifer (argument_list)
A function has to be declared before it is called. This is
also known as the functions prototpye
double sin (double x); //declaration before main (in math.h)
int main() {
double pi = 3.14;
Writing Functions
It is desirable to delegate work outside the main block.
To identify a function use case, looked for a repeated
sequence of statements that share some form of
commonality.
I dont care how the work is done, Im only interested in
return value. double triesfct = 1;
for (int i = 1; i<=tries; i++) triesfct *= i;
Calculate
double winsfct = 1;
combinatorics:
for (int i = 1; i<=wins; i++) winsfct *= i;
float losses = tries - wins;
double lossesfct = 1;
for (int i = 1; i<=losses; i++) lossesfct *= i;
float comb = triesfct / (winsfct * lossesfct);
Switch Statements
The switch statement evaluates
an expression and executes a set
of statements depending upon its
value
The result of the expression in the
switch statement must be of type
integer.
If the result does not match any of
the case labels the default block
is executed.
A break must be included at the
end of each case otherwise the
next statement will be executed
switch (expression) {
case value1:
statement1;
break;
case value2:
statement1;
:
statementN;
break;
:
case valueN:
:
break;
default:
:
}
case 1:
:
break;
case 2:
:
break;
case 3:
:
break;
default:
:
}
Enumerations
enum label {name1 = value1, name2 = value2, .. nameN = valueN}
Enumerations
enum DaysOfWeek {mon=1,tue,wed,thr,fri,sat,sun};
cout << "Enter day (mon=1 to sun=7)" << endl;
int day;
cin >> day;
if (day == thr) {
cout << "C++ Lecture at 11am!\n";
}
If you only want to use the enum to refer to constants
(but don't plan to use the type to declare variables, func
arguments, etc) you can use an unnamed enum
enum {name1, name2, .. nameN};
Typedefs
typedef name type;
It is sometimes uncertain which type will be needed for a
set of variables
typedef long double Number;
e.g. An integer variable
storing the value of a
factorial calculation will
only be useful up to 12!
int main() {
// calculate combinations
Number triesfct = 1;
Use typedef to
aid redefinition of
a variable type
across an entire
program.
Default arguments
function_identifer (argument1 = value1, .. ,argumentN = valueN)
It is often useful to supply a default value for a function
argument.
The default is entered in the function prototype.
double getAngle(double vect1[],double vect2[],int radorDeg = 1);
double getArea(double PI = 3.14,double radius);
int main() {
double angle = getAngle(vect1,vect2); // get angle in radians
double angle = getAngle(vect1,vect2,2); // get angle in degrees
// get area of circle
double Area1 = getArea(3.14157,3); // ok
double Area2 = getArea(,3); // error
}
Recursion
Recursion routines are a powerful and elegant tool in
programming.
Number factorial(int n) {
int fact = 1;
for (int i = 1; i<=n; i++)
fact *= i;
return fact;
}
Number factorial(int n) {
if (n > 1) {
return (n * factorial(n-1));
} else {
return 1;
}
} // function calls itself
Function Overloading
A function is distinct if the function name OR the number
and type of arguments is unique.
int conversion(int celsius);
// same identifier but different
float conversion(float celsius); // argument and return types
int main() {
float celsius;
float fahr = conversion(celsius); // celsius is a float so float
:
// version of function called
}
// functions convert celsius into fahrenheit
int conversion(int celsius) { return (int) (((9.0/5.0) * celsius) + 32); }
float conversion(float celsius) { return (((9.0/5.0) * celsius) + 32); }
Templates allow the use of the same func for multiple types (see later)
Function Overloading
Function overloading is not just used for grouping
identical functions. Overloading can be used when two
functions have a similar purpose.
float probdist(float prob, int wins, int tries);
float probdist(int r, float mean);
int main() {
:
float binom = probdist(prob,wins,tries); // get binomial
float poisson = probdist(r,mean);
// get poisson
:
// the func to call depends only on input args
}
float probdist(float prob, int wins, int tries) {}
float probdist(int r, float mean) {}
Polymorphism
DEFINITION:
In object-oriented programming, polymorphism (from
the Greek meaning having multiple forms) is the
characteristic of being able to assign a different meaning
or usage to something in different contexts - specifically,
to allow an entity such as a variable, a function, or an
object to have more than one form. There are several
different kinds of polymorphism.
http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212803,00.html
2, 5