22:prev_link: 02_program_structure
33:next_link: 04_data
44
5+ [[functions]]
56= Functions =
67
78[chapterquote="true"]
@@ -145,6 +146,7 @@ function-local variables as existing only within the function, the
145146language makes it possible to read and understand functions as small
146147universes, without having to worry about all the code at once.
147148
149+ [[scoping]]
148150== Nested scope ==
149151
150152(((variable,scope)))(((inner function)))(((nested functions)))
@@ -248,7 +250,7 @@ if (safeMode)
248250 launchMissiles = function(value) {/* do nothing */};
249251----
250252
251- In Chapter 5, we will discuss the wonderful things that can be done by
253+ In link:05_higher_order.html#higher_order[ Chapter 5] , we will discuss the wonderful things that can be done by
252254passing around function values to other functions.
253255
254256== Declaration notation ==
@@ -302,6 +304,7 @@ function example() {
302304}
303305----
304306
307+ [[stack]]
305308== The call stack ==
306309
307310indexsee:[stack,call stack]
@@ -397,6 +400,7 @@ one will tell you about it.
397400
398401indexsee:[optional argument,argument optional]
399402
403+ [[power]]
400404(((argument,optional)))The upside is that this behavior can be used
401405to have a function take “optional” arguments. For example, the
402406following version of `power` can be called either with two arguments
@@ -422,7 +426,7 @@ console.log(power(4, 3));
422426// → 64
423427----
424428
425- In the next chapter, we will see a way in which a function body can
429+ In the link:04_data.html#arguments_object[ next chapter] , we will see a way in which a function body can
426430get at the exact list of arguments that were passed. This is helpful
427431because it makes it possible for a function to accept any number of
428432arguments. `console.log` makes use of this—it outputs all of the
@@ -546,12 +550,12 @@ between human-friendliness and machine-friendliness. Almost
546550any program can be made faster by making it
547551bigger and more convoluted. The programmer may decide on an appropriate balance.
548552
549- In the case of the earlier `power` function, the inelegant (looping)
550- version is still fairly simple and easy to read. It doesn't make much
551- sense to replace it with the recursive version. Often, though, a
552- program deals with such complex concepts that giving up some
553- efficiency in order to make the program more straightforward becomes
554- an attractive choice.
553+ In the case of the link:03_functions.html#power[ earlier] `power`
554+ function, the inelegant (looping) version is still fairly simple and
555+ easy to read. It doesn't make much sense to replace it with the
556+ recursive version. Often, though, a program deals with such complex
557+ concepts that giving up some efficiency in order to make the program
558+ more straightforward becomes an attractive choice.
555559
556560The basic rule, which has been repeated by many programmers and with
557561which I wholeheartedly agree, is to not worry about efficiency until
@@ -578,6 +582,7 @@ with loops. Most often these are problems that require exploring or
578582processing several “branches”, each of which might branch out again
579583into more branches.
580584
585+ [[recursive_puzzle]]
581586Consider this puzzle: by starting from the number 1 and repeatedly
582587either adding 5 or multiplying by 3, an infinite amount of new
583588numbers can be produced. How would you write a function that, given a
@@ -788,6 +793,7 @@ sure you're going to need it. It can be tempting to write general
788793Resist that urge. You won't get any real work done, and you'll end up
789794writing a lot of code that no one will ever use.
790795
796+ [[pure]]
791797== Functions and side effects ==
792798
793799(((side effect)))(((pure function)))(((function,purity)))Functions
@@ -860,9 +866,10 @@ sections help organize regular text.
860866
861867=== Minimum ===
862868
863- The previous chapter introduced the standard function `Math.min` that
864- returns its smallest argument. We can do that ourselves now. Write a
865- function `min` that takes two arguments and returns their minimum.
869+ The link:02_program_structure.html#return_values[previous chapter]
870+ introduced the standard function `Math.min` that returns its smallest
871+ argument. We can do that ourselves now. Write a function `min` that
872+ takes two arguments and returns their minimum.
866873
867874ifdef::html_target[]
868875
@@ -928,13 +935,13 @@ endif::html_target[]
928935
929936!!solution!!
930937
931- Your function will likely look somewhat similar to the inner `find` function in
932- the recursive `findSolution` example in this chapter, with an
933- `if`/`else if`/`else` chain that tests which of the three cases
938+ Your function will likely look somewhat similar to the inner `find`
939+ function in the recursive `findSolution` link:03_functions.html#recursive_puzzle[ example] in this chapter, with
940+ an `if`/`else if`/`else` chain that tests which of the three cases
934941applies. The final `else`, corresponding to the third case, makes the
935- recursive call. Each of the
936- branches should contain a `return` statement or in some other way
937- arrange for a specific value to be returned.
942+ recursive call. Each of the branches should contain a `return`
943+ statement or in some other way arrange for a specific value to be
944+ returned.
938945
939946When given a negative number, the function will recurse again and
940947again, passing itself an ever more negative number, thus getting
0 commit comments