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.

552<br />

Creating Functions<br />

If the condition is True, the loop runs at least once. If this condition is<br />

False, the loop doesn’t run.<br />

A variation of the WHILE loop is the DO-WHILE loop, which looks like this:<br />

do {<br />

Command;<br />

} while (condition);<br />

The main difference between the two loops is that the WHILE loop may run<br />

zero or more times, but the DO-WHILE loop will always run at least once.<br />

Somewhere inside a WHILE and DO-WHILE loop, you must have a command<br />

that can change the condition from True to False; otherwise, the loop will<br />

never end, and your program will appear to hang or freeze.<br />

Creating Functions<br />

In Java/C#, every subprogram is a function that can return a value. The<br />

<strong>for</strong>mat of a typical Java function looks like this:<br />

Datatype functionname (Parameter list)<br />

{<br />

Commands;<br />

Return value;<br />

}<br />

The three parts of a C/C++ function are<br />

✦ Data type: Defines the type of value the function returns, such as an<br />

integer (int) or floating point number (float). If you define the data<br />

type as void, then the function doesn’t return a value.<br />

✦ Parameter list: Defines any data and their data types that the function<br />

needs to work.<br />

✦ Return: Defines a value to return. This value must be the same data type<br />

specified right be<strong>for</strong>e the function name.<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 />

}

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

Saved successfully!

Ooh no, something went wrong!