Functions – A Revision
-
Upload
philyra-desma -
Category
Documents
-
view
13 -
download
0
description
Transcript of Functions – A Revision
Functions – A Revision
Today’s materialWe will review
Predefined functions (just a quick reminder)Consider general good practice when
developing a functionThen look at cases of functions that
return values via the ‘return’ statement Use ‘By Reference’ to return more than one value
With the option of also ‘returning’ the success of the operation
Then...Look at some additional things C++ allows us
to do with functionsFunctions with default valuesFunction overloading
And then to endWork through a simple example that combines
all the material into a working programme
Predefined functionsThese are functions that are provided by the
developers of the compilerSome are standard to C++Others may be compiler specific
They facilitate common tasksFor example mathematical functions, eg sin,
cosString functions, eg toupper
Our ‘own’ functionsWe develop our own functions to
Enable us to break code into manageable blocks Rather than one ‘huge’ main()
Functions can be individually specified With testing criteria defined if appropriate
By doing this developers can work in parallel on functions and combine them into the final working programme
Well written functions are also ‘self contained’ so we can use them in other programmes we develop.
When we develop a functionBefore we code we must consider carefully
what we wish the function to doFirstly we consider
What is a sensible name for the function We cannot use existing names (unless overloading) The name should indicate what the function does!
Do we need to pass in any parameters? Again, if so, think carefully about the data types – it
may not always be obvious
When we develop a functionWe also need to consider
What (if any) value will be returned If so, what will be the data type returned?
Will more than one value be returned? If so we need to use the ‘by reference’ approach
And if returning ‘by reference’ Is a void function OK or, Should we additionally return the ‘success’ of the
function via the return statement?
In summaryAll functions are of the format
return_type name ( arguments )
Any valid C++ data type, or void
Something relevant to the function helps !
Any valid C++ data type, or void
But in C++ we can do a more!We can have default values (arguments) for
functionsWe specify these in the function definition /
prototypeThey are good for parameters that we seldom
change when calling a function (but would like the option to!)
There are however a few rules!Default values come at the end of the
parameter listYou cannot have a default value for a ‘by
reference’ variableWhen calling a function, once a default value is
taken, all the following variables take their default values
Some examplesThese are OK!
void PrintValues(int nValue1, int nValue2=10)void OpenLogFile(char *strFilename="default.l
og");int RollDie(int nSides=6);
These are not!int RollDie(int nSides=6 , int rolls);Int CalculateVolume ( int &radius = 10, int
&length = 5);
And a code example!void PrintValues(int nValue1=10, int nValue2=20, int nValue3=30){ using namespace std; cout << "Values: " << nValue1 << " " << nValue2 << " " << nValue3 << endl;}
PrintValues(1, 2,
3);
PrintValues(1, 2);
PrintValues(1);
PrintValues();
Given the code
The output would be
Values: 1 2 3
Values: 1 2 30
Values: 1 20
30
Values: 10 20
30
But in C++ we can do a more!We also have the option to ‘overload’ a function
This means defining multiple versions of a function with the same name
They are distinguished by having different parameter lists, egvoid functionXYZ( int a, int b, int c)void functionXYZ( int a)void functionXYZ( void )
Note however that the return type
But in C++ we can do a more!Note however that the return type does not
distinguish functions when overloading.As a result the following would cause an
errorvoid functionXYZ( int a, int b, int c)int functionXYZ( int a, int b, int c)
And now for some coding!Let’s do an example that bring together all
we know!