15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Data Structures 535<br />

If a function doesn’t return a value or require any data, it might look like this:<br />

void myfunction ()<br />

{<br />

Command;<br />

}<br />

If the function needs to return an integer value, it might look like this:<br />

int myfunction ()<br />

{<br />

Command;<br />

return value;<br />

}<br />

Book VI<br />

Chapter 1<br />

C and C++<br />

In the preceding example, value represents any integer value.<br />

To accept data, a function needs a parameter list, which simply lists a variable<br />

to hold data along with its specific data type, such as an integer or character.<br />

To create a function that accepts an integer and a character, you could do<br />

something like this:<br />

int myfunction (int mynumber, char myletter)<br />

{<br />

Command;<br />

return value;<br />

}<br />

The preceding function accepts two values by value, so the function can<br />

change the values of variables in its parameter list, but those changed values<br />

won’t appear outside that function.<br />

If you want a function to change the values of its parameters, you need to<br />

define a parameter list by identifying which variables accept values by reference.<br />

To identify values passed by reference, use the & sign, such as<br />

int myfunction (int& mynumber, char myletter)<br />

{<br />

Command;<br />

return value;<br />

}<br />

Data Structures<br />

Many C/C++ compilers include libraries that offer data structures, such as<br />

stacks or collections. However, three built-in data structures of C/C++ are the<br />

structure (sometimes called a record in other <strong>programming</strong> languages), enumerated<br />

variables, and the array.

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

Saved successfully!

Ooh no, something went wrong!