A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string. The type of value your function returns depends largely on the task it performs. Show
You use the Function Returns edit combo box in the General page of the New Script dialog to tell JAWS the type of value the function returns. You also type the description of the return in the Return description edit box. Adding a description for the return, helps you and anyone else using your function to determine exactly what the value should be used for within the calling script or function. When you create a new function that returns a string value, the Script Manager places the following function beginning line into your script file: String Function MyFunction () The "string" key word that precedes the "function" key word tells you that the MyFunction function returns a string value to the calling script or user-defined function. The Return StatementYou use the return statement to send a value back to the calling script or user-defined function. This key word tells JAWS to return the specified value to the calling script or function. You can return the value as a literal value or within a variable. The syntax of a return statement that sends a string value stored in a variable called sText back to the calling script or user-defined function follows: Return sText A return statement that returns a string of text, "This is a string", as a literal follows: Return "this is a string" When a function returns a value to the calling script or user-defined function, you must store that value in either a local or global variable. You can then use that variable to make decisions on what the calling script or user-defined function should do next. An example of storing a string value returned by a function called MyFunction in a local variable follows: Example 1: Assigning the Output of a Function to a VariableScript MyScript () You can also use the return value of one function as the parameter for another. Using the previous example, the return value from MyFunction could be passed directly to the SayFormattedMessage function without storing it in a local variable first. The only disadvantage to this approach is that the MyFunction function may not retrieve any text. Therefore, the SayFormattedMessage function won't cause JAWS to speak anything. The example reworked to use the return value of MyFunction as the parameter for SayFormattedMessage follows: Example 2: Using a Function as a Parameter For Another FunctionScript MyScript () Another example of a user-defined function that returns a value follows: string function VerbosityLevelToggle(int iRetCurVal) You can find the function shown above in the default script file. JAWS performs this function each time you press SPACEBAR on the "User Verbosity" entry in the Adjust JAWS Options dialog. The function begins by determining the value of the iRetCurVal parameter. If the value of the parameter is 0 or false, then JAWS performs the VerbosityLevel function to change the verbosity level from its current value to the next value. For example, if you are using the beginner verbosity level, the VerbosityLevel function changes your verbosity level to intermediate. Next, the GetVerbosity function retrieves the verbosity setting now being used and stores it in the Verbosity local variable. The If statement then determines the value stored in the Verbosity variable and returns the appropriate string of text to indicate the new verbosity level. The actual text is stored in the Common.jsm JAWS message file found in your JAWS settings folder. What are Functions?
Predefined FunctionsUsing predefined functions:
Rogue Wave C++ Standard Library Class Reference Microsoft MSDN Library - Standard C++ Library Reference Microsoft MSDN Library - C/C++ Languages User-Defined FunctionsUsing User-Defined functions:
Mnemonic: "ProDeCall":
//Value-returning function definition syntax: including header and body functionType functionName(formal parameter list) //function header { //function body statements... //value-returning function return statement return expression; }
//function prototype syntax: only data types (not names) of parameters must be specified functionType functionName(formal parameter list);
Mnemonic: "TON" - (must agree in Type, Order, and Number) //function call syntax: x = functionName(actual parameter list); //Assignment, Output, Argument in another function call x = functionName(); //Assignment, Output, Argument in another function call //return statement syntax: return expression; Flow of ExecutionProgram Execution:
/*
This program illustrates value-returning functions
and compares two, then three numbers.
written by Mark K. Jowett, Ph.D.
6:03 PM 9/11/2004
*/
#include<iostream>
//using namespace std;
//function prototypes
double testTwo(double x, double y);
double testThree(double x, double y, double z);
int main()
{
double num1, num2;
//value-returning function used in output
std::cout << "The larger of 5 and 10 is "
<< testTwo(5, 10) << std::endl; //function call
std::cout << "Enter one number: ";
std::cin >> num1;
std::cout << "Enter another number: ";
std::cin >> num2;
std::cout << std::endl;
//value-returning function used in output
std::cout << "The larger of " << num1 << " and "
<< num2 << " is " << testTwo(num1, num2) << std::endl; //function call
//value-returning function used in output
std::cout << "The largest of 10, 15, and 20 is "
<< testThree(10, 15, 20) << std::endl; //function call
std::cout << "Press Enter key to exit...";
std::cin.get(); std::cin.get(); // make DOS window stay open
return 0;
}
//function definition
double testTwo(double x, double y)
{
if (x >= y)
return x;
else
return y;
}
//function definition
double testThree (double x, double y, double z)
{
//value-returning function used as argument in another function call
return testTwo(x, testTwo(y, z));
}
Displays:
The larger of 5 and 10 is 10
Enter one number: 2
Enter another number: 3
The larger of 2 and 3 is 3
The largest of 10, 15, and 20 is 20
Press Enter key to exit...
What is the first line in a function called?The first line of the function definition is called the header; the rest is called the body. The header has to end with a colon and the body has to be indented. By convention, the indentation is always four spaces (see Section 3.13). The body can contain any number of statements.
Does the function header marks the beginning of the function definition?True. The function header marks the beginning of the function definition. True. A function definition specifies what a function does and causes the function to execute.
What does the following statement mean num1 num2 Get_num ()?num1, num2 = get_num() a. The function get_num() is expected to return a value each for num1 and num2.
When a function is called by its name during the execution of a program then it is?When a function is called by its name during the execution of a program, then it is. executed.
|