Flash / Flex / ActionScript/Function/return
Содержание
- 1 Assign value returned from a function to a variable
- 2 Defining a Return Type for Your Function
- 3 If you attempt to actually return a value in a void method, the compiler generates an error.
- 4 Obtaining the Result of a Method: use a return statement that specifies the value to return.
- 5 Returning void: If you don"t need your function to return anything, simply use a return type of void.
- 6 Return value from function
- 7 The return statement exits the current method
- 8 Use a return statement to exit a method under certain conditions
- 9 Use the return value of a method, without storing it in a variable, by passing it as a parameter to another function
Assign value returned from a function to a variable
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var color:String = getColor();
trace(color); // Displays: Red
}
function getColor():String {
var color:String = "Red";
return color;
color = "Blue";
return color;
}
}
}
Defining a Return Type for Your Function
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var myObject:* = createObject("Array");
myObject.push(1, 2, 3, 4, 5);
trace(myObject); //Displays: 1, 2, 3, 4, 5
}
function createObject(type:String):* {
switch (type) {
case "Boolean": return new Boolean ();
case "Number": return new Number ();
case "String": return new String ();
case "Array": return new Array ();
default: trace("Unknown type"); return null;
}
}
}
}
If you attempt to actually return a value in a void method, the compiler generates an error.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
}
private function sampleMethod ( ):void {
return "some value"; // This causes the compiler to generate an error.
}
}
}
Obtaining the Result of a Method: use a return statement that specifies the value to return.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var playerScore:Number = average(6, 10);
trace("The player"s average score is " + playerScore);
}
private function average (a:Number, b:Number):Number {
return (a + b)/2;
}
}
}
Returning void: If you don"t need your function to return anything, simply use a return type of void.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
doNothing();
}
function doNothing ():void {
}
}
}
Return value from function
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace(getCircumference(25)); // Displays: 78.53981633974483
}
function getCircumference (diameter:Number):Number {
var circumference:Number = diameter * Math.PI;
return circumference;
}
}
}
The return statement exits the current method
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
sampleFunction( );
}
private function sampleFunction ( ):void {
return;
trace("Never called");
}
}
}
Use a return statement to exit a method under certain conditions
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
checkPassword("M");
checkPassword("S");
}
private function checkPassword (password:String):void {
if (password != "S") {
return;
}
trace ("T");
}
}
}
Use the return value of a method, without storing it in a variable, by passing it as a parameter to another function
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace("The player"s average score is " + average(6, 10));
}
private function average (a:Number, b:Number):Number {
return (a + b)/2;
}
}
}