12.07.2015 Views

PDF version - Computing Help - University of Cambridge

PDF version - Computing Help - University of Cambridge

PDF version - Computing Help - University of Cambridge

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1A C++ courseworkhttp://www-h.eng.cam.ac.uk/help/tpl/languages/C++/...need 1 number as input (like the square root function) or several, or none. Notethat in C++, functions are said to "return" their output back to the thing that asked for them. They can return one thing or nothing.Execution <strong>of</strong> the function code ends when a return statement is reached, or when the function code ends.The first line <strong>of</strong> the function is compact and contains a lot <strong>of</strong> information.int - this is saying that the function is going to calculate an integer value rather than (say) a string.timesBy7 - this is the name <strong>of</strong> the function(int number) - this is saying that timesBy7 needs to be given an integer as input, and inside this function the integer is going tobe known as number. Some functions don't need any inputs. Other functions might need many inputs. This function needs exactlyone integer.Here's a little program that uses this function to display some multiples <strong>of</strong> 7. Though short, it illustrates what you need to do whenwriting your own functions. From now on, just about all your programs in every computing language you learn will use functions, sostudy this example carefully#include #include using namespace std;// This function multiples the given number by 7int timesBy7(int number) {return number*7;}int main() {int num=1;while (num

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!