Declaring and calling functions

Before we can use a function to do anything, we have to declare it. So let’s start by looking at how functions are declared. Make a new folder and name it “Functions”. Then make a new file and name it “Functions.jspp”. Write in the following code:

external $;

string getFavoriteAnimalString(string animal) {
    return "My favorite animal is the " + animal;
}

Save Functions.jspp to your Functions folder. The code we have written declares a function named getFavoriteAnimalString. This name is appropriate, because the function’s purpose is to return a string which states one’s favourite animal. That task is accomplished by the return statement in the function’s body (the part within the curly braces): the return statement evaluates the expression to the right of the return keyword, and then sends the evaluated expression back to the function’s caller (where it might be assigned to a variable, for example). Since the purpose of getFavoriteAnimalString is to return a string, we can say that the function’s return type is string, and we specify this by writing string to left of the function’s name.

Just as the output of getFavoriteAnimalString will be a string, it also takes a string input. To compose the string which reports one’s favourite animal, the function needs to know which particular animal is one’s favourite. It receives this input information via the string parameter named animal, which we write inside the parentheses to the right of the function’s name. When at a later stage of our program we call getFavoriteAnimalString to get it to execute, we will pass a particular string into its input parameter: the particular string we pass it will be the function’s argument.

Note: The distinction between a function’s parameter(s) and its argument(s) can be confusing. To clarify, a parameter is a variable written when declaring a function. The parameter type specifies what type of input the function takes. An argument, by contrast, is the actual value passed to a function as input when the function is called. The argument becomes the value of the parameter variable.

Before we call getFavoriteAnimalString, let’s set up an HTML document which we will use to display the result. Make a second file named “Functions.html” and write in the following:

<!DOCTYPE html>
<title>Functions program</title>
<body>
<p id="content"></p>
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="Functions.jspp.js"></script>
</body>
</html>

Save Functions.html to your Functions folder. Now go back to Functions.jspp and write in the following, below the code where you declared getFavoriteAnimalString:

string favoriteAnimalString = getFavoriteAnimalString("cat");
$("#content").text(favoriteAnimalString);

On the first of these new lines, we call getFavoriteAnimalString, passing it the string “cat” as its argument. This function call returns the string “My favorite animal is the cat”, and we assign this string to the variable favoriteAnimalString. On the second line, we use jQuery to select the “content” element of Functions.html and to set its text to the value of favoriteAnimalString. Compile Functions.jspp and then open Functions.html in a browser. If everything has worked, your document should display “My favorite animal is the cat”.

JS++ | Functions

A function is a section of code which contains a set of instructions, where the instructions describe how a particular task is to be accomplished. Functions are declared and may then be called one or more times. Declaring a function involves specifying the instructions that the function will contain. When a function is called, it executes those instructions. Functions are fundamental to computer programming in general, and they play a central role in JS++.

Note: This tutorial does not cover external functions in JS++. An external function is declared with the function keyword and returns an external type. We will examine external functions and external types in Chapter 9. (To preview, an external type is one which isn’t a JS++ type; usually, these will be JavaScript types.) This tutorial examines internal JS++ functions. Internal functions are not declared using the function keyword, and can return any type.

Similar Reads

Declaring and calling functions

Before we can use a function to do anything, we have to declare it. So let’s start by looking at how functions are declared. Make a new folder and name it “Functions”. Then make a new file and name it “Functions.jspp”. Write in the following code:...

Returning from a function

In our example, getFavoriteAnimalString returns a string. In general, however, a JS++ function can return any valid type, so long as the value returned belongs to the return type specified to the left of the function’s name in the declaration....

Parameters

Functions can take zero, one, or multiple parameters. If a function takes multiple parameters, then those parameters can be of the same type or different types:...

Recursive functions

A recursive function is one that may call itself during its execution. Here is a well-known example of a recursive function:...

Overloading

JS++ allows functions to be overloaded. Overloading consists in declaring two functions with the same name, but with different parameter lists. For example, we might overload a function named displayFavoriteAnimalString as follows:...

Callbacks and function expressions

Consider the following code:...