EnglishРусский  

Ads

Installer and installation software
Commercial and Freeware installers.

Function declaration: func

A function consists of two parts: a declaration and a function body. When you declare a function, you specify the keyword func, the type of its return value, its name, its attributes enclosed in angle brackets and its parameters enclosed in parentheses. Only the function name is required. When you do not specify the type of its return value, the function does not return any values.

A body of a function or a method is everything included in braces that follow the function description. The function body can contain subfunctions, expressions, constructions and descriptions of local variables.

func  uint sum( uint left right )
{
    return left + right
}

Attributes

entry

This attribute is specified for functions that must be started automatically before the main function is called.

main

This attribute is specified for the main function with which the program is started. If there are several functions with this attribute, the last function with main attribute will be called. The main function is run after all entry functions are called. Functions that have main or entry attribute should not have parameters.

func  uint myprog<main>
{
   print("Hello, World!")
   getch()
}

result

Gentee does not allow returning a structural type from a function if it belongs to (is described inside) this function. This attribute makes it possible to evade this restriction. You can find more details about using it at the Returning variables page.

alias

If you need to get and transmit a function, method or operator identifier somewhere, you can use this attribute. As functions and methods can have the same names, but different parameters, finding the necessary function can lead to some difficulties. You can assign an alias name to the attribute and use this name as a variable – function identifier.

func uint myfunc_verylongname<alias = myfunc>( uint param )
{
   return param * 10
}

func str mystring<result>
{
   result = "Result string"
}

func main<main>
{ 
 	print( "Val= \( myfunc->func( 10 ))")
 	print( mystring() )
 	getch()
}

Parameters

Each parameter declaration is a comma-delimited series of parameter names with the type identifiers specified after a type name, then followed by a comma or space and a new type name and parameters. If a function takes no parameters, omit the identifier list and the parentheses in its declaration. You can define functions with the same name but with different parameters. In this case, when you call a function the compiler looks for a function with the same name and parameters.

When you describe parameters, you can use brackets to specify the dimension and the of operator. When you describe such parameters, you do not have to specify a precise number of elements in brackets.

func uint myfunc( uint a b c, byte d, str st1 st2, arr marr[,] of uint )
{
   ...
}

Addressing the parameters is the same as addressing local variables. All numeric types are given to a function or a method by value. That means you can change the value of the parameter without any consequences. All structure types are given by reference. In this case all the changes you have made will happen to the original variable that you passed as a parameter.

func str myadd( str left )
{
   left += " OK!"
   return left
}

func main<main>
{
   str val 
   myadd( val = "Process" )
   print( val )
}

Related links