User-Defined Functions
User-defined functions are
blocks of
code that can be executed just like a built-in "
ll" function. They are used to improve readability and to reduce the amount of redundant code in a
script. If you copy and paste a section of code more then once in a script, that code should be put into a function.
Functions can
return (give) a
value back to the caller that is any valid
LSL type. The
return keyword is used to do this.
Functions are declared (defined, created) above the default
state, either before or after
global variables.
The format for declaring a function is as follows:
type functionName(type parameter1, type parameter2, type parameter3) {
// code goes here:
}
Where
type is any valid LSL type or nothing (which specifies that the function returns nothing),
functionName is any combination of letters and numbers that starts with a letter (A-Z, a-z), $ or _. The parenthesis after the function name are where the parameters of the function are defined. Parameters follow the same naming conventions
variables do, and are seperated by commas. Parameters are variables with a
scope encompassing the entire function; they can be used just like variables anywhere inside the function. Functions without parameters simply have nothing between the open and closed parenthesis (the parenthesis are still required).
Example usage of a function:
float getAbsoluteValueOf(float value) {
if (value < 0)
return value * -1;
return value;
}
default {
state_entry() {
float myFloat = 4.392;
float absFloat = getAbsoluteValueOf(myFloat);
llSay(0, "The absolute value of " + (string) myFloat
+ " is " + (string) absFloat);
}
}
getAbsoluteValueOf has the same behavior as the function
llFabs has, and can be used in the same way.
Here's an example of a function without parameters or a return value:
saySomething() {
llSay(0, "Something");
}
default {
state_entry() {
saySomething();
}
}
This script makes the
object say "Something" out loud, on
channel 0. As you can see, functions can call other functions;
saySomething can call
llSay.
User-defined functions can call other
user-defined functions as well:
sayOutloud(string message) {
llSay(0, message);
}
saySomething() {
sayOutloud("Something");
}
default {
state_entry() {
saySomething();
}
}
User-defined functions can also call themselves. This is a special programming aspect called
recursion.
subtractTillTwo(integer value) {
if (value > 2) {
subtractTillTwo(value - 1);
} else if (value == 2) {
llSay(0, "Value is two!");
}
}
default {
state_entry() {
subtractTillTwo(35);
}
}
llPizza, added originally as a joke, is another practical example of a user-defined function.
Functions Jasa SEO Jasa SEO Murah Sepatu Online Toko Sepatu Online Sepatu Sepatu Murah Sepatu Safety Sepatu Futsal Cheapes Hostgator Coupon Link Booking Televisori offerte Notebook Offerte Berita Terkini Internet Marketer Muda Internet Marketer Indonesia Portatile Apple RDAnet Lorks Karikatur Bisnis Modal Kecil Bisnis UKM Berita Terbaru Iklan Baris Jasa SEO Jasa SEO Murah SEO Indonesia Konsultan SEO SEO Belajar SEO Kursus SEO Kursus SEO Murah Jam Tangan Casio Jam Tangan Casio Jam Tangan Murah Jam Tangan Grosir Baju Terbaru Grosir Baju Baju Terbaru Grosir Baju Murah Bisnis Online Belajar SEO Kerupuk Kerupuk kulit Social Bookmark Dofollow Social Bookmark Kumpulan Puisi Kirim Puisi bola hantu Penumbuh Rambut Penumbuh Rambut timbangan WBC Wonogiri Jasa SEO Murah Jasa SEO Jam Tangan Murah