12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

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.

SYNTAX<br />

▼<br />

▲<br />

Gett<strong>in</strong>g Your Feet Wet<br />

functions can be used <strong>in</strong> <strong>C++</strong> exactly as they can be used <strong>in</strong> C. However, <strong>C++</strong> takes functions<br />

a bit further. I’ll leave that discussion for now and pick it up aga<strong>in</strong> later when we look deeper<br />

<strong>in</strong>to <strong>C++</strong>.<br />

HOUSE RULES FOR FUNCTIONS<br />

■ A function can take any number of parameters or no parameters at all.<br />

■ A function can be written to return a value, but it is not mandatory that a function<br />

return a value.<br />

■ If a function has a return type of void, it cannot return a value. If you attempt to<br />

return a value from a function with a return type of void, a compiler error will be<br />

issued. A function that returns void need not conta<strong>in</strong> a return statement at all, but<br />

it may if desired. Either way is acceptable. If no return statement is provided, the<br />

function returns automatically when it gets to the end of the function block (the<br />

clos<strong>in</strong>g brace).<br />

■ If the function prototype <strong>in</strong>dicates that the function returns a value, the function<br />

body should conta<strong>in</strong> a return statement that returns a value. If the function does<br />

not return a value, a compiler warn<strong>in</strong>g is issued.<br />

■ Functions can take any number of parameters but can return only one value.<br />

■ Variables can be passed to functions by value, by po<strong>in</strong>ter, or by reference. (I’ll<br />

discuss this a little later.)<br />

The function statement, <strong>in</strong> declaration (prototype) format:<br />

ret_type function_name(argtype_1 arg_1, argtype_2 arg_2, ..., argtype_n arg_n);<br />

The function declaration identifies a function that will be <strong>in</strong>cluded <strong>in</strong> the code. It shows the<br />

return data type (ret_type) of the function and the name of the function (function_name),<br />

and identifies the order (arg_1, arg_2, …, arg_n) and types (argtype_1, argtype_2, …,<br />

argtype_n) of data arguments the function will expect.<br />

The function statement, <strong>in</strong> def<strong>in</strong>ition format:<br />

ret_type function_name(argtype_1 arg_1, argtype_2 arg_2, ..., argtype_n arg_n) {<br />

statements;<br />

}<br />

The function def<strong>in</strong>ition identifies the code block (statements) that makes up the function<br />

and shows the return data type (ret_type) of the function. function_name identifies the<br />

function. The parameters supplied to the function (arg_1, arg_2, …, arg_n) and their types<br />

(argtype_1, argtype_2, …, argtype_n) are <strong>in</strong>cluded.<br />

25<br />

1

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

Saved successfully!

Ooh no, something went wrong!