💾 Archived View for sdf.org › rsdoiel › blog › 2020 › 06 › 20 › Procedures-as-parameters.html captured on 2023-06-14 at 14:27:45.

View Raw

More Information

⬅️ Previous capture (2023-03-20)

-=-=-=-=-=-=-

<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="language" content="EN">
  <title>Procedures-as-parameters</title>

  <link rel="stylesheet" type="text/css"  href="/printfonts/print.css" media="print" />
  <link rel="stylesheet" type="text/css"  href="/webfonts/fonts.css" media="screen" />
  <link rel="stylesheet" type="text/css"  href="/css/site.css" media="screen" />
  <link rel="stylesheet" type="text/css"  href="/css/tables.css" media="screen" />
  <link title="RSS feed for rsdoiel's blog" rel="alternate" type="application/rss+xml" href="https://rsdoiel.github.io/rss.xml" />
  <meta name="keywords" content="Oberon, procedures, passing procedures
as parameters, programming">
  <link rel="alternative" type="application/markdown" href="/blog/2020/06/20/Procedures-as-parameters.md">
  <link rel="search" type="application/opensearchdescription+xml"
        title="Robert's Rambling Search Engine"
        href="search.osdx">
</head>
<body>
<nav>
<ul>
<li><a href="/">R. S. Doiel</a></li>
<li><a href="/about.html">About</a></li>
<li><a href="/blog/">Blog</a></li>
<li><a href="/cv.html">CV</a></li>
<li><a href="https://github.com/rsdoiel">GitHub</a></li>
<li><a href="/library-terminology.html">Library Jargon</a></li>
<li><a href="/presentations.html">Presentations</a></li>
<li><a href="/projects.html">Projects</a></li>
<li><a href="/resume.html">Resume</a></li>
<li><a href="/search.html">Search</a></li>
<li><a href="/series/">Series</a></li>
</ul>
</nav>

<section>
  <article>
  <a data-pocket-label="pocket" data-pocket-count="none" class="pocket-btn" data-lang="en"></a>
<script type="text/javascript">!function(d,i){if(!d.getElementById(i)){var j=d.createElement("script");j.id=i;j.src="https://widgets.getpocket.com/v1/j/btn.js?v=1";var w=d.getElementById(i);d.body.appendChild(j);}}(document,"pocket-btn-js");</script>
<h1 id="procedures-as-parameters">Procedures as parameters</h1>
<p>By R. S. Doiel, 2020-06-20</p>
<p>This is the ninth post in the <a
href="../../04/11/Mostly-Oberon.html">Mostly Oberon</a> series. Mostly
Oberon documents my exploration of the Oberon Language, Oberon System
and the various rabbit holes I will inevitably fall into.</p>
<p>Oberon-07 supports the passing of procedures as parameters in a
procedure. Let’s create a module name <a
href="Noises.Mod">Noises.Mod</a> to explore this.</p>
<p>The key to supporting this is Oberon’s type system. We need to decide
what our generic procedure will look like first. In our case our
procedures that will display an animal noise will include the name of
the animal speaking. We’ll call this type of procedure “Noise”. It’ll
accept an ARRAY OF CHAR for the name as a parameter then use the
standard Out module to display the animal name and noise they make.</p>
<pre><code>
    TYPE
      Noise = PROCEDURE (who : ARRAY OF CHAR);
</code></pre>
<p>The two “Noise” procedures will be “BarkBark” and “ChirpChirp”. They
will implement the same parameter signature as describe in the “Noise”
type.</p>
<pre><code>
    PROCEDURE BarkBark(who : ARRAY OF CHAR);
    BEGIN
      Out.String(who);
      Out.String(&quot;: Bark, bark&quot;);Out.Ln();
    END BarkBark;

    PROCEDURE ChirpChirp(who : ARRAY OF CHAR);
    BEGIN
      Out.String(who);
      Out.String(&quot;: Chirp, chirp&quot;);Out.Ln();
    END ChirpChirp;
</code></pre>
<p>We’ll also create a procedure, MakeNoise, that accepts the animal
name and our “Noise” procedure name and it’ll call the “Noise” type
procedure passing in the animal name.</p>
<pre><code>
    PROCEDURE MakeNoise(name : ARRAY OF CHAR; noise : Noise);
    BEGIN
      (* Call noise with the animal name *)
      noise(name);
    END MakeNoise;
</code></pre>
<p>If we invoke MakeNoise with our animal name and pass the name of the
procedure we want to do the MakeNoise procedure will generate our noise
output. Here’ is what is looks like all together.</p>
<pre><code>
    MODULE Noises;
      IMPORT Out;
    
    TYPE 
      Noise = PROCEDURE(who : ARRAY OF CHAR);
    
    PROCEDURE BarkBark(who : ARRAY OF CHAR);
    BEGIN
      Out.String(who);
      Out.String(&quot;: Bark, bark&quot;);Out.Ln();
    END BarkBark;
    
    PROCEDURE ChirpChirp(who : ARRAY OF CHAR);
    BEGIN
      Out.String(who);
      Out.String(&quot;: Chirp, chirp&quot;);Out.Ln();
    END ChirpChirp;
    
    PROCEDURE MakeNoise(name : ARRAY OF CHAR; noise : Noise);
    BEGIN
      (* Call noise with the animal name *)
      noise(name);
    END MakeNoise;
    
    BEGIN
      MakeNoise(&quot;Fido&quot;, BarkBark);
      MakeNoise(&quot;Tweety&quot;, ChirpChirp);
      MakeNoise(&quot;Fido&quot;, ChirpChirp);
      MakeNoise(&quot;Tweety&quot;, BarkBark);
    END Noises.
</code></pre>
<p>Note when we pass the procedures we include their name
<strong>without</strong> parenthesis. Our type definition tells the
compiler that the procedure can be a parameter, any procedure with the
same signature, e.g. <code>who : ARRAY OF CHAR</code> as the only
parameter will be treated as a “Noise” type procedures.</p>
<h3 id="next-and-previous">Next and Previous</h3>
<ul>
<li>Next <a href="../../07/07/Procedures-in-records.html">Procedures in
Records</a></li>
<li>Previous <a href="../../05/25/Dynamic-types.html">Dynamic
types</a></li>
</ul>
  </article>
</section>

<footer>
<p>copyright © 2016 - 2023 R. S. Doiel<br /> <a
href="/rssfeed.html">RSS</a> feeds and website built with <a
href="https://rsdoiel.github.io/pttk">pttk</a>, Bash, Make and <a
href="https://pandoc.org">Pandoc</a>.</p>
</footer>
<!-- START: PrettyFi from https://github.com/google/code-prettify -->
<script>
/* We want to add the class "prettyprint" to all the pre elements */
var pre_list = document.querySelectorAll("pre");

pre_list.forEach(function(elem) {
    elem.classList.add("prettyprint");
    elem.classList.add("linenums");/**/
    elem.classList.add("json"); /**/
});
</script>
<style>
li.L0, li.L1, li.L2, li.L3, li.L4, li.L5, li.L6, li.L7, li.L8, li.L9
{
    color: #555;
    list-style-type: decimal;
}
</style>
<link rel="stylesheet" type="text/css" href="/css/prettify.css">
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_
prettify.js"></script>
<!--  END: PrettyFi from https://github.com/google/code-prettify -->

</body>
</html>