Using functions in layout
Posted: 12 Nov 2015, 12:29
size=150Usage of out properties in functions/size
Common problem with functions is how to get result to global scope, when returned local variable is not between global variables and print (present) body of function is required
bThree ways are generally possible:/b
list
*1. Direct return/*:m
*2. Using structure which is returned/*:m
*3. Out property (new and recommended)/*:m/list:u
b1. Direct return/b
code
double Calculate_Area_first(double b, double h) {
double<m↑2â†,2> A = b*h;
return A;
}
double b = 0.5;
double h = 1;
Calculate_Area_first(b, h);
double A↓1↠= Calculate_Area_first(b, h);
/code
+: Function is simple.
-: Function have to be calculated twice. First for display second for assign. 2 rows in script, assign have to be set invisible in layout
-: The slowest, about 60 μs in this example
b2. Using structure which is returned/b
code
void Calculate_Area_second(double b, double h) {
double<m↑2â†,2> A = b*h;
Result.Set("A", A);
}
double b = 0.6;
double h = 1;
object Result = new Structure();
Calculate_Area_second(b, h);
double A↓2↠= Result.A;
/code
+: Function is calculated just once
-: Global object Result have to exist, 3 rows in script, potencially dangerous - modification, Assign have to be set invisible in layout
-: Slow, about 30 μs in this example
b3. Out property/b
code
void Calculate_Area_third(double b, double h, out double A) {
double<m↑2â†,2> A = b*h;
}
double b = 0.7;
double h = 1;
Calculate_Area_third(b, h, out double A↓3â†);
/code
+: Function is calculated just once, function is simple, only one row, assign is done together with calling the function
+: The fastest, about 18 μs in this example
bSummary /b
First type is recommended for functions without visible layout output. Therefore they are used only for assign.
Second type is recommended for functions which somehow modify already existed array or structure. It is potencially dangerous, because something is changed without any notification.
Third type is recommended for functions which are displayed in layout and which return some values to global variables.
Common problem with functions is how to get result to global scope, when returned local variable is not between global variables and print (present) body of function is required
bThree ways are generally possible:/b
list
*1. Direct return/*:m
*2. Using structure which is returned/*:m
*3. Out property (new and recommended)/*:m/list:u
b1. Direct return/b
code
double Calculate_Area_first(double b, double h) {
double<m↑2â†,2> A = b*h;
return A;
}
double b = 0.5;
double h = 1;
Calculate_Area_first(b, h);
double A↓1↠= Calculate_Area_first(b, h);
/code
+: Function is simple.
-: Function have to be calculated twice. First for display second for assign. 2 rows in script, assign have to be set invisible in layout
-: The slowest, about 60 μs in this example
b2. Using structure which is returned/b
code
void Calculate_Area_second(double b, double h) {
double<m↑2â†,2> A = b*h;
Result.Set("A", A);
}
double b = 0.6;
double h = 1;
object Result = new Structure();
Calculate_Area_second(b, h);
double A↓2↠= Result.A;
/code
+: Function is calculated just once
-: Global object Result have to exist, 3 rows in script, potencially dangerous - modification, Assign have to be set invisible in layout
-: Slow, about 30 μs in this example
b3. Out property/b
code
void Calculate_Area_third(double b, double h, out double A) {
double<m↑2â†,2> A = b*h;
}
double b = 0.7;
double h = 1;
Calculate_Area_third(b, h, out double A↓3â†);
/code
+: Function is calculated just once, function is simple, only one row, assign is done together with calling the function
+: The fastest, about 18 μs in this example
bSummary /b
First type is recommended for functions without visible layout output. Therefore they are used only for assign.
Second type is recommended for functions which somehow modify already existed array or structure. It is potencially dangerous, because something is changed without any notification.
Third type is recommended for functions which are displayed in layout and which return some values to global variables.