Function Declaration

<< Click to Display Table of Contents >>

Navigation:  Concordance Programming Language Reference >

Function Declaration

The following topic describes function declaration, parameter, and return value syntax.

Function Declarations

Syntax:

name(optional parameters)

{

statements

}

A CPL function consists of a group of program statements that perform a job. Functions can optionally return values. CPL functions can call other functions to perform work without worrying how they do their jobs. Functions use discreet local variables that come into existence when the function runs, and go away when the function finishes. Functions facilitate complex programs by breaking them into small, easy to manage tasks.

CPL program execution always begins with a function called main. Every CPL program must have a main() function. It is executed by Concordance. main() may then call other functions for help in executing the program.

 

Function Parameters

Parameter declarations appear within the parentheses following the function’s name. Parameters are declared exactly as all other variables are declared, by stating their type followed by their names. Multiple parameter declarations can be separated by commas if they are of the same type, or by semi-colons for different type declarations.

Example:

max(float a; float b)

{

float x;

if (a > b)

x = a;

else

x = b;

return(x);

}

Concordance converts all parameters passed to max() into float types if necessary. The function parameters could also have been declared max(float a, b) with the same effect. max() returns the largest of the two parameters passed. A statement that uses max() might look like this:

salary = salary * max(1.5,raise);

Since a and b become local variables, initialized to their passed values and accessible only within max(), max() can be rewritten without local variable x.

max(float a, b)

{

if (a < b)

a = b;

return(a);

}

 

Return Statement

The value computed by max() is returned by the return statement. Any value can be computed within the return statement’s parentheses. A return statement without the parentheses will cause the function to finish, but no value will be returned. Return statements are optional, a function without one will return automatically when the closing end statement is encountered.

CPL functions can contain multiple return statements. However, a single return statement at the end of the function is preferred. This makes the function easier to understand, and simpler to maintain.