Example 3 explained, Example 3 explained -12 – Rockwell Automation 2098-IPD-xxx Ultra5000 C Programming using the Motion Library User Manual

Page 24

Advertising
background image

Publication 2098-PM001E-EN-P — July 2002

1-12

Programming Motion Control in C

{

MoveSetAcc(accdec);

/* Initialize index move

*/

MoveSetVel(velocity);

MoveSetDec(accdec);

OutputSetState(INMOTION, ON);

/* Set Output 1 */

MoveIncremental(distance);

/* Start Move */

while (MoveInProgress());

/* Wait for move to

complete */

OutputSetState(INMOTION, OFF);

/* Clear Output 1 */

return ++partcount;

/* Increment partcount by

1. */

}

Example 3 Explained

A function definition has this form:

return-type

function-name(parameter declarations, if any)

{

declarations

statements

}

The function insert is called from only one place by main, in the line

partcount = insert(INSERTDIST, INSERTVEL, INSERTACCDEC);

The call passes three arguments to insert, which each time returns a
long integer partcount. The first line of insert itself,

long insert(long distance, long velocity, long accdec)

declares the parameter types and names, and the type of the result
that the function returns. The names used by insert for its parameters
are local to insert, and are not visible to any other functions (i.e.,
Other routines can use the same names without conflict.).

The value of partcount is transferred to main by the return statement.
Any expression may follow return:

return expression;

A function need not return a value; a return with no expression causes
control, but no value, to be returned to the caller. Also, the calling
function can ignore a value returned by a function.

Advertising