Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Simula
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Sample code == === Minimal program === The empty [[computer file]] is the minimal [[computer program|program]] in Simula, measured by the size of the [[source code]]. It consists of one thing only; a dummy [[Statement (computer science)|statement]]. However, the minimal program is more conveniently represented as an empty block: '''Begin''' '''End'''; It begins executing and immediately terminates. The language lacks any [[return value]] from the program. === Classic Hello world === An example of a [[Hello world program]] in Simula: '''Begin''' '''OutText''' ("Hello, World!"); '''Outimage'''; '''End'''; Simula is [[case-insensitive]]. === Classes, subclasses and virtual procedures === A more realistic example with use of classes,<ref name="CommonBase"/>{{rp|1.3.3, 2}} subclasses<ref name="CommonBase"/>{{rp|2.2.1}} and virtual procedures:<ref name="CommonBase"/>{{rp|2.2.3}} '''Begin''' '''Class''' Glyph; '''Virtual''': '''Procedure''' print '''Is''' '''Procedure''' print;; '''Begin''' '''End'''; Glyph '''Class''' Char (c); '''Character''' c; '''Begin''' '''Procedure''' print; OutChar(c); '''End'''; Glyph '''Class''' Line (elements); '''Ref''' (Glyph) '''Array''' elements; '''Begin''' '''Procedure''' print; '''Begin''' '''Integer''' i; '''For''' i:= 1 '''Step''' 1 '''Until''' UpperBound (elements, 1) '''Do''' elements (i).print; OutImage; '''End'''; '''End'''; '''Ref''' (Glyph) rg; '''Ref''' (Glyph) '''Array''' rgs (1 : 4); ''! Main program;'' rgs (1):- '''New''' Char ('A'); rgs (2):- '''New''' Char ('b'); rgs (3):- '''New''' Char ('b'); rgs (4):- '''New''' Char ('a'); rg:- '''New''' Line (rgs); rg.print; '''End'''; The above example has one [[Superclass (computer science)|super class]] (Glyph) with two [[Subclass (computer science)|subclasses]] (<code>Char</code> and <code>Line</code>). There is one [[Virtual function|virtual procedure]] with two [[implementation]]s. The execution starts by executing the main program. Simula lacks the concept of [[abstract class]]es, since classes with pure [[Virtual function|virtual procedures]] can be [[Instance (computer science)|instantiated]]. This means that in the above example, all classes can be instantiated. Calling a pure virtual procedure will however produce a [[Runtime (program lifecycle phase)|run-time]] [[Error message|error]]. === Call by name === Simula supports [[call by name]]<ref name="CommonBase"/>{{rp|8.2.3}} so the [[Jensen's Device]] can easily be implemented. However, the default transmission mode for simple parameter is [[call by value]], contrary to [[ALGOL]] which used [[call by name]]. The source code for the Jensen's Device must therefore specify [[call by name]] for the parameters when compiled by a Simula compiler. Another much simpler example is the [[Arithmetic function#Summation functions|summation function]] <math>\sum</math> which can be implemented as follows: '''Real''' '''Procedure''' Sigma (k, m, n, u); '''Name''' k, u; '''Integer''' k, m, n; '''Real''' u; '''Begin''' '''Real''' s; k:= m; '''While''' k <= n '''Do''' '''Begin''' s:= s + u; k:= k + 1; '''End'''; Sigma:= s; '''End'''; The above code uses [[call by name]] for the controlling variable (k) and the expression (u). This allows the controlling variable to be used in the expression. Note that the Simula standard allows for certain restrictions on the controlling variable in a [[for loop]]. The above code therefore uses a while loop for maximum portability. The following: <math> Z = \sum_{i=1}^{100}{1 \over (i + a)^2}</math> can then be implemented as follows: Z:= Sigma (i, 1, 100, 1 / (i + a) ** 2); === Simulation === Simula includes a [[simulation]]<ref name="CommonBase"/>{{rp|14.2}} package for doing [[discrete event simulation]]s. This simulation package is based on Simula's object-oriented features and its [[coroutine]]<ref name="CommonBase"/>{{rp|9.2}} concept. Sam, Sally, and Andy are shopping for clothes. They must share one fitting room. Each one of them is browsing the store for about 12 minutes and then uses the fitting room exclusively for about three minutes, each following a normal distribution. A simulation of their fitting room experience is as follows: Simulation '''Begin''' '''Class''' FittingRoom; '''Begin''' '''Ref''' (Head) door; '''Boolean''' inUse; '''Procedure''' request; '''Begin''' '''If''' inUse '''Then''' '''Begin''' Wait (door); door.First.Out; '''End'''; inUse:= '''True'''; '''End'''; '''Procedure''' leave; '''Begin''' inUse:= '''False'''; '''Activate''' door.First; '''End'''; door:- '''New''' Head; '''End'''; '''Procedure''' report (message); '''Text''' message; '''Begin''' OutFix (Time, 2, 0); OutText (": " & message); OutImage; '''End'''; Process '''Class''' Person (pname); '''Text''' pname; '''Begin''' '''While''' '''True''' '''Do''' '''Begin''' Hold (Normal (12, 4, u)); report (pname & " is requesting the fitting room"); fittingroom1.request; report (pname & " has entered the fitting room"); Hold (Normal (3, 1, u)); fittingroom1.leave; report (pname & " has left the fitting room"); '''End'''; '''End'''; '''Integer''' u; '''Ref''' (FittingRoom) fittingRoom1; fittingRoom1:- '''New''' FittingRoom; '''Activate''' '''New''' Person ("Sam"); '''Activate''' '''New''' Person ("Sally"); '''Activate''' '''New''' Person ("Andy"); Hold (100); '''End'''; The main block is prefixed with <code>Simulation</code> for enabling simulation. The simulation package can be used on any block and simulations can even be nested when simulating someone doing simulations. The fitting room object uses a queue (<code>door</code>) for getting access to the fitting room. When someone requests the fitting room and it's in use they must wait in this queue (<code>Wait (door)</code>). When someone leaves the fitting room the first one (if any) is released from the queue (<code>'''Activate''' door.first</code>) and accordingly removed from the door queue (<code>door.First.Out</code>). Person is a subclass of <code>Process</code> and its activity is described using hold (time for browsing the store and time spent in the fitting room) and calls procedures in the fitting room object for requesting and leaving the fitting room. The main program creates all the objects and activates all the person objects to put them into the event queue. The main program holds for 100 minutes of simulated time before the program terminates.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)