Beginning and Ending a Function

<< Click to Display Table of Contents >>

Navigation:  Concordance Programming Fundamentals > Writing a Function >

Beginning and Ending a Function

After you have declared the main() function, your next task is to write the beginning and ending of the function.

To declare a function

1.Write the name of the function, followed by two parentheses (), followed by two brackets {}.

2.Optionally in the parentheses, write any values you wish to pass as parameters, separated by commas.

i.As in math, parameters represent values that exist outside the function that you want to use inside the function. Note that parameters are different from global variables,  A parameter is essentially a copy of a value, rather than the value itself. (In programming terminology, this is called pass-by-value.) Therefore, a function can modify the value of a parameter, without worrying about accidentally changing the original value outside the function. In contrast, any change to a global parameter will change that value for all subsequent parts of the script.

ii.If you do not have any parameters, you can simply have empty parentheses ().

 

The following example describes a basic function declaration with two parameters:

Multiply_x_by_y(int x, int y)

{

}

This example declares a function named Multiply_x_by_y, which takes two parameters: an integer x, and an integer y.

 

Once you have declared your function, you can declare any additional variables you may need within the function. For more information, see Declaring Variables. For more information on declaring a function, see Function Declaration in the CPL Language Reference.