forked from CalculusWithJulia/CalculusWithJulia.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.html
3065 lines (2520 loc) · 150 KB
/
functions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"
rel="stylesheet">
<style>
.julia {display: block; font-family: "Source Code Pro";
color:#0033CC;
}
.hljl {font-family: "Source Code Pro";
color:#0033CC;
}
body { padding-top: 60px; }
h5:before {content:"\2746\ ";}
h6:before {content:"\2742\ ";}
pre {display: block;}
th, td {
padding: 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
tr:hover {background-color: #f5f5f5;}
.admonition-title:before {content:"\2746\ ";}
.admonition-title { color:#0033CC}
</style>
<!-- .julia:before {content: "julia> "} -->
<style></style>
<script src="https://code.jquery.com/jquery.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ["\$","\$"], ["\\(","\\)"]]
},
displayAlign: "left",
displayIndent: "5%"
});
</script>
<!-- not TeX-AMS-MML_HTMLorMML-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>
</script>
<script>
window.PlotlyConfig = {MathJaxConfig: 'local'}
</script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
$("h1").each(function(index) {
var title = $( this ).text()
$("#page_title").html("<strong>" + title + "</strong>");
document.title = title
});
$( "h2" ).each(function( index ) {
var nm = $( this ).text();
var id = $.trim(nm).replace(/ /g,'');
this.id = id
$("#page_dropdown").append("<li><a href='#" + id + "'>" + nm + "</a></li>");
});
$('[data-toggle="popover"]').popover();
});
</script>
</head>
<body data-spy="scroll" >
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#" id="page_title"></a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Jump to... <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu" id="page_dropdown"></ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<header>
</header>
<div class="title">
</div>
<div class="container-fluid">
<div class="span10 offset1">
<h1>Functions</h1>
<p>A mathematical <a href="http://en.wikipedia.org/wiki/Function_(mathematics)">function</a> is defined abstractly by</p>
<blockquote>
<p><strong>Function:</strong> A function is a <em>relation</em> which assigns to each element in the domain a <em>single</em> element in the range. A <strong>relation</strong> is a set of ordered pairs, <span class="math">$(x,y)$</span>. The set of first coordinates is the domain, the set of second coordinates the range of the relation.</p>
</blockquote>
<p>That is, a function gives a correspondence between values in its domain with values in its range.</p>
<p>This definition is abstract, as functions can be very general. With single-variable calculus, we generally specialize to real-valued functions of a single variable (<em>univariate functions</em>). These typically have the correspondence given by a rule, such as <span class="math">$f(x) = x^2$</span> or <span class="math">$f(x) = \sqrt{x}$</span>. The function's domain may be implicit (as in all <span class="math">$x$</span> for which the rule is defined) or may be explicitly given as part of the rule. The function's range is then the image of its domain, or the set of all <span class="math">$f(x)$</span> for each <span class="math">$x$</span> in the domain (<span class="math">$\{f(x): x \in \text{ domain}\}$</span>).</p>
<p>Some examples of mathematical functions are:</p>
<p class="math">\[
~
f(x) = \cos(x), \quad g(x) = x^2 - x, \quad h(x) = \sqrt{x}, \quad
s(x) = \begin{cases} -1 & x < 0\\1&x>0\end{cases}.
~
\]</p>
<p>For these examples, the domain of both <span class="math">$f(x)$</span> and <span class="math">$g(x)$</span> is all real values of <span class="math">$x$</span>, where as for <span class="math">$h(x)$</span> it is implicitly just the set of non-negative numbers, <span class="math">$[0, \infty)$</span>. Finally, for <span class="math">$s(x)$</span>, we can see that the domain is defined for every <span class="math">$x$</span> but <span class="math">$0$</span>.</p>
<p>In general the range is harder to identify than the domain, and this is the case for these functions too. For <span class="math">$f(x)$</span> we know the <span class="math">$\cos$</span> function is trapped in <span class="math">$[-1,1]$</span> and it is intuitively clear than all values in that set are possible. The function <span class="math">$h(x)$</span> would have range <span class="math">$[0,\infty)$</span>. The <span class="math">$s(x)$</span> function is either <span class="math">$-1$</span> or <span class="math">$1$</span>, so only has two possible values in its range. What about <span class="math">$g(x)$</span>? It is a parabola that opens upward, so any <span class="math">$y$</span> values below the <span class="math">$y$</span> value of its vertex will not appear in the range. In this case, the symmetry indicates that the vertex will be at <span class="math">$(1/2, -1/4)$</span>, so the range is <span class="math">$[-1/4, \infty)$</span>.</p>
<div class="alert alert-info" role="alert">
<div class="markdown"><p><strong>Thanks to Euler (1707-1783):</strong> The formal idea of a function is a relatively modern concept in mathematics. According to <a href="http://www.maa.org/sites/default/files/pdf/upload_library/22/Ford/dunham1.pdf">Dunham</a>, Euler defined a function as an "analytic expression composed in any way whatsoever of the variable quantity and numbers or constant quantities." He goes on to indicate that as Euler matured, so did his notion of function, ending up closer to the modern idea of a correspondence not necessarily tied to a particular formula or “analytic expression.” He finishes by saying: "It is fair to say that we now study functions in analysis because of him."</p>
</div>
</div>
<p>We will see that defining functions within <code>Julia</code> can be as simple a concept as Euler started with, but that the more abstract concept has a great advantage that is exploited in the design of the language.</p>
<h2>Defining simple mathematical functions</h2>
<p>The notation <code>Julia</code> uses to define simple mathematical functions could not be more closely related to how they are written mathematically. For example, the functions <span class="math">$f(x)$</span>, <span class="math">$g(x)$</span>, and <span class="math">$h(x)$</span> above may be defined by:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>cos</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'>
</span><span class='hljl-nf'>h</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
h (generic function with 1 method)
</pre>
<p>The left-hand sign of the equals sign is still an assignment, though in this case an assignment to a function object which has a name and a specification of an argument, <span class="math">$x$</span> in each case above, though other <em>dummy variables</em> could be used. The right hand side is simply <code>Julia</code> code to compute the <em>rule</em> corresponding to the function.</p>
<p>Calling the function also follows standard math notation:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>pi</span><span class='hljl-p'>),</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>),</span><span class='hljl-t'> </span><span class='hljl-nf'>h</span><span class='hljl-p'>(</span><span class='hljl-ni'>4</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
(-1.0, 2, 2.0)
</pre>
<p>For typical cases like the three above, there isn't really much new to learn.</p>
<div class="alert alert-info" role="alert">
<div class="markdown"><p>The equals sign in the definition of a function above is an <em>assignment</em>. Assignment restricts the expressions available on the <em>left</em>-hand side to a) a variable name, b) an indexing assignment, as in <code>xs[1]</code>, or c) a function assignment following this form <code>function_name(args...)</code>. Whereas function definitions and usage in <code>Julia</code> mirrors standard math notation; equations in math are not so mirrored in <code>Julia</code>. In mathematical equations, the left-hand of an equation is typically a complicated algebraic expression. Not so with <code>Julia</code>, where the left hand side of the equals sign is prescribed and quite limited.</p>
</div>
</div>
<h3>The domain of a function</h3>
<p>Functions in <code>Julia</code> have an implicit domain, just as they do mathematically. In the case of <span class="math">$f(x)$</span> and <span class="math">$g(x)$</span>, the right-hand side is defined for all real values of <span class="math">$x$</span>, so the domain is all <span class="math">$x$</span>. For <span class="math">$h(x)$</span> this isn't the case, of course. Trying to call <span class="math">$h(x)$</span> when <span class="math">$x < 0$</span> will give an error:</p>
<pre class='hljl'>
<span class='hljl-nf'>h</span><span class='hljl-p'>(</span><span class='hljl-oB'>-</span><span class='hljl-ni'>1</span><span class='hljl-p'>)</span>
</pre>
<pre class="julia-error">
ERROR: DomainError with -1.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
</pre>
<p>The <code>DomainError</code> is one of many different error types <code>Julia</code> has, in this case it is quite apt: the value <span class="math">$-1$</span> is not in the domain of the function.</p>
<h3>Equations, functions, calling a function</h3>
<p>Mathematically we tend to blur the distinction between the equation</p>
<p class="math">\[
~
y = 5/9 \cdot (x - 32)
~
\]</p>
<p>and the function</p>
<p class="math">\[
~
f(x) = 5/9 \cdot (x - 32)
~
\]</p>
<p>In fact, the graph of a function <span class="math">$f(x)$</span> is simply defined as the graph of the equation <span class="math">$y=f(x)$</span>. There is a distinction in <code>Julia</code> as a command such as</p>
<pre class='hljl'>
<span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-ni'>40</span><span class='hljl-t'>
</span><span class='hljl-n'>y</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>5</span><span class='hljl-oB'>/</span><span class='hljl-ni'>9</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-ni'>32</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
-40.0
</pre>
<p>will evaluate the righthand side with the value of <code>x</code> bound at the time of assignment to <code>y</code>, whereas assignment to a function</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>5</span><span class='hljl-oB'>/</span><span class='hljl-ni'>9</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-ni'>32</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>72</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-cs'>## room temperature</span>
</pre>
<pre class="output">
22.22222222222222
</pre>
<p>will create a function object which is called with a value of <code>x</code> at a later time–-the time the function is called. So the value of <code>x</code> defined when the function is created is not important here (as the value of <code>x</code> used by <code>f</code> is passed in as an argument).</p>
<p>Within <code>Julia</code>, we make note of the distinction between a function object versus a function call. In the definition <code>f(x)=cos(x)</code>, the variable <code>f</code> refers to a function object, whereas the expression <code>f(pi)</code> is a function call. This mirrors the math notation where an <span class="math">$f$</span> is used when properties of a function are being emphasized (such as <span class="math">$f \circ g$</span> for composition) and <span class="math">$f(x)$</span> is used when the values related to the function are being emphasized (such as saying "the plot of the equation <span class="math">$y=f(x)$</span>).</p>
<p>Distinguishing these three related but different concepts (equations, function objects, and function calls) is important when modeling on the computer.</p>
<h3>Cases</h3>
<p>The definition of <span class="math">$s(x)$</span> above has two cases:</p>
<p class="math">\[
~
s(x) = \begin{cases} -1 & s < 0\\ 1 & s > 0. \end{cases}
~
\]</p>
<p>We learn to read this as "If <span class="math">$s$</span> is less than <span class="math">$0$</span>, then the answer is <span class="math">$-1$</span>. If <span class="math">$s$</span> is greater than <span class="math">$0$</span> the answer is <span class="math">$1$</span>." Often–-but not in this example–-there is an "otherwise" case to catch those values of <span class="math">$x$</span> that are not explicitly mentioned. As there is no such "otherwise" case here, we can see that this function has no definition when <span class="math">$x=0$</span>. This function is often called the "sign" function and is also defined by <span class="math">$\lvert x\rvert/x$</span>. (<code>Julia</code>'s <code>sign</code> function actually defines <code>sign(0)</code> to be <code>0</code>.)</p>
<p>How do we create conditional statements in <code>Julia</code>? Programming languages generally have "if-then-else" constructs to handle conditional evaluation. In <code>Julia</code>, the following code will handle the above condition:</p>
<pre class='hljl'>
<span class='hljl-k'>if</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'><</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-t'>
</span><span class='hljl-oB'>-</span><span class='hljl-ni'>1</span><span class='hljl-t'>
</span><span class='hljl-k'>elseif</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>></span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-t'>
</span><span class='hljl-ni'>1</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>
<p>The "otherwise" case would be caught with an <code>else</code> addition. So, for example, this would implement <code>Julia</code>'s definition of <code>sign</code> (which also assigns <span class="math">$0$</span> to <span class="math">$0$</span>):</p>
<pre class='hljl'>
<span class='hljl-k'>if</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'><</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-t'>
</span><span class='hljl-oB'>-</span><span class='hljl-ni'>1</span><span class='hljl-t'>
</span><span class='hljl-k'>elseif</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>></span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-t'>
</span><span class='hljl-ni'>1</span><span class='hljl-t'>
</span><span class='hljl-k'>else</span><span class='hljl-t'>
</span><span class='hljl-ni'>0</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>
<p>The conditions for the <code>if</code> statements are expressions that evaluate to either <code>true</code> or <code>false</code>, such as generated by the Boolean operators <code><</code>, <code><=</code>, <code>==</code>, <code>!-</code>, <code>>=</code>, and <code>></code>.</p>
<p>If familiar with <code>if</code> conditions, they are natural to use. However, for simpler cases of "if-else" <code>Julia</code> provides the more convenient <em>ternary</em> operator: <code>cond ? if_true : if_false</code>. (The name comes from the fact that there are three arguments specified.) The ternary operator checks the condition and if true returns the first expression, whereas if the condition is false the second condition is returned. Both expressions are evaluated. (The <a href="http://julia.readthedocs.org/en/latest/manual/control-flow/#short-circuit-evaluation">short-circuit</a> operators can be used to avoid both evaluations.)</p>
<p>For example, here is one way to define an absolute value function:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>>=</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-t'> </span><span class='hljl-oB'>?</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>:</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-n'>x</span>
</pre>
<pre class="output">
f (generic function with 1 method)
</pre>
<p>The condition is <code>x >= 0</code>–-or is <code>x</code> non-negative? If so, the value <code>x</code> is used, otherwise <code>-x</code> is used.</p>
<p>Here is a means to implement a function which takes the larger of <code>x</code> or <code>10</code>:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>></span><span class='hljl-t'> </span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>?</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>:</span><span class='hljl-t'> </span><span class='hljl-nfB'>10.0</span>
</pre>
<pre class="output">
f (generic function with 1 method)
</pre>
<p>(This could also utilize the <code>max</code> function: <code>f(x) = max(x, 10.0)</code>.)</p>
<p>Or similarly, a function to represent a cell phone plan where the first 500 minutes are 20 dollars and every additional minute is 5 cents:</p>
<pre class='hljl'>
<span class='hljl-nf'>cellplan</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'><</span><span class='hljl-t'> </span><span class='hljl-ni'>500</span><span class='hljl-t'> </span><span class='hljl-oB'>?</span><span class='hljl-t'> </span><span class='hljl-nfB'>20.0</span><span class='hljl-t'> </span><span class='hljl-oB'>:</span><span class='hljl-t'> </span><span class='hljl-nfB'>20.0</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-nfB'>0.05</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-oB'>-</span><span class='hljl-ni'>500</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
cellplan (generic function with 1 method)
</pre>
<div class="alert alert-success" role="alert">
<div class="markdown"><p>Type stability. These last two definitions used <code>10.0</code> and <code>20.0</code> instead of the integers <code>10</code> and <code>20</code> for the answer. Why the extra typing? When <code>Julia</code> can predict the type of the output from the type of inputs, it can be more efficient. So when possible, we help out and ensure the output is always the same type.</p>
</div>
</div>
<h5>Example</h5>
<p>The <code>ternary</code> operator can be used to define an explicit domain. For example, a falling body might have height given by <span class="math">$h(t) = 10 - 16t^2$</span>. This model only applies for non-negative <span class="math">$t$</span> and non-negative <span class="math">$h$</span> values. So, in particular <span class="math">$0 \leq t \leq \sqrt{10/16}$</span>. To implement this function we might have:</p>
<pre class='hljl'>
<span class='hljl-nf'>h</span><span class='hljl-p'>(</span><span class='hljl-n'>t</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-t'> </span><span class='hljl-oB'><=</span><span class='hljl-t'> </span><span class='hljl-n'>t</span><span class='hljl-t'> </span><span class='hljl-oB'><=</span><span class='hljl-t'> </span><span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-ni'>10</span><span class='hljl-oB'>/</span><span class='hljl-ni'>16</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>?</span><span class='hljl-t'> </span><span class='hljl-nfB'>10.0</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-ni'>16</span><span class='hljl-n'>t</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>:</span><span class='hljl-t'> </span><span class='hljl-nf'>error</span><span class='hljl-p'>(</span><span class='hljl-s'>"t is not in the domain"</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
h (generic function with 1 method)
</pre>
<p>We might also have used <code>NaN</code> instead of an error, or <span class="math">$0$</span>, as the falling body would come to rest when it hits the ground.</p>
<h4>Nesting ternary operators</h4>
<p>The function <code>s(x)</code> isn't quite so easy to implement, as there isn't an "otherwise" case. We could use an <code>if</code> statement, but instead illustrate using a second, nested ternary operator:</p>
<pre class='hljl'>
<span class='hljl-nf'>s</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'><</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-t'> </span><span class='hljl-oB'>?</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-ni'>1</span><span class='hljl-t'> </span><span class='hljl-oB'>:</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>></span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-t'> </span><span class='hljl-oB'>?</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-t'> </span><span class='hljl-oB'>:</span><span class='hljl-t'> </span><span class='hljl-nf'>error</span><span class='hljl-p'>(</span><span class='hljl-s'>"0 is not in the domain"</span><span class='hljl-p'>))</span>
</pre>
<pre class="output">
s (generic function with 1 method)
</pre>
<p>With nested ternary operators, the advantage over the <code>if</code> condition is not very compelling, but for simple cases the ternary operator is quite useful. (The extra parentheses around the expression when <code>x<0</code> is not true are actually unnecessary, though added here for clarity.)</p>
<h2>Functions defined with the "function" keyword</h2>
<p>For more complicated functions, say one with a few steps to compute, an alternate form for defining a function can be used:</p>
<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>function_name</span><span class='hljl-p'>(</span><span class='hljl-n'>function_arguments</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-oB'>...</span><span class='hljl-n'>function_body</span><span class='hljl-oB'>...</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>
<p>The last value computed is returned unless the <code>function_body</code> contains an explicit <code>return</code> statement.</p>
<p>For example, the following is a more verbose way to define <span class="math">$f(x) = x^2$</span>:</p>
<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-k'>return</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>
<pre class="output">
f (generic function with 1 method)
</pre>
<p>The line <code>return x^2</code>, could have just been <code>x^2</code> as it is the last (and) only line evaluated.</p>
<div class="alert alert-info" role="alert">
<div class="markdown"><p>The <code>return</code> keyword is not a function, so is not called with parentheses. An emtpy <code>return</code> statement will return a value of <code>nothing</code>.</p>
</div>
</div>
<h5>Example</h5>
<p>Imagine we have the following complicated function related to the trajectory of a <a href="http://www.researchgate.net/publication/230963032_On_the_trajectories_of_projectiles_depicted_in_early_ballistic_woodcuts">projectile</a> with wind resistance:</p>
<p class="math">\[
~
f(x) = \left(\frac{g}{k v_0\cos(\theta)} + \tan(\theta) \right) x + \frac{g}{k^2}\ln\left(1 - \frac{k}{v_0\cos(\theta)} x \right)
~
\]</p>
<p>Here <span class="math">$g$</span> is the gravitational constant <span class="math">$9.8$</span> and <span class="math">$v_0$</span>, <span class="math">$\theta$</span> and <span class="math">$k$</span> parameters, which we take to be <span class="math">$200$</span>, <span class="math">$45$</span> degrees and <span class="math">$1/2$</span> respectively. With these values, the above function can be computed when <span class="math">$x=100$</span> with:</p>
<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>g</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>v0</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>theta</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>k</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nfB'>9.8</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>200</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>45</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>/</span><span class='hljl-ni'>2</span><span class='hljl-t'>
</span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>v0</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-nf'>cosd</span><span class='hljl-p'>(</span><span class='hljl-n'>theta</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-p'>(</span><span class='hljl-n'>g</span><span class='hljl-oB'>/</span><span class='hljl-p'>(</span><span class='hljl-n'>k</span><span class='hljl-oB'>*</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-nf'>tand</span><span class='hljl-p'>(</span><span class='hljl-n'>theta</span><span class='hljl-p'>))</span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-n'>g</span><span class='hljl-oB'>/</span><span class='hljl-n'>k</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-nf'>log</span><span class='hljl-p'>(</span><span class='hljl-ni'>1</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-n'>k</span><span class='hljl-oB'>/</span><span class='hljl-n'>a</span><span class='hljl-oB'>*</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>100</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
96.75771791632161
</pre>
<p>By using a multi-line function our work is much easier to look over for errors.</p>
<h2>Parameters, function context (scope), keyword arguments</h2>
<p>Consider two functions implementing the slope-intercept form and point-slope form of a line:</p>
<p class="math">\[
~
f(x) = m \cdot x + b, \quad g(x) = y_0 + m \cdot (x - x_0).
~
\]</p>
<p>Both functions use the variable <span class="math">$x$</span>, but there is no confusion, as we learn that this is just a dummy variable to be substituted for and so could have any name. Both also share a variable <span class="math">$m$</span> for a slope. Where does that value come from? In practice, there is a context that gives an answer. Despite the same name, there is no expectation that the slope will be the same for each function if the context is different. So when parameters are involved, a function involves a rule and a context to give specific values to the parameters.</p>
<p>Something similar is also true with <code>Julia</code>. Consider the example of writing a function to model a linear equation with slope <span class="math">$m=2$</span> and <span class="math">$y$</span>-intercept <span class="math">$3$</span>. A typical means to do this would be to define constants, and then use the familiar formula:</p>
<pre class='hljl'>
<span class='hljl-n'>m</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-t'>
</span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>m</span><span class='hljl-oB'>*</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>b</span>
</pre>
<pre class="output">
f (generic function with 1 method)
</pre>
<p>This will work as expected. For example, <span class="math">$f(0)$</span> will be <span class="math">$b$</span> and <span class="math">$f(2)$</span> will be <span class="math">$7$</span>:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>0</span><span class='hljl-p'>),</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
(3, 7)
</pre>
<p>All fine, but what if somewhere later the values for <span class="math">$m$</span> and <span class="math">$b$</span> were <em>redefined</em>:</p>
<pre class='hljl'>
<span class='hljl-n'>m</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span>
</pre>
<pre class="output">
(3, 2)
</pre>
<p>Now what happens with <span class="math">$f(0)$</span>? When <span class="math">$f$</span> was defined <code>b</code> was <span class="math">$3$</span>, but now if we were to call <code>f</code>, <code>b</code> is 2. Which value will we get? More generally, when <code>f</code> is being evaluated in what context does <code>Julia</code> look up the bindings for the variables it encounters? It could be that the values are assigned when the function is defined, or it could be that the values for the parameters are resolved when the function is called. If the latter, what context will be used?</p>
<p>Before discussing this, let's just see in this case:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>0</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
2
</pre>
<p>So the <code>b</code> is found from the currently stored value. This fact can be exploited. we can write template-like functions, such as <code>f(x)=mx+b</code> and reuse them just by updating the parameters separately.</p>
<p>How <code>Julia</code> resolves what a variable refers to is described in detail in the manual page <a href="http://julia.readthedocs.org/en/latest/manual/variables-and-scoping/">Scope of Variables</a>. In this case, the function definition finds variables in the context of where the function was defined, the main workspace. As seen, this context can be modified after the function definition and prior to the function call. It is only when <code>b</code> is needed, that the context is consulted, so the most recent binding is retrieved. Contexts (more formally known as environments) allow the user to repurpose variable names without there being name collision. For example, we typically use <code>x</code> as a function argument, and different contexts allow this <code>x</code> to refer to different values.</p>
<p>Mostly this works as expected, but at times it can be complicated to reason about. In our example, definitions of the parameters can be forgotten, or the same variable name may have been used for some other purpose. The potential issue is with the parameters, the value for <code>x</code> is straightforward, as it is passed into the function. However, we can also pass the parameters, such as <span class="math">$m$</span> and <span class="math">$b$</span>, as arguments. For parameters, we suggest using <a href="http://julia.readthedocs.org/en/latest/manual/functions/#keyword-arguments">keyword</a> arguments. These allow the specification of parameters, but also give a default value. This can make usage explicit, yet still convenient. For example, here is an alternate way of defining a line with parameters <code>m</code> and <code>b</code>:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>;</span><span class='hljl-t'> </span><span class='hljl-n'>m</span><span class='hljl-oB'>=</span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>b</span><span class='hljl-oB'>=</span><span class='hljl-ni'>0</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>m</span><span class='hljl-oB'>*</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>b</span>
</pre>
<pre class="output">
f (generic function with 1 method)
</pre>
<p>The right-hand side is identical to before, but the left hand side is different. Arguments defined <em>after</em> a semicolon are keyword arguments. They are specified as <code>var=value</code> (or <code>var::Type=value</code> to restrict the type) where the value is used as the default, should a value not be specified when the function is called.</p>
<p>Calling a function with keyword arguments can be identical to before:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>0</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
0
</pre>
<p>During this call, values for <code>m</code> and <code>b</code> are found from how the function is called, not the main workspace. In this case, nothing is specified so the defaults of <span class="math">$m=1$</span> and <span class="math">$b=0$</span> are used. Whereas, this call will use the user-specified values for <code>m</code> and <code>b</code>:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>0</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>m</span><span class='hljl-oB'>=</span><span class='hljl-ni'>3</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>b</span><span class='hljl-oB'>=</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
2
</pre>
<p>Keywords are used to mark the parameters whose values are to be changed from the default. Though one can use <em>positional arguments</em> for parameters–-and there are good reasons to do so–-using keyword arguments is a good practice if performance isn't paramount, as their usage is more explicit yet the defaults mean that a minimum amount of typing needs to be done.</p>
<h5>Example</h5>
<p>In the example for multi-line functions we hard coded many variables inside the body of the function. In practice it can be better to pass these in as parameters along the lines of:</p>
<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>;</span><span class='hljl-t'> </span><span class='hljl-n'>g</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nfB'>9.8</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>v0</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>200</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>theta</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>45</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>k</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>/</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>v0</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-nf'>cosd</span><span class='hljl-p'>(</span><span class='hljl-n'>theta</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-p'>(</span><span class='hljl-n'>g</span><span class='hljl-oB'>/</span><span class='hljl-p'>(</span><span class='hljl-n'>k</span><span class='hljl-oB'>*</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-nf'>tand</span><span class='hljl-p'>(</span><span class='hljl-n'>theta</span><span class='hljl-p'>))</span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-n'>g</span><span class='hljl-oB'>/</span><span class='hljl-n'>k</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-nf'>log</span><span class='hljl-p'>(</span><span class='hljl-ni'>1</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-n'>k</span><span class='hljl-oB'>/</span><span class='hljl-n'>a</span><span class='hljl-oB'>*</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>
<pre class="output">
f (generic function with 1 method)
</pre>
<h2>Multiple dispatch</h2>
<p>The concept of a function is of much more general use than its restriction to mathematical functions of single real variable. A natural application comes from describing basic properties of geometric objects. The following function definitions likely will cause no great concern when skimmed over:</p>
<pre class='hljl'>
<span class='hljl-nf'>Area</span><span class='hljl-p'>(</span><span class='hljl-n'>w</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>h</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>w</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-n'>h</span><span class='hljl-t'> </span><span class='hljl-cs'># of a rectangle</span><span class='hljl-t'>
</span><span class='hljl-nf'>Volume</span><span class='hljl-p'>(</span><span class='hljl-n'>r</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>h</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>pi</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-n'>r</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-n'>h</span><span class='hljl-t'> </span><span class='hljl-cs'># of a cylinder</span><span class='hljl-t'>
</span><span class='hljl-nf'>SurfaceArea</span><span class='hljl-p'>(</span><span class='hljl-n'>r</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>h</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>pi</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-n'>r</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-n'>r</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-n'>h</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>r</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-p'>))</span><span class='hljl-t'> </span><span class='hljl-cs'># of a right circular cone, including the base</span>
</pre>
<pre class="output">
SurfaceArea (generic function with 1 method)
</pre>
<p>The right-hand sides may or may not be familiar, but it should be reasonable to believe that if push came to shove, the formulas could be looked up. However, the left-hand sides are subtly different–-they have two arguments, not one. In <code>Julia</code> it is trivial to define functions with multiple arguments–-we just did.</p>
<p>Earlier we saw the <code>log</code> function can use a second argument to express the base. This function is basically defined by <code>log(b,x)=log(x)/log(b)</code>. The <code>log(x)</code> value is the natural log, and this definition just uses the change-of-base formula for logarithms.</p>
<p>But not so fast, on the left side is a function with two arguments and on the right side the functions have one argument–-yet they share the same name. How does <code>Julia</code> know which to use? <code>Julia</code> uses the number, order, and <em>type</em> of the arguments passed to a function to determine which function definition to use. This is technically known as <a href="http://en.wikipedia.org/wiki/Multiple_dispatch">multiple dispatch</a> or <strong>polymorphism</strong>. As a feature of the language, it can be used to greatly simplify the number of functions the user must learn. The basic idea is that many functions are "generic" in that they will work for many different scenarios.</p>
<div class="alert alert-success" role="alert">
<div class="markdown"><p>Multiple dispatch is very common in mathematics. For example, we learn different ways to add: integers (fingers, carrying), real numbers (align the decimal points), rational numbers (common denominators), complex numbers (add components), vectors (add components), polynomials (combine like monomials), ... yet we just use the same <code>+</code> notation for each operation. The concepts are related, the details different.</p>
</div>
</div>
<p><code>Julia</code> is similarly structured. <code>Julia</code> terminology would be to call the operation "<code>+</code>" a <em>generic function</em> and the different implementations <em>methods</em> of "<code>+</code>". This allows the user to just need to know a smaller collection of generic concepts yet still have the power of detail-specific implementations. To see how many different methods are defined in the base <code>Julia</code> language for the <code>+</code> operator, we can use the command <code>methods(+)</code>. As there are so many (<span class="math">$\approx 200$</span>) and that number is growing, we illustrate how many different logarithm methods are implemented for "numbers:"</p>
<pre class='hljl'>
<span class='hljl-nf'>methods</span><span class='hljl-p'>(</span><span class='hljl-n'>log</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-n'>Number</span><span class='hljl-p'>,))</span><span class='hljl-t'> </span><span class='hljl-oB'>|></span><span class='hljl-t'> </span><span class='hljl-n'>collect</span>
</pre>
15-element Array{Method,1}:<ul><li> log(a::<b>Float16</b>) in Base.Math at <a href="https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/math.jl#L1019" target="_blank">math.jl:1019</a><li> log(a::<b>Complex{Float16}</b>) in Base.Math at <a href="https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/math.jl#L1020" target="_blank">math.jl:1020</a><li> log(x::<b>Float64</b>) in Base.Math at <a href="https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/special/log.jl#L254" target="_blank">special/log.jl:254</a><li> log(x::<b>Float32</b>) in Base.Math at <a href="https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/special/log.jl#L290" target="_blank">special/log.jl:290</a><li> log(x::<b>BigFloat</b>) in Base.MPFR at <a href="https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/mpfr.jl#L671" target="_blank">mpfr.jl:671</a><li> log(::<b>Irrational{:ℯ}</b>) in Base.MathConstants at <a href="https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/mathconstants.jl#L95" target="_blank">mathconstants.jl:95</a><li> log(x::<b>SymPy.Sym</b>) in SymPy at <a href="https://github.com/JuliaPy/SymPy.jl/tree/bd87b5c0398627c0be2667d79778ffc5f58dc9fb//src/mathfuns.jl#L40" target="_blank">/Users/verzani/julia/SymPy/src/mathfuns.jl:40</a><li> log(x::<b>ImplicitEquations.OInterval</b>) in ImplicitEquations at <a href="https://github.com/jverzani/ImplicitEquations.jl/tree/8548e7c639d5b8bfbccb91391cdea616d1cbd6d6//src/intervals.jl#L217" target="_blank">/Users/verzani/julia/ImplicitEquations/src/intervals.jl:217</a><li> log(z::<b>Complex{T}</b>)<i> where T<:AbstractFloat</i> in Base at <a href="https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/complex.jl#L555" target="_blank">complex.jl:555</a><li> log(z::<b>Complex{T}</b>)<i> where T<:IntervalArithmetic.Interval</i> in IntervalArithmetic at <a href="file:///Users/verzani/.julia/packages/IntervalArithmetic/1T5or/src/intervals/complex.jl" target="_blank">/Users/verzani/.julia/packages/IntervalArithmetic/1T5or/src/intervals/complex.jl:101</a><li> log(z::<b>Complex</b>) in Base at <a href="https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/complex.jl#L574" target="_blank">complex.jl:574</a><li> log(a::<b>IntervalArithmetic.Interval{T}</b>)<i> where T</i> in IntervalArithmetic at <a href="file:///Users/verzani/.julia/packages/IntervalArithmetic/1T5or/src/intervals/functions.jl" target="_blank">/Users/verzani/.julia/packages/IntervalArithmetic/1T5or/src/intervals/functions.jl:299</a><li> log(xx::<b>IntervalArithmetic.DecoratedInterval{T}</b>)<i> where T</i> in IntervalArithmetic at <a href="file:///Users/verzani/.julia/packages/IntervalArithmetic/1T5or/src/decorations/functions.jl" target="_blank">/Users/verzani/.julia/packages/IntervalArithmetic/1T5or/src/decorations/functions.jl:336</a><li> log(d::<b>ForwardDiff.Dual{T,V,N} where N where V</b>)<i> where T</i> in ForwardDiff at <a href="file:///Users/verzani/.julia/packages/ForwardDiff/N0wMF/src/dual.jl" target="_blank">/Users/verzani/.julia/packages/ForwardDiff/N0wMF/src/dual.jl:202</a><li> log(x::<b>Real</b>) in Base.Math at <a href="https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/special/log.jl#L395" target="_blank">special/log.jl:395</a></ul>
<p>(The arguments have <em>type annotations</em> such as <code>x::Float64</code> or <code>x::BigFloat</code>. <code>Julia</code> uses these to help resolve which method should be called for a given set of arguments. This allows for different operations depending on the variable type. For example, in this case, the <code>log</code> function for <code>Float64</code> values uses a fast algorithm, whereas for <code>BigFloat</code> values an algorithm that can handle multiple precision is used.)</p>
<h5>Example. An application of composition and multiple dispatch</h5>
<p>As mentioned <code>Julia</code>'s multiple dispatch allows multiple functions with the same name. The function that gets selected depends not just on the type of the arguments, but also on the number of arguments given to the function. We can exploit this to simplify our tasks. For example, consider this optimization problem:</p>
<blockquote>
<p>For all rectangles of perimeter 20, what is the one with largest area?</p>
</blockquote>
<p>The start of this problem is to represent the area in terms of one variable. We see next that composition can simplify this task, which when done by hand requires a certain amount of algebra.</p>
<p>Representing the area of a rectangle in terms of two variables is easy, as the familiar formula of width times height applies:</p>
<pre class='hljl'>
<span class='hljl-nf'>Area</span><span class='hljl-p'>(</span><span class='hljl-n'>w</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>h</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>w</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-n'>h</span>
</pre>
<pre class="output">
Area (generic function with 1 method)
</pre>
<p>But the other fact about this problem–-that the perimeter is <span class="math">$20$</span>–-means that height depends on width. For this question, we can see that <span class="math">$P=2w + 2h$</span> so that–-as a function–-<code>h</code> depends on <code>w</code> as follows:</p>
<pre class='hljl'>
<span class='hljl-nf'>h</span><span class='hljl-p'>(</span><span class='hljl-n'>w</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-ni'>20</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-oB'>*</span><span class='hljl-n'>w</span><span class='hljl-p'>)</span><span class='hljl-oB'>/</span><span class='hljl-ni'>2</span>
</pre>
<pre class="output">
h (generic function with 1 method)
</pre>
<p>By hand we would substitute this last expression into that for the area and simplify (to get <span class="math">$A=w\cdot (20-2 \cdot w)/2 = -w^2 + 10$</span>). However, within <code>Julia</code> we can let <em>composition</em> do the substitution and leave the algebraic simplification for <code>Julia</code> to do:</p>
<pre class='hljl'>
<span class='hljl-nf'>Area</span><span class='hljl-p'>(</span><span class='hljl-n'>w</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>Area</span><span class='hljl-p'>(</span><span class='hljl-n'>w</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-nf'>h</span><span class='hljl-p'>(</span><span class='hljl-n'>w</span><span class='hljl-p'>))</span>
</pre>
<pre class="output">
Area (generic function with 2 methods)
</pre>
<p>This might seem odd, just like with <code>log</code>, we now have two <em>different</em> but related functions named <code>Area</code>. Julia will decide which to use based on the number of arguments when the function is called. This setup allows both to be used on the same line, as above. This usage style is not common with computer languages, but is a feature of <code>Julia</code> which is built around the concept of <em>generic</em> functions with multiple dispatch rules to decide which rule to call.</p>
<p>For example, jumping ahead a bit, the <code>plot</code> function of <code>Plots</code> (loaded with the accompanying <code>CalculusWithJulia</code> package) expects functions of a single numeric variable. Behind the scenes, then the function <code>A(w)</code> will be used in this graph:</p>
<pre class='hljl'>
<span class='hljl-k'>using</span><span class='hljl-t'> </span><span class='hljl-n'>CalculusWithJulia</span><span class='hljl-t'> </span><span class='hljl-cs'># loads the `Plots` package</span><span class='hljl-t'>
</span><span class='hljl-nf'>plot</span><span class='hljl-p'>(</span><span class='hljl-n'>Area</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>10</span><span class='hljl-p'>)</span>
</pre>
<div id="c79150be-8491-4ec4-abc0-594e1b6b4da3" style="width:576px;height:384px;"></div>
<script>
PLOT = document.getElementById('c79150be-8491-4ec4-abc0-594e1b6b4da3');
Plotly.plot(PLOT, [
{
"xaxis": "x1",
"colorbar": {
"title": ""
},
"yaxis": "y1",
"text": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
],
"x": [
0.04882909509894066,
0.09765819019788131,
0.5491536809408047,
1.0006491716837282,
1.2719791423699836,
1.5433091130562389,
1.772715150106948,
2.002121187157657,
2.2513204437440857,
2.500519700330514,
2.7302593728383977,
2.9599990453462817,
3.20630425470559,
3.4526094640648983,
3.7379782037251106,
4.023346943385323,
4.276949002247768,
4.530551061110215,
4.7529372132562075,
4.9753233654022,
5.217783965977698,
5.460244566553195,
5.704146177040657,
5.948047787528121,
6.217712206886082,
6.487376626244043,
6.743955516846238,
7.000534407448435,
7.267316921280971,
7.5340994351135055,
7.758673485890979,
7.983247536668451,
8.257426998266046,
8.53160645986364,
8.789343240575363,
9.047080021287085,
9.459994330723841,
9.872908640160599,
9.9364543200803
],
"showlegend": true,
"mode": "lines",
"name": "y1",
"zmin": -0.1,
"legendgroup": "y1",
"zmax": 0.1,
"line": {
"color": "rgba(0, 154, 250, 1.000)",
"shape": "linear",
"dash": "solid",
"width": 1
},
"y": [
0.4859066704612252,
0.9670447798660876,
5.189967044117212,
9.00519295204595,
11.101860485075555,
13.051288112119954,
14.584632497650782,
16.012722623510985,
17.44476069702079,
18.752598231564136,
19.848277485412055,
20.83839610501192,
21.78265557331273,
22.605582529298477,
23.4073009857271,
24.04614880700521,
24.477197254649504,
24.779617693775254,
24.93895997940639,
24.999391063704927,
24.952570144163026,
24.78817493895826,
24.50417816135903,
24.101205392563035,
23.517176981200627,
22.78771077170289,
21.95861915526157,
20.99786208461494,
19.85927397847298,
18.57834005295741,
17.389720598242114,
16.100234134961617,
14.389169350967455,
12.527755812649405,
10.640877805105807,
8.621143301298925,
5.108450569911196,
1.254761384648182,
0.631418745760548
],
"type": "scatter",
"hoverinfo": "text"
}
]
, {
"showlegend": true,
"xaxis": {
"showticklabels": true,
"gridwidth": 0.5,
"tickvals": [
0.0,
2.5,
5.0,
7.5,
10.0
],
"visible": true,
"ticks": "inside",
"range": [
-0.24779966165050013,
10.23308307682974
],
"domain": [
0.03769928064547487,
0.9931649168853894
],
"tickmode": "array",
"linecolor": "rgba(0, 0, 0, 1.000)",
"showgrid": true,
"title": "",
"mirror": false,
"tickangle": 0,
"showline": true,
"gridcolor": "rgba(0, 0, 0, 0.100)",
"titlefont": {
"color": "rgba(0, 0, 0, 1.000)",
"family": "sans-serif",
"size": 15
},
"tickcolor": "rgb(0, 0, 0)",
"ticktext": [
"0.0",
"2.5",
"5.0",
"7.5",
"10.0"
],
"zeroline": false,
"type": "-",
"tickfont": {
"color": "rgba(0, 0, 0, 1.000)",
"family": "sans-serif",
"size": 11
},
"zerolinecolor": "rgba(0, 0, 0, 1.000)",
"anchor": "y1"
},
"paper_bgcolor": "rgba(255, 255, 255, 1.000)",
"annotations": [],
"height": 384,
"margin": {
"l": 0,
"b": 20,
"r": 0,
"t": 20
},
"plot_bgcolor": "rgba(255, 255, 255, 1.000)",
"yaxis": {
"showticklabels": true,
"gridwidth": 0.5,
"tickvals": [
0.0,
5.0,
10.0,
15.0,
20.0,
25.0
],
"visible": true,
"ticks": "inside",
"range": [
-0.2494978613360858,
25.734795595502238
],
"domain": [
0.0391878098571012,
0.989747375328084
],
"tickmode": "array",
"linecolor": "rgba(0, 0, 0, 1.000)",
"showgrid": true,
"title": "",
"mirror": false,
"tickangle": 0,
"showline": true,
"gridcolor": "rgba(0, 0, 0, 0.100)",
"titlefont": {
"color": "rgba(0, 0, 0, 1.000)",
"family": "sans-serif",
"size": 15
},
"tickcolor": "rgb(0, 0, 0)",
"ticktext": [
"0",
"5",
"10",
"15",
"20",
"25"
],
"zeroline": false,
"type": "-",
"tickfont": {
"color": "rgba(0, 0, 0, 1.000)",
"family": "sans-serif",
"size": 11
},
"zerolinecolor": "rgba(0, 0, 0, 1.000)",
"anchor": "x1"
},
"legend": {
"tracegroupgap": 0,
"bordercolor": "rgba(0, 0, 0, 1.000)",
"bgcolor": "rgba(255, 255, 255, 1.000)",
"font": {
"color": "rgba(0, 0, 0, 1.000)",
"family": "sans-serif",
"size": 11
},
"y": 1.0,
"x": 1.0
},
"width": 576
}
);
</script>
<p>From the graph, we can see that that width for maximum area is <span class="math">$w=5$</span> and so <span class="math">$h=5$</span> as well.</p>
<h2>Anonymous functions</h2>
<p>Simple mathematical functions have a domain and range which are a subset of the real numbers, and generally have a concrete mathematical rule. However, the definition of a function is much more abstract. We've seen that functions for computer languages can be more complicated too, with, for example, the possibility of multiple input values. Things can get more abstract still.</p>
<p>Take for example, the idea of the shift of a function. The following mathematical definition of a new function <span class="math">$g$</span> related to a function <span class="math">$f$</span>:</p>
<p class="math">\[
~
g(x) = f(x-c)
~
\]</p>
<p>has an interpretation–-the graph of <span class="math">$g$</span> will be the same as the graph of <span class="math">$f$</span> shifted to the right by <span class="math">$c$</span> units. That is <span class="math">$g$</span> is a transformation of <span class="math">$f$</span>. From one perspective, the act of replacing <span class="math">$x$</span> with <span class="math">$x-c$</span> transforms a function into a new function. Mathematically, when we focus on transforming functions, the word <a href="http://en.wikipedia.org/wiki/Operator_%28mathematics%29">operator</a> is sometimes used. This concept of transforming a function can be viewed as a certain type of function, in an abstract enough way. The relation would be just pairs of functions <span class="math">$(f,g)$</span> where <span class="math">$g(x) = f(x-c)$</span>.</p>
<p>With <code>Julia</code> we can represent such operations. The simplest thing would be to do something like:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-n'>x</span><span class='hljl-t'>
</span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-ni'>3</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
g (generic function with 1 method)
</pre>
<p>Then <span class="math">$g$</span> has the graph of <span class="math">$f$</span> shifted by 3 units to the right. Now <code>f</code> above refers to something in the main workspace, in this example a specific function. Better would be to allow <code>f</code> to be an argument of a function. So we need to do something like:</p>
<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>shift_right</span><span class='hljl-p'>(</span><span class='hljl-n'>f</span><span class='hljl-p'>;</span><span class='hljl-t'> </span><span class='hljl-n'>c</span><span class='hljl-oB'>=</span><span class='hljl-ni'>0</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-k'>function</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-n'>c</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>
<pre class="output">
shift_right (generic function with 1 method)
</pre>
<p>That takes some parsing. In the body of the <code>shift_right</code> is the definition of a function. But this function has no name– it is <em>anonymous</em>. But what it does should be clear–-it subtracts <span class="math">$c$</span> from <span class="math">$x$</span> and evaluates <span class="math">$f$</span> at this new value. Since the last expression creates a function, this function is returned by <code>shift_right</code>.</p>
<p>So we could have done something more complicated like:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-n'>x</span><span class='hljl-t'>
</span><span class='hljl-n'>l</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>shift_right</span><span class='hljl-p'>(</span><span class='hljl-n'>f</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>c</span><span class='hljl-oB'>=</span><span class='hljl-ni'>3</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
#4 (generic function with 1 method)
</pre>
<p>Then <code>l</code> is a function that is derived from <code>f</code>.</p>