140 likes | 213 Views
Functions – A Revision. Today’s material. We will review Predefined functions (just a quick reminder) Consider general good practice when developing a function Then look at cases of functions that return values via the ‘return’ statement Use ‘By Reference’ to return more than one value
E N D
Today’s material • We will review • Predefined functions (just a quick reminder) • Consider general good practice when developing a function • Then 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 functions • Functions with default values • Function overloading • And then to end • Work through a simple example that combines all the material into a working programme
Predefined functions • These are functions that are provided by the developers of the compiler • Some are standard to C++ • Others may be compiler specific • They facilitate common tasks • For example mathematical functions, eg sin, cos • String functions, egtoupper
Our ‘own’ functions • We 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 function • Before we code we must consider carefully what we wish the function to do • Firstly 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 function • We 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 summary • All functions are of the format return_typename( 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 functions • We specify these in the function definition / prototype • They 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 list • You cannot have a default value for a ‘by reference’ variable • When calling a function, once a default value is taken, all the following variables take their default values
Some examples • These are OK! • voidPrintValues(int nValue1, int nValue2=10) • void OpenLogFile(char *strFilename="default.log"); • intRollDie(intnSides=6); • These are not! • intRollDie(intnSides=6 , int rolls); • IntCalculateVolume ( 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; } Given the code The output would be PrintValues(1, 2, 3); PrintValues(1, 2); PrintValues(1); PrintValues(); 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, eg • void functionXYZ( int a, int b, int c) • void functionXYZ( inta) • 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 error • void functionXYZ( int a, int b, int c) • intfunctionXYZ( int a, int b, int c)
And now for some coding! • Let’s do an example that bring together all we know!