Writing a Function Body

<< Click to Display Table of Contents >>

Navigation:  Concordance Programming Fundamentals > Writing a Function >

Writing a Function Body

Once you have declared your function and the variables, you can write the rest of your function.

Functions exist mainly to help you organize your code, so you can put whatever you wish into one, as long as it seems logical to you. The main caveats to writing a function body are as follows:

If you wish to return a value out of your function, you must use a return value. For more information, see Returning a Value.

The parameters are considered variables within the function; however, they contain only a copy of the value you passed in. Therefore, nothing you do will affect the original value.

 

The following example code takes the two parameters x and y, multiplies them together, and assigns that value to z. So, if the value of x was 5 and y was 10, z would end up holding 50.

Multiply_X_by_Y(int x, int y)

{

   int z;

   z = x * y;
}

 

Once you have finished writing your function, you can use the function in a function call. For more information, see Calling a Function.