Breaking Functions

You have some idea of how powerful functions are. But beware for they’re easy to break in lots of different ways. We saw some of this back in Chapter 2, ​Debugging: Fixing Code When Things Go Wrong​. Now that we understand functions better, it’s worth taking another look at breaking them.

The most common thing to do is forget a curly brace:

​ ​function​ r(max)
​  ​if​ (max) ​return​ max * Math.random();
​  ​return​ Math.random();
​ }
images/functions/function_missing_opening_curly_brace.png

It’s hard to miss that error! The error messages don’t say that we forgot the curly brace, which would be more helpful. But it’s pretty clear that something ...

Get 3D Game Programming for Kids, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.