Rearranging labs
The lab at 1:10 has 40 students in it
The lab at 3:10
has 18 students in it.
There are not enough machines at 1:10, so we would like anyone who possibly can to move to
the 3:10 lab.
Please email me if you can switch labs
C++ Functions,Chapter 2 Weiss, Chapter 5 Lafore
Components of a function
The
declaration
This is called the function prototype and is
usually found near the top of a program
This is just the first line of the function
The definition
This is the C++ implementation of the function
The call
This is a call to the operating system to actually
execute the code in the function definition with real data.
Function declaration (prototypes)
Historically a
C++ compiler parses the source code from top to bottom.
If a function is
called before the code for it is found, an error message is sent
A prototype tells
the compiler it will find the code later, so dont crash the program.
What is the
prototype?
returnType functionName(argurments);
no
implementation of the function
One reason for
using prototypes is so main( ) is the first
function listed, since this is where execution begins.
Defining functions
Functions do not
have to be part of a class.
They consist of
returnType funName(arguments)
{ statements}
The first line
(called the declarator) must agree exactly with its
prototype
If you put the
definition before the first call, a prototype is not needed.
The argument
variable names may be omitted from the prototype.
#include <iostream>
using namespace std;
int max2(int, int); //prototype
int main( )
{ int x =
54, y=32;
cout<<
"Max is << max2(x,y)<<endl;
return 0;
}
int max2(int a, int b) //definition
{ return a >b ? a
: b; }
Calling functions
When a function is called, control of the CPU is
transferred to the function, and the statement in the function are executed
The return
statement in a function causes control to pass back to the calling function.
All that is needed to call a function is the function
name followed by parentheses.
Inside the
parentheses should be the actual data the function needs to do its job.
If the function
is a void function (returns void) it is a statement by itself.
If it returns a
value, you should have a variable to accept the value it returns.
Overloading functions
C++ allows you to have more than one function with the
same name.
These are called
overloaded fuctions.
The compiler must know which function to use when the
function is called.
The signature of the of the
overloaded functions must be different so the compiler can tell which to use.
The signature
is the function name, and the type and number of arguments.
Example of overloading functions
int max2(int, int);
char max2(char, char);
int main( )
{ int x =
54, y=32;
char m='T', n='Z';
cout<<
"Max is "<<max2(x,y)<<endl;
cout<<"But
" << max2(m,n) << " is also
max" << endl;
return 0;
}
int max2(int a, int b)
{ return a >b ? a
: b; }
char max2(char c, char d)
{ return c > d ? c : d ; }
Default parameters for functions
A function can be
called without specifying all of its arguments.
The function
prototype provides default values for those arguments that are not specified in
the call
The user of the
class can either accept the default, or provide their own
value.
Using default values
Example: void myCell(char someC=*, int someV=0);
So this function
can be called by a client either with zero, one or two arguments.
myCell( );
myCell(A)
myCell(A,
82);
Inline functions
Function calls
involve a lot of overhead, and can be relatively time consuming
They are really a call to the operating system to save
the current environment, and give control to another piece of code
One reason to use
functions is to make the logic of our programs clearer, but we also want fast
programs
We can suggest to
the compiler that a short function be compiled inline (i.e., no function call)
Compilers are
quite sophisticated, and will decide if that is really the best way for your
program to be compiled.
Just use the
keyword inline in the function definition:
inline int someFunction(arguments) {body}
Separate Compilation
The source code
for C++ programs is typically found in several different files.
You must tell the
compiler to include these files when it compiles your code.
The #include statement does just exactly that.
By convention, library files to be included are
enclosed in angle brackets <iostream>
Used defined files are enclosed in double quotes: #include otherFile.h
Often, the
prototypes of functions are put into a header file, and the definitions of the
functions are put in another.
This separates
the interface the client using your program needs from the implementation details.
There is more to
this story we will talk about later.
Passing parameters
In C++ and Java all parameters are passed by
value, which means the called function gets a copy of the parameter.
Sometimes what is passed is an address, so the
Call by value
A copy of the
actual argument is passed so it will not be changed in the calling function.
Call by
reference
The parameter is
an address, so the called function has direct access to the object to which the
address points, and can change it, but it cannot change the address
Call by
constant reference
If
you want to make sure the function does NOT change the object passed to it, use
a call by constant reference.
Example
of parameter passing
double avg(const
vector<int> & arr,
int n, bool & flag)
Deciding which way to use
1) if the parameter should be able to be changed, you MUST
use call by reference
2) if the parameter should NOT be changed:
If the parameter is a primitive use call by value
If the parameter is a class type, usually use call by
constant reference
If the class type is small, so that making a copy is
trivial, you could use call by value