Inline Functions

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

Inline Functions Overview

The Fractal Science Kit fractal programming language supports inline functions. Inline functions allow you to write a block of code once and execute the function 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 function above that required to execute the instructions themselves, of course.

Inline functions can include arguments passed by-value or by-reference and can return a complex value or an object.

Inline functions are defined using one of the following statements:

[<Type>] <FunctionName>(<Arg>, <Arg>, ...) = <Expression>

or

[<Type>] <FunctionName>(<Arg>, <Arg>, ...) {
  <Statements>
}

The 1st form is appropriate only for very simple functions. <Expression> is an expression that results in a value of the function's declared <Type>.

The 2nd form may include local variables, loops, calls to built-in functions, calls to inline functions and methods, return statements, etc. You can only call other inline functions/methods that have already been defined, and (not surprisingly) recursion is not supported. The last statement executed by a function must be a return statement. return statements are of the form:

return <Expression>

<Expression> is an expression that results in a value of the function's declared <Type>.

The type of the function's return value is given by <Type> and should be Complex or an Object name. <Type> is taken as Complex if omitted. A function must return a value consistent with its declared type.

The function 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.ProjectPointOnLine, Geometry.IntersectLines, and Geometry.AverageAngle, are 3 of the functions with the Geometry prefix. Grouping the functions related to an object is a common practice, as well. For example, Circle.DistanceTo, Circle.ReflectPoint, and Circle.ReflectCircle, are 3 of the Circle functions.

The function arguments are optional but the opening and closing parentheses are required, as they are when the function is invoked. When the function is invoked, the number of arguments and their types must match the function definition or an error results. <Arg> in the function definitions 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 function do not affect the actual argument passed to the function. In rare cases, you may want to return information to the calling program through 1 or more of the function's arguments. This is more common for Inline Methods. In those cases, you would use the byref modifier to request that the argument be passed by-reference. When you invoke a function 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 function will affect the array in the calling program. The byref modifier is not required for array arguments.

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

Example:

Complex Geometry.MidPoint(p0, p1) = (p0+p1)/2

This simple function returns the midpoint of the 2 argument points.

You can call this function using:

midpt = Geometry.MidPoint(p, q)
midpt = Geometry.MidPoint(z, point[n])
midpt = Geometry.MidPoint(z-center, 0)

Example:

Complex Geometry.IntersectLines(p0, p1, q0, q1) {
  n = (q1.x-q0.x)*(p0.y-q0.y) - \
      (q1.y-q0.y)*(p0.x-q0.x)
  d = (q1.y-q0.y)*(p1.x-p0.x) - \
      (q1.x-q0.x)*(p1.y-p0.y)
 
  if (d = 0) {
    if (n = 0) {
      return p0 ' lines are coincident
    } else {
      return Infinity ' lines are parallel
    }
  }
  return p0 + n/d * (p1-p0)
}

This function returns the point of intersection of the 2 lines defined by p0/p1 and q0/q1.

You can call this function using:

center = Geometry.IntersectLines(p0, p1, q0, q1)

Example:

'
' Return the product of the count transformations in m[].
'
Mobius Mobius.MultiplyArray(Mobius m[], count) {
  Mobius result = Mobius.Identity()
 
  for (i = 0, i < count, i += 1) {
    result = Mobius.Multiply(result, m[i])
  }
  return result
}

This function returns the product of the set of Mobius transformations in the array m[].

You can call this function using:

Mobius m[] = \
  Mobius(A0, B0, C0, D0), \
  Mobius(A1, B1, C1, D1), \
  Mobius(A2, B2, C2, D2), \
  Mobius(A3, B3, C3, D3)

Mobius product = Mobius.MultiplyArray(m[], 4)

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

 

Copyright © 2004-2016 Hilbert, LLC
All rights reserved