2.6 Names
Defining a function involves assigning names, not only for the function itself, but also for its parameters.
In Julia, there are a few restrictions regarding names: they must begin with a letter, the underscore character, or a Unicode symbol (in practice, only a subset of Unicode is allowed). The remaining characters can also include !, digits and many more Unicode symbols. However, note that it is not possible to use names that are already reserved by the language. We will see some of them soon.
Pragmatically speaking, the creation of names should take some rules into consideration:
Avoid using digits, if possible;
For portability reasons, accented characters should be avoided;
If a name has more than one word, those words should be concatenated or, when the result is difficult to understand, the names should be separated by an underscore (_). For example, a function that computes the area of a circle might be named circle_area.
The choice of names will have a significant impact on the program’s legibility. Let us consider for example the area \(A\) of a triangle with base \(b\) and height \(c\), which can be defined mathematically by:
\[A(b,c) = \frac{b \cdot c}{2}\]
In Julia we will have:
A(b, c) = (b*c)/2
Note that the Julia definition is identical to the corresponding mathematical expression. However, if we did not know beforehand what the purpose of this function was, we would hardly understand it just by looking at its name and/or at the names of the parameters. Therefore, and contrary to mathematics, the names that we assign in Julia should have a clear meaning. Instead of writing A, it is preferable that we write triangle_area and, instead of writing b and c, we should write base and height, respectively. Taking these aspects into consideration, we can present a more meaningful definition:
triangle_area(base, height) = (base*height)/2
As the number of definitions grow, names become particularly important for the reader to quickly understand the written program, so it is crucial that names are carefully chosen.
2.6.1 Exercises 4
2.6.1.1 Question 6
Function that calculates the volume of a sphere;
Function that tests if a number is a prime number;
Function that converts a measurement in centimeters into inches.
2.6.1.2 Question 7
Define the function radians_from_degrees that receives an angle in degrees and computes the corresponding value in radians. Note that \(180\) degrees are \(pi\) radians.
2.6.1.3 Question 8
Define the function degrees_from_radians that receives an angle in radians and computes the corresponding value in degrees.
2.6.1.4 Question 9
Define a function that calculates the perimeter of a circle given its radius.
2.6.1.5 Question 10
Define a function that calculates the volume of a parallelepiped from its length, width and height.
2.6.1.6 Question 11
Define a function that calculates the volume of a cylinder from its height and base radius. The volume corresponds to multiplying the area of the base by the cylinder’s height.
2.6.1.7 Question 12
Define a function average that calculates the average value between two numbers. For example: average(2, 3) \(\rightarrow\) 2.5.