Functions – A Revision

14
Functions – A Revision

description

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 - PowerPoint PPT Presentation

Transcript of Functions – A Revision

Page 1: Functions – A Revision

Functions – A Revision

Page 2: 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

Page 3: Functions – A Revision

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

Page 4: Functions – A Revision

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

Page 5: Functions – A Revision

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.

Page 6: Functions – A Revision

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

Page 7: Functions – A Revision

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?

Page 8: Functions – A Revision

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

Page 9: Functions – A Revision

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

Page 10: Functions – A Revision

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);

Page 11: Functions – A Revision

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

Page 12: Functions – A Revision

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

Page 13: Functions – A Revision

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)

Page 14: Functions – A Revision

And now for some coding!Let’s do an example that bring together all

we know!