📄 functions.jc
字号:
/*
* functions.jc
*
* This file will explain a little how functions (and methods) are used
* in JewelScript.
*/
/* import the standard library, so we can print to console */
import stdlib;
/*
* Global functions
*
* Global functions are defined (or declared) using the 'function' keyword.
* A global function can reside outside of any class namespace, or within:
*/
function hello()
{
stdlib::Print("Hello!\n");
}
class CFoo
{
method CFoo()
{
bar = 0;
}
method SetBar( long b )
{
bar = b;
}
function hello()
{
stdlib::Print("Hello, too!\n");
}
long bar;
}
/*
* Instance member functions (methods)
*
* As we can see in class CFoo, instance member functions (methods) need to
* be defined (or declared) using the 'method' keyword. If the name of a method
* is equal to the class name, then it is a constructor for that class.
* Constructors never have a return value. And, each constructor *must* initialize
* all member variables declared in the class! A class can have any amount of
* constructors, but it *must* have at least one!
* Class CFoo also has a global function. This global function can be called from
* anywhere in the program without needing an object. However, the function
* resides in the namespace of the class, so in order to call it, you must write:
*/
function test()
{
CFoo::hello();
}
/*
* Function / method arguments
*
* JewelScript distiguishes functions and methods not only by their name, but also
* by their full argument list and result type. This means, that...
*/
function hello(long l)
{
stdlib::Printf("hello(long %d)\n", l);
}
/* ...and... */
function hello(float f)
{
stdlib::Printf("hello(float %g)\n", f);
}
/*
* ...are seen as different functions for the compiler. The compiler will choose the
* correct version of 'hello' from the arguments passed to the function call:
*/
function test2()
{
hello( 10 ); // will call function hello(long l)
hello( 20.0 ); // will call function hello(float f) XXX: CURRENTLY BROKEN DUE TO AUTO-CONVERT!
}
/*
* Note that you can create conflicts when using the typeless 'var' type as function
* argument:
*/
function var_problem( var anyType )
{
}
/*
function var_problem( float f )
{
}
*/
/*
* If the above function would not be commented out, the compiler would issue a
* compile-time error. This is because the compiler does not know what function
* to call when the argument is float, since both functions accept a float argument
* and there are no other arguments or result type to distinguish both functions.
*
* Also note that the 'const' and 'reference' modifiers are not taken into account,
* when the compiler tries to distinguish functions. This is not a bug, it is done
* on purpose, to reduce the risk of programming errors.
* The following example would give you another compile-time error, if the second
* function wasn't commented out:
*/
function const_problem( const long l )
{
}
/*
function const_problem( long l )
{
}
*/
function ref_problem( long& l )
{
}
/*
function ref_problem( long l )
{
}
*/
/*
* Result type
*
* JewelScript does not understand the keyword 'void'. If a function has no return
* value, this is notated by not specifying a return type, as you can see with all
* the previous functions. If we wanted to return a value, for example a number,
* we would write:
*/
function long hello(long l)
{
stdlib::Printf("long hello(long %d)\n", l);
return l;
}
/*
* Note that the above hello function only differs from the previous hello(long l)
* by return type, but the compiler is able to know which function to call:
*/
function test3()
{
hello( 30 );
long l = hello( 40 );
}
/*
* function main
* main entry point
*/
function main()
{
hello();
test();
test2();
test3();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -