Inline Methods

Home • Gallery • Tutorials • Download • Purchase • Site Map
 

Inline Methods Overview

The Fractal Science Kit fractal programming language supports inline methods. Inline methods allow you to write a block of code once and execute the method with a single line of code. As the name implies, the code is inserted directly into the program at the point of call and optimized during compilation so there is no performance penalty for calling the method above that required to execute the instructions themselves, of course.

Inline methods can include arguments passed by-value or by-reference.

Inline methods are defined using:

void <MethodName>(<Arg>, <Arg>, ...) {
  <Statements>
}

Inline methods may include local variables, loops, calls to built-in functions, calls to inline functions and methods, return statements, etc. The return statements cannot include a value however. You can only call other inline functions/methods that have already been defined, and (not surprisingly) recursion is not supported. When an inline method is invoked, it must be alone on the line; i.e., it cannot be assigned to a variable or passed as an argument to a function/method. This makes sense since it does not return a value.

The method name must be a valid identifier with the exception that a period (.) is allowed to appear within the name. The reason a period is permitted, is to support notational grouping of related functions based on the part of the name to the left of the period. For example, Geometry.GeneratePointsOnCircle, Geometry.GenerateAngles, and Geometry.GeneratePointsOnSegment, are 3 of the methods with the Geometry prefix. Grouping the methods related to an object is a common practice, as well. For example, Mobius.Translate, Mobius.Rotate, and Mobius.Scale, are 3 of the Mobius methods.

The method arguments are optional but the opening and closing parentheses are required, as they are when the method is invoked. When the method is invoked, the number of arguments and their types must match the method definition or an error results. <Arg> in the method definition above, can be a non-array argument or an array argument, each with its own syntax.

The syntax for non-array arguments is:

[byref] [<Type>] <ArgName>

Most arguments are passed by-value. This means that changes to the argument inside the method do not affect the actual argument passed to the method. In rare cases, you may want to return information to the calling program through 1 or more of the method's arguments. In those cases, you would use the byref modifier to request that the argument be passed by-reference. When you invoke a method that has a byref argument, the argument you pass must be a variable; i.e., not an expression, a literal number, a read-only variable, or a constant, since these cannot be changed. If you do not use the byref modifier, the argument is passed by-value.

The type of the argument is given by <Type> and should be Complex or an Object name. If the argument is Complex, the type specification can be omitted. The name of the argument must be a valid identifier.

Array arguments must match 1 of the following forms:

[<Type>] <ArgName>[]
[<Type>] <ArgName>[,]

The 1st form is for 1-dimensional arrays and the 2nd form is for 2-dimensional arrays.

The type of the array argument is given by <Type> and should be Complex or an Object name. If the argument is Complex, the type specification can be omitted. The name of the array argument (<ArgName>) must be a valid identifier and the trailing brackets ([] or [,]) are required.

All array arguments are passed by-reference and changes to the array inside the method will affect the array in the calling program. The byref modifier is not required for array arguments.

When you pass an array to the method, you must include the [] or [,] along with the array name.

Example:

'
' This method fills points[] with count points equally spaced
' around a circle with the given center and radius, and starting
' with a point at angle angle0 radians from the X axis. The array
' points[] is assumed to be large enough to hold count values.
'
void Geometry.GeneratePointsOnCircle(points[], count, center, radius, angle0) {
  if (count > 0) {
    inc = 2*Math.PI/count
    ang = angle0
   
    for (i = 0, i < count, i += 1) {
      points[i] = center + radius*Cis(ang)
      ang += inc
    }
  }
}

You could call this method using:

Complex points[8]
Geometry.GeneratePointsOnCircle(points[], 8, 0, 1, 0)

The brackets [] are included with the array name in the call.

Example:

'
' Find the 2 solutions to the equation: A*x^2 + B*x + C = 0.
'
void Math.ApplyQuadraticFormula(A, B, C, byref root0, byref root1) {
  if (A <> 0) {
    t0 = 2*A
    t1 = Sqrt(B^2 - 4*A*C)
    root0 = (-B + t1)/t0
    root1 = (-B - t1)/t0
  } else {
    ' single root so return in both variables
    root0 = -C/B
    root1 = root0
  }
}

This method uses byref arguments root0 and root1 to return the 2 solutions to the equation: A*x^2+B*x+C=0.

 

Copyright © 2004-2019 Ross Hilbert
All rights reserved