Sub topic 2
How a function call is written
Every formula in this tutorial uses the same grammar. Once you can read that grammar, you can read SUM, AVERAGE, COUNT, MIN, MAX and any function you meet later. This chapter covers the parts of a function call, the rules for combining them, and the errors you get when you break those rules.
This chapter assumes you can type a value or formula into a cell and press Enter. It also assumes you know that B2 means column B, row 2, and that B2:B6 is a range of five cells. The previous chapter covered ranges in more detail.
The anatomy of a call
A function call has four parts:
- An equals sign,
=, that tells Sheets the cell holds a formula and not plain text. - The function name, such as
SUM. - A pair of parentheses that enclose the arguments.
- Zero or more arguments inside the parentheses, separated by commas.
=SUM(B2:B6)Here SUM is the name and B2:B6 is its single argument. The function reads the cells in that range, adds the numbers and returns one value. The cell shows that value, and the formula bar shows the formula.
=SUM(B2:B6, D2:D6, 100)This call has three arguments: two ranges and a literal number. The commas separate the arguments, and the function treats them as one pool of values.
What counts as an argument
An argument is any piece of input the function accepts. For the five aggregate functions, an argument can be any of these:
| Kind | Example | Notes |
|---|---|---|
| A single cell | `B2` | One value. |
| A range | `B2:B6` | Many values, read in one pass. |
| A literal number | `100` | Typed directly into the formula. |
| An expression | `B2*1.2` | Sheets calculates it first, then passes the result. |
| Another function call | `ROUND(C2, 1)` | Sheets evaluates the inner call first. This is called nesting. |
You can mix these kinds freely, as in =MAX(B2:B6, C2, 50, D2*2). The function does not care where each value came from.
Text arguments need quotation marks. In =IFERROR(AVERAGE(B2:B6), "No data"), the second argument is the text No data. Without the quotes, Sheets would read No and data as names and return an error. Use straight double quotes ", not curly quotes. If you paste a formula from a word processor, curly quotes often sneak in and break it.
Required and optional arguments
Each function defines how many arguments it takes. SUM, AVERAGE, COUNT, MIN and MAX all need at least one value. After the first, you can add as many more as you like. Other functions have a fixed number of required arguments followed by optional ones. For example, ROUND(value, places) needs a value, and places is optional. You will use ROUND in a later chapter.
When you type a function name and an opening parenthesis, Sheets shows a help box. It lists the arguments, marks the optional ones and gives a short example. Use this box to check the order of arguments for a function you do not know well.
Separators and locale
The comma between arguments is a convention set by your spreadsheet's locale. Locales that use a comma as the decimal mark, such as Germany or France, usually use a semicolon as the argument separator instead:
=SUM(B2:B6; D2:D6; 100)Check your locale under File, then Settings. If a formula copied from a tutorial fails with a parse error, the separator is a likely cause. Replace commas with semicolons, or the other way round. The examples in this tutorial use commas.
Rules the parser applies
Sheets applies a few rules when it reads your formula.
- Function names are not case sensitive.
=sum(b2:b6)and=SUM(B2:B6)do the same thing. Sheets usually converts the name to capitals once it recognizes it. If the name stays lowercase after you press Enter, Sheets did not recognize it. - Spaces between tokens are ignored.
=SUM( B2:B6 , 100 )works. Spaces inside a quoted text argument are kept, because they are part of the text. - Every opening parenthesis needs a closing one. Sheets often adds the closing parenthesis for you as you type. In nested calls, count them yourself before you press Enter.
- Arguments run left to right, and each is evaluated before the function runs. In
=ROUND(AVERAGE(B2:B6), 2), theAVERAGEcall finishes first.ROUNDthen receives its result as its first argument. - A function with no arguments still needs parentheses.
=TODAY()is correct and=TODAYis not. The five functions in this tutorial always take at least one argument.
A worked example
Suppose the sheet has monthly sales in B2:B6:
| Row | A | B |
|---|---|---|
| 1 | Month | Sales |
| 2 | Jan | 120 |
| 3 | Feb | 95 |
| 4 | Mar | 140 |
| 5 | Apr | 80 |
| 6 | May | 110 |
Type each of these into its own cell and compare:
=SUM(B2:B6)
=AVERAGE(B2:B6)
=COUNT(B2:B6)
=MIN(B2:B6)
=MAX(B2:B6)The results are 545, 109, 5, 80 and 140. All five calls have the same shape: a name, parentheses and one range. Only the name changes, and with it the question you ask of the data.
You can also build a single formula from several calls. This one reports the spread between the best and worst month:
=MAX(B2:B6) - MIN(B2:B6)This is not nesting. It is two separate calls joined by the subtraction operator, and Sheets evaluates each call and then subtracts. It returns 60. Operators such as +, -, * and / can join function calls to each other, to cell references and to numbers.
Reading a nested formula
Nested formulas look dense, but you can read them from the inside out. Take this one:
=IFERROR(ROUND(AVERAGE(B2:B6), 2), "No data")AVERAGE(B2:B6)produces 109.ROUND(109, 2)rounds that to two decimal places and returns 109.IFERROR(109, "No data")checks whether its first argument is an error. It is not, so it returns 109.
If B2:B6 held no numbers, step 1 would produce a #DIV/0! error. ROUND would pass the error through, and IFERROR would catch it and return No data. The inner calls run first, and the outer calls work with whatever the inner ones return.
A common mistake is putting an argument in the wrong call. In =ROUND(AVERAGE(B2:B6, 2)), the 2 falls inside AVERAGE, which now averages the five sales and the number 2. ROUND then gets only one argument, and that is a different result from the one you wanted. Check which parenthesis each comma sits inside.
Typing formulas efficiently
You can type a range by hand, or you can select it with the mouse after typing =SUM(. Dragging across the cells writes the range into the formula for you, and it avoids typos in addresses. You can also click a single cell to add it as an argument. Pressing Enter closes the parenthesis if it is still open.
Sheets also highlights each reference in the formula with a color and outlines the matching cells on the grid. While editing a formula, check that the colored outline covers exactly the cells you intend. This is the fastest way to catch a range that is one row short.
Errors that come from the call itself
Some errors come from the shape of the call, not from the data. They are worth recognizing because their fixes are mechanical.
| What you see | Typical cause | Fix |
|---|---|---|
| `#NAME?` | A misspelled function name, such as `=SUMM(B2:B6)`, or text without quotes. | Correct the spelling or add quotation marks. |
| `#ERROR!` with "Formula parse error" | An unmatched parenthesis, a misplaced separator, or curly quotes. | Check parentheses and separators for your locale. |
| `#N/A` with "Wrong number of arguments" | Too few or too many arguments for the function. | Check the help box for what the function expects. |
| `#REF!` | A reference points to a cell or range that no longer exists, for example after deleting a column. | Rewrite the reference. |
Errors caused by the data, such as #DIV/0! from AVERAGE over an empty range, are handled in a later chapter with IFERROR.
Quick reference
- Start with
=, then the function name, then arguments in parentheses. - Separate arguments with commas, or semicolons in some locales.
- Arguments can be cells, ranges, numbers, expressions, quoted text, or other function calls.
- Inner calls run before outer calls.
- Names are case-insensitive, and extra spaces are harmless.
Exercise
A colleague writes =ROUND(AVERAGE(B2:B6, 2)) to get the average of B2:B6 rounded to two decimal places. The cell shows 87.1667 for data where the true average is 109. What is wrong, and what is the corrected formula?
Show answer
The 2 is inside the AVERAGE call, so AVERAGE averages the five cells and the extra number 2, which pulls the result down. ROUND receives only one argument, so it rounds to zero places by default. The closing parenthesis for AVERAGE must come before the 2. The corrected formula is =ROUND(AVERAGE(B2:B6), 2).
Exercise
Write one formula that returns the difference between the largest and smallest value in B2:B6, and explain why this is not an example of nesting.
Show answer
=MAX(B2:B6) - MIN(B2:B6). Neither function call sits inside the other's parentheses. They are two independent calls joined by the subtraction operator. Nesting means one call appears as an argument of another, as in =ROUND(AVERAGE(B2:B6), 2).
Exercise
You type =SUM(B2:B6; D2:D6) in a spreadsheet and get a formula parse error. Give two possible causes and the fix for each.
Show answer
First, the spreadsheet locale uses commas as the argument separator, so the semicolon is invalid. Change it to =SUM(B2:B6, D2:D6). Second, the formula may contain a typo such as a missing parenthesis or curly quotes. Check that every opening parenthesis has a closing one. If the locale uses semicolons, the original formula is correct and the cause lies elsewhere.