You want to get to a final result, which involves some values. This way, your program will be less likely to throw errors. It is very useful to know and understand what values are returned by functions, so we try to include this information wherever possible. How about the square or cube root of the number, or the circumference of a circle with a radius of length num? Next, we do a conditional test — if the entered value is not a number, we print an error message into the paragraph. At this point, we'd like you to have a go at writing out a couple of functions of your own and adding them to the library.
You've already met return values a number of times, although you may not have thought about them explicitly. Those values need to be calculated by a function, which then returns the results so they can be used in the next stage of the calculation. Standard Added more specific requirements for the string representation. We use the function to test whether the num value is not a number — if so, it returns true, and if not, false. Some functions don't return a significant value after completion, but others do, and it's important to understand what their values are, how to make use of them in your code, and how to make your own custom functions return useful values. Some functions don't return a return value as such in our reference pages, the return value is listed as void or undefined in such cases. The factorial function returns the of the given number.
Conclusion So there we have it — functions are fun, very useful and, although there's a lot to talk about in regards to their syntax and functionality, fairly understandable given the right articles to study. This is a little beyond the scope of this course, but worth studying before too long. Generally, a return value is used where the function is an intermediate step in a calculation of some kind. If the test returns false, the num value is a number, so we print out a sentence inside the paragraph element stating what the square, cube, and factorial of the number are. There's one last essential concept for us to discuss in this course, to close our look at functions — return values.
Enter the following event handler below the existing functions: input. Draft Standardizes native function string, line endings. As you go further into your programming career, you'll start to do the same kinds of things over and over again. When this anonymous function runs, the existing value entered into the input is stored in the num variable. Note: If you have trouble getting the example to work, feel free to check your code against the also , or ask us for help. We'll cover all of these below. Objective: To understand function return values, and how to make use of them.
If you'd like to contribute to the interactive examples project, please clone and send us a pull request. In the code above, we are saving this return value as the value of the newString variable. For user-defined objects, the toString method returns a string containing the source text segment which was used to define the function. Return values are just what they sound like — values returned by the function when it completes. It is generally a good idea to check that any necessary parameters have been provided, and in the right datatype, and if they are optional, that some kind of default value is provided to allow for that. When this function completes finishes running , it returns a value, which is a new string with the replacement made. If there is anything you didn't understand, feel free to read through the article again, or to ask for help.
. The decompiler was removed, so that the indentation parameter is not needed any more. Using return values in your own functions To return a value from a custom function, you need to use. The source for this interactive example is stored in a GitHub repository. We saw this in action recently in our example. The sentence calls the squared , cubed , and factorial functions to get the required values. .
. . . . . . .
. . . . . .
. . . . . .