Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at email@example.com. That is, contact me for the slightest problem you have about what you are reading.
A function definition consists of the following in the order given
– The reserved word, function.
– The name of the function.
– A list of parameters to the function, enclosed in parentheses and separated by commas (see below).
– The statements that define the function, enclosed in curly braces. The statements in a function can have among them calls to other functions defined in the current program (application).
Note: another name for reserved word is keyword.
In the following example, we define a function that will add two numbers, find the square of the sum and then return the result.
The function begins with the reserved word, function. The name of the function is myFn. This is followed by parentheses. Then you have the block. In the block, you have the declaration and assignment of the two numbers. The third statement in the block sums the two numbers. The fourth statement squares the sum. The last statement returns the square to the statement that would call the function, outside the function. The reserved word, return, is used for this. It is followed by a variable or a literal. Not all functions end with the return instruction. Some functions just perform a task and do not return anything.
Calling a Function
You call a function by just typing the name of the function, followed by parentheses, in a statement. The following code illustrates this. Try it:
This code is similar to the previous with the addition of two last statements. The last-but-one statement calls the function. This statement is outside the function. The right operand of the statement is “myFn()”. It is this expression that calls the function. When it calls the function, it receives the value returned by the return statement in the function. This value is now assigned to the variable, result. The last statement displays the result.
A function call does not always have to assign a return value to a variable. Functions that do not have return values are called by just typing the name, followed by parentheses (then semicolon, to form a statement).
Parameters and Arguments
Now in the above function we can only deal with two particular numbers, which are 2 and 3. This is a disadvantage. If we declare and assign the variables outside the function, then we can always change the values of the variables, then send the variables to the function before the function is executed. In this way, we shall be able to deal with many other pairs of numbers. The following example illustrates this:
This time the variables have been declared and assigned outside the function. Some other function elsewhere in the code can actually change these values. However, a function cannot change the value of a variable inside some other function. In the definition of the function, the parentheses now have two variables. These variables in this position are called Parameters. These parameters of the function are used within the function.
In the last-but-one statement, where the function is called; the parentheses have two variables. These variables in this position are called Arguments. These arguments of the function are the variables declared outside the function. The arguments to a function call, can be literals, something like:
result = myFn(4, 5);
Read the above code and try it.
It is advisable to always make the variables for the parameters different from the corresponding variables for the arguments. If you do not do this, then while manipulating the parameters within the function, you might change the values of the variables outside the function.
The parseInt function is used to convert an integer in the form of a string to a correct integer. “25” for example, is a string, whose content is an integer. After the conversion, you have the returned value that you can use in arithmetic operations. “25” as it is, is a string and cannot be used in arithmetic operations. Read and try the following code:
parseInt truncates off the decimal part; it does not round the number.
This is the second top-level function we shall look at. It converts a float number, which is in the form of a string to a proper float number. After the conversion, you have the returned value that you can use in arithmetic operations. “32.45” as it is, is a string and cannot be used in arithmetic operations. Read and try the following code:
The alert argument can also be an expression.
We have come to the end of this part of the series. Rendezvous in the next part.
To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (use menu if available):
Comparison and Arithmetic Operators