Core Function UnsetFunction
From Sputnik Wiki
(Difference between revisions)
(Created page with "<pre> UnsetFunction( <function-name> ) </pre> === Description === Delete a previous user defined function from Sputnik. ==== function-name ==== The function name to delete. ...") |
(→Example) |
||
Line 24: | Line 24: | ||
=== Example === | === Example === | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | // Define a function called Add2 | ||
+ | Function Add2($a, $b) | ||
+ | { | ||
+ | return $a + $b; | ||
+ | } | ||
+ | |||
+ | // Call the function | ||
+ | println("1+2 Is: " . Add2(1, 2) ); | ||
+ | |||
+ | // Now delete the function from Sputnik | ||
+ | UnsetFunction("Add2"); | ||
+ | |||
+ | // Try call the function again | ||
+ | println("Lets try call it again"); | ||
+ | println("1+2 Is: " . Add2(1, 2) ); // Notice the crash here? | ||
+ | // It crashes above because the funciton "Add2" no longer exists within Sputnik | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Heres an example loading the function from a String instead of hard coding it | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> |
Revision as of 16:14, 19 April 2012
UnsetFunction( <function-name> )
Contents |
Description
Delete a previous user defined function from Sputnik.
function-name
The function name to delete.
Return Value
Success: Returns 1.
Failure: Returns 0.
Remarks
This function is used to remove a previous added function to Sputnik for example if you have a plugin system that adds a series of functions you could make the plugin remove all functions when a command is typed.
Then you could load the plugin again basically allowing an update to your programs source code while it is still running.
Example
// Define a function called Add2 Function Add2($a, $b) { return $a + $b; } // Call the function println("1+2 Is: " . Add2(1, 2) ); // Now delete the function from Sputnik UnsetFunction("Add2"); // Try call the function again println("Lets try call it again"); println("1+2 Is: " . Add2(1, 2) ); // Notice the crash here? // It crashes above because the funciton "Add2" no longer exists within Sputnik
Heres an example loading the function from a String instead of hard coding it
// Make a string containing text of a FUNCTION to load this string as Sputnik code later my $Function = @" Function Add2($a, $b) { return $a + $b; } "; // Load the function for the first time // This will view the string as a peice of Sputnik code (same as include()) Eval($Function); // Bascially it will add the funciton "Add2" to Sputnik // Call the function println("1+2 Is: " . Add2(1, 2) ); // Now delete the function from Sputnik UnsetFunction("Add2"); // Try call the function again println("Lets try call it again"); println("1+2 Is: " . Add2(1, 2) ); // Notice the crash here? // It crashes above because the funciton "Add2" no longer exists within Sputnik