Sum

What is it?

The addition operator (+) we discussed earlier in this guide is useful when you want to add together two numbers. The Sum function gives you the ability to add together two or more values without the need to repeatedly use the addition operator.

What is the Syntax?

The Sum function’s syntax is as follows:

Sum( Number1, Number2, Number3, Number4 ... );

Note that anything beyond Number2 is optional, and you can include as many numbers as you like.

Why might I want to use this?

If you want to add two or more numbers together, you may find it easier to keep track of your expression if you enclose all the numbers you are adding in a single function call, rather than writing something like this:

MyTotal := 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20;

Using the Sum function instead, you would rewrite the above expression as follows:

MyTotal := Sum( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );

Although this achieves the same effect as the first method and uses almost the same amount of code, it is clear that the arguments of your Sum function are grouped together by the parentheses, helping make your code more readable as it grows in size and complexity.

 

<< Previous: Square or Round Brackets? | Next: Power >>