Difference between revisions of "Call"

From Kolmafia
Jump to navigation Jump to search
imported>Bale
(http://kolmafia.us/showthread.php?1349-Can-I-sort-an-array-Can-I-call-a-method-if-given-a-reference&p=9021&viewfull=1#post9021)
(Fix formatting, add Scope section)
Line 1: Line 1:
 
{{DISPLAYTITLE:call}}
 
{{DISPLAYTITLE:call}}
Syntax: call [''type''] <identifier>( param, ... );
+
Syntax: <code>call [''type''] &lt;identifier&gt;( param, ... );</code>
 
: ''type'' is an optional parameter which must match the return value of the function. If not specified, ''type'' defaults to void.
 
: ''type'' is an optional parameter which must match the return value of the function. If not specified, ''type'' defaults to void.
  
For instance:
+
For instance, the following ASH code:
<div style="margin-bottom: 1em; border: dashed 1px green; padding: 1em; margin:0px 20px;"><syntaxhighlight>
+
 
int foo( int a, int b )
+
<syntaxhighlight lang="d" line highlight="6,10">
{
+
int foo( int a, int b ) {
  return a + b;
+
  return a + b;
 
}
 
}
  
 
String a = "foo";
 
String a = "foo";
 
 
float c = call float a( 2, 3 );
 
float c = call float a( 2, 3 );
 
 
print( "c = " + c );
 
print( "c = " + c );
  
 
a = "print";
 
a = "print";
 
 
call a( "print 1" );
 
call a( "print 1" );
 +
print( "done" );
 +
</syntaxhighlight>
  
print( "done" );
 
</syntaxhighlight></div>
 
 
will output the following to the CLI:
 
will output the following to the CLI:
c = 5.0
+
 
print 1
+
<pre>
done
+
c = 5.0
{{Format}}
+
print 1
 +
done
 +
</pre>
 +
 
 +
== Scope ==
 +
 
 +
<code>call</code> can only execute functions that are ''visible in the scope of the <code>call</code> statement''.
 +
 
 +
For example, executing the following ASH code will result in an error, because the <code>func_inside_bar()</code> function is not visible from within <code>foo()</code>:
 +
 
 +
<syntaxhighlight lang="d" line highlight="3">
 +
void foo( string callback ) {
 +
  // foo() cannot see func_inside_bar()
 +
  call void callback( 123 );
 +
}
 +
 
 +
void bar() {
 +
  void func_inside_bar( int arg ) {
 +
      print( arg );
 +
  }
 +
  foo( "func_inside_bar" );
 +
}
 +
 
 +
bar(); // Error: Function 'func_inside_bar( int )' undefined.
 +
</syntaxhighlight>
 +
 
 +
However, the following code will run successfully:
 +
 
 +
<syntaxhighlight lang="d" line highlight="5">
 +
void foo( string callback ) {
 +
  void func_inside_foo( int arg ) {
 +
      print( arg );
 +
  }
 +
  call void callback( 123 );
 +
}
 +
 
 +
void bar() {
 +
  // Although bar() cannot see func_inside_foo(),
 +
  // it can still pass its name to foo()
 +
  foo( "func_inside_foo" );
 +
}
 +
 
 +
bar(); // Prints "123"
 +
</syntaxhighlight>
  
 
[[Category:Scripting]]
 
[[Category:Scripting]]

Revision as of 06:49, 23 February 2021

Syntax: call [type] <identifier>( param, ... );

type is an optional parameter which must match the return value of the function. If not specified, type defaults to void.

For instance, the following ASH code:

 1 int foo( int a, int b ) {
 2    return a + b;
 3 }
 4 
 5 String a = "foo";
 6 float c = call float a( 2, 3 );
 7 print( "c = " + c );
 8 
 9 a = "print";
10 call a( "print 1" );
11 print( "done" );

will output the following to the CLI:

c = 5.0
print 1
done

Scope

call can only execute functions that are visible in the scope of the call statement.

For example, executing the following ASH code will result in an error, because the func_inside_bar() function is not visible from within foo():

 1 void foo( string callback ) {
 2    // foo() cannot see func_inside_bar()
 3    call void callback( 123 );
 4 }
 5 
 6 void bar() {
 7    void func_inside_bar( int arg ) {
 8       print( arg );
 9    }
10    foo( "func_inside_bar" );
11 }
12 
13 bar(); // Error: Function 'func_inside_bar( int )' undefined.

However, the following code will run successfully:

 1 void foo( string callback ) {
 2    void func_inside_foo( int arg ) {
 3       print( arg );
 4    }
 5    call void callback( 123 );
 6 }
 7 
 8 void bar() {
 9    // Although bar() cannot see func_inside_foo(),
10    // it can still pass its name to foo()
11    foo( "func_inside_foo" );
12 }
13 
14 bar(); // Prints "123"