Join thousands of grownups who get articles on teaching kids to love coding.
Back to Articles

What's up with the parentheses in code?

Seamus Edson Headshot

Wondering why you have to use parentheses with Bitsbox functions? Our engineer extraordinaire Seamus explains!

The short answer is: Bitsbox uses Javascript which uses parentheses that way. It's like the grammar of code! () is part of the Javascript syntax. Just like we need certain things—periods, commas, subjects and verbs—to communicate, computers need that, too. A programming language lets a human (smart/interpretive) talk to a computer (kinda dumb and very literal). While another human might understand what we mean without parentheses, a computer might not.

Let's take an example from math class: 2 * 3 + 3 is 9, but 2 * (3 + 3) is 12. The parentheses are there to show WHAT we mean. The first expression has an ambiguous order of operations. We have to rely on the convention of PEMDAS to evaluate it (which the computer also does). The parentheses in the second expression make the order of operations more explicit. If that is what me mean, we have to included them.

That is the main purpose of parentheses in a programming language like JS: to group stuff so the computer knows what we mean. The other main use of parentheses is for functions. Declaring a function looks like this:

    1  myFunc (a, b) {
    2  
    3    return a * (b + 2)
    4  
    5  }
  

and calling a function looks like this:

    1  myFunct(12, 3)
  

These are both extensions of the same idea of grouping. We use parentheses to mark the group of variables that a function has to care about.

Plus, we've got curly braces. The curly braces {} are used like parentheses too, but instead of grouping expressions for interpretation, they group lines of code into functions or blocks for the computer to read and know what we mean. Before most keyboards had { } and [ ] some programming languages used ( ) for everything: grouping lines of code, function declarations, array access…Which led to some very ambiguous looking code that could be hard to read!

When people invent programming languages they can make any symbol do anything! * does not have to be multiplication and it isn't in every programming language. It is just a well worn convention to use * for multiplication. In JS // is used to write a comment line but in Python, you'd use # to do the same thing. The convention of using parentheses in the Javascript way is pretty old school and is the same in many of the most popular programming languages in the world. The earliest computer research was done by trained mathematicians. Because of their legacy, many programming syntaxes are still based on western mathematical notation—which uses parentheses in pretty similar ways.

So the next time you are writing your code in Bitsbox, you're using the same kind of syntax programmers all over the world have come to adopt! Pretty neat!

Got any questions for why something works the way it does? Ask our engineers!

Hannah Jorgensen
About the author: Hannah works customer support, is the proud recipient of an English degree, and is learning to code with Bitsbox.
Join thousands of grownups who get articles on teaching kids to love coding.
Cute icons Stamp not found