Perfect Developer basic tutorial 2 This page last modified 2011-10-29 (JAC)

Calling Functions and Constructors

Function calls

Functions that are not class members are invoked using the function name followed by the parameter list in brackets.
For example:

  myFunction(x, 2)

Member functions may be invoked on the current object self in the same way, or on a specific object using the dot notation.

If a function takes no parameters, an empty pair of brackets is not used. This means that the expression myAccount.balance could refer to a member variable balance within the object myAccount, or it could refer to the result of calling a member function balance on object myAccount. This is deliberate; it allows you to replace a member variable by a function without having to change every reference to it. Typically, clients of myAccount would not need to know whether balance is a variable or a function.

Constructor calls

A class declares constructors to build new values of that class. A constructor is invoked by giving the class name follows by a parameter list in braces (curly brackets), like this:

  BankAccount{"Joe Bloggs", 100}

This would yield a value of class BankAccount whose initial value is somehow related to the string "Joe Bloggs" and the number 100. You might remember seeing other examples of constructor calls in the descriptions of built-in classes, for example char{123}.

If there are no parameters, an empty set of braces is used so that the expression can be identified as a constructor call. A constructor that takes no parameters is called a default constructor for the class.

Knowledge round-up quiz

Next:  Collections

 

Save My Place Glossary Language Reference Manual
Tutorials Overview Main site   
Copyright © 1997-2012 Escher Technologies Limited. All rights reserved. Information is subject to change without notice.