<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Justin Johnson on Medium]]></title>
        <description><![CDATA[Stories by Justin Johnson on Medium]]></description>
        <link>https://medium.com/@softcoda?source=rss-5ada8f601f4f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2qmKzgg5ZkDmnPDKt_POvA.jpeg</url>
            <title>Stories by Justin Johnson on Medium</title>
            <link>https://medium.com/@softcoda?source=rss-5ada8f601f4f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 01:23:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@softcoda/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The ABC of Callback Functions — PART 1]]></title>
            <link>https://medium.com/@softcoda/the-abc-of-callback-functions-part-1-6149409489ea?source=rss-5ada8f601f4f------2</link>
            <guid isPermaLink="false">https://medium.com/p/6149409489ea</guid>
            <category><![CDATA[callback-function]]></category>
            <category><![CDATA[programming-tips]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[anonymous-function]]></category>
            <dc:creator><![CDATA[Justin Johnson]]></dc:creator>
            <pubDate>Mon, 08 Mar 2021 18:31:58 GMT</pubDate>
            <atom:updated>2021-03-08T18:56:29.412Z</atom:updated>
            <content:encoded><![CDATA[<p>How to understand Callback functions conceptually and easily apply it in any programming language or framework that uses this paradigm.</p><p>In this article, I will be considering things from the perspective of <strong>Javascript</strong>. Afterwards, in subsequent articles, I will take a look at how the same concept works and applies in other Programing languages like: Python, Kotlin and Dart.</p><p>Some key concepts to consider</p><ol><li><strong>Functions as first classs citizens</strong></li></ol><p>To understand Callback Functions work more conceptually, you must first understand the fact that they are to be considered and treated in the same manner as you would variables. This means that:</p><ul><li>They can be assigned and reassigned to local variables:</li><li>They can be passed as arguments to other Functions</li><li>They can also be the returned by other functions</li></ul><p>Now, let’s take a look at the above points individually:</p><p><strong>Functions As variables</strong></p><p><strong>Regular variable declaration</strong></p><p>let a = 20;</p><p><strong>Regular Function declaration</strong></p><p>function addTwo(x, y) {</p><p>return ( x +y )</p><p>}</p><p><strong>Regular Variable Assignment</strong></p><p>a = 20</p><p>console.log( a) //20</p><p>console.log( addTwo(2, 3) ) //5</p><p>Function Variable Assignment:</p><p>let addAnyTwo = addTwo</p><p>console.log( addAnyTwo(4, 5) ) // 9</p><p><strong>Variable Reassignment</strong></p><p>a = 30</p><p>console.log(a) //30</p><p><strong>Function Variable Reassignment:</strong></p><p>addAnyTwo = function( numX, numY) {</p><p>return ( numX + numY );</p><p>}</p><p>console.log( addAnyTwo( 4, 5 ) ); <em>// 9</em></p><p>As you can see from the above two examples, functions can be treated in the same manner as you would a regular variable.</p><p><strong><em>Note: to use the function variable you must call it by bringing the parenthesis: ‘()’</em></strong></p><p>Also, In the second example, during reassignment of the function variable: addAnyTwo, we had to leverage an <strong><em>Anonymous</em></strong> <strong><em>Function(on the right side of the ‘=’ operator);</em></strong> more about this in the subsequent section.</p><p><strong>Funtions can be passed as arguments to other Funtions</strong></p><p>function square( a, b, aFunc) {</p><p>//return the square of a function, passing any two integer args</p><p>return ( aFunc(a, b) * aFunc(a, b) )</p><p>}</p><p><em>// Passing the Function variable, from the previous example, as an argument to the square function:</em></p><p>console.log( square (1, 2, addAnyTwo) )</p><p>//breakdown — square:</p><p>return ( addAnyTwo(1, 2) * addAnyTwo(1, 2) ) // 3 * 3</p><p>// 9</p><p><strong><em>Note: When passing in the function variable as an argument, we do not and must not include the parenthesis: ‘()’. You must only pass in the name of the function or function variable.</em></strong></p><p><strong>Funtions can be returned by other Funtions</strong></p><p>Finally, as a First Class Citizen, a Function can be returned just like a regular variable. Consider the following:</p><p>function adderFunc( ) {</p><p>return addAnyTwo</p><p>}</p><p>let sumFunc = adderFunc() //return addAnyTwo and assign to sumFunc</p><p>//Now, variable sumFunc is a Function variable that accepts any two variables and returns their sum:</p><p>sumFunc( 2, 3 ) <strong><em>// 5</em></strong></p><p><strong>2. Function Signature</strong></p><p>Delving deeper into the basics of Callback Functions, we must fist understand what a Funtion’s signature is. A Function’s Signature describes the following:</p><p>a. the number and type of arguments a Function accepts(if it accepts any)</p><p>b. the type of the return value of the function( i.e. if it returns any).</p><p>So, consider the two functions:</p><ol><li><strong>addAnyTwo(2, 3) // 5</strong></li></ol><p>the above function, as its <strong>signature</strong>: accepts any two integer value as arguments and returns the sum of the two. Thus, it has an integer as the type of its return value.</p><p><strong>2. adderFunc()</strong> /<strong>/ addAnyTwo</strong></p><p>the above function’s signature is thus: accepts no arguments, but returns a Functuon variable. Thus, its return type is a function.</p><p>Note: very importantly, understanding a Function’s signature helps us to basically know how to interact with it: what type of variables should to pass to it and how to define those variables — as a regular variable or a function variable. And in the case of a Function argument, how it interacts with the <strong>Parent function.</strong></p><p><strong>3. Anonymous Functions</strong></p><p>These are basically functions without a name. They can be assigned to a variable, but are mostly past in as arguments to other functions. Example:</p><p>function(a, b) {</p><p>return ( a + b );</p><p>}</p><p><strong>Assigning to a variable:</strong></p><p>const aSum = function(a, b) {</p><p>return ( a + b)</p><p>}</p><p>aSum(2, 3) // 5</p><p><strong>Passing it in directly as an argument:</strong></p><p>squareFunc( 1, 2, functions (a, b) {</p><p>return ( a + b )</p><p>}</p><p>) // ( 1 + 2 ) * ( 1 + 2) = 3 * 3 = 9</p><p>Thus, from the above, a Callback Function is basically an Anonymous function passed in as an argument to a Parent Function, and called within the body of the Parent Function.</p><p>Notes on a Callback Function:</p><ul><li>It’s signature(expected structure) is as defined by the Parent Function</li><li>It’s called/executed only by and within the body of the Parent Function</li><li>Only the Parent function determines when it is called. Thus, it is not called directly by you.</li></ul><p><strong>4. Parent and Child functions</strong></p><p>From now onwards, we shall refer to a <strong>Callback Function</strong> as a <strong>Child Function</strong> and the Function in which it is called/executed as the <strong>Parent Function.</strong></p><p>So,</p><p>squareFunc(1, 2, addAnyTwo)</p><p>Child/Callback Function =&gt; addAnyTwo</p><p>Parent Function =&gt; squareFunc</p><p><strong>5. Parent-Child Communication</strong></p><ul><li>The parameters or arguments of a Callback Function is how the Parent Function communicates with the Child Function</li><li>The arguments of the Child Function are passed in by the Parent Function, wheneverit is called.</li><li>The return value of the Child/Callback Function is how it communicates with the Parent Function</li></ul><p><strong>6. Conclusion</strong></p><p>So, in conclusion, before you interact with any Function that accepts a Callback Function and start defining it, ask yourself the following questions:</p><ul><li>What’s the Signature of the Callback Function</li><li>Does it accept any argument? If so, what are the argument’s types and are they mandatory?</li><li>Does it return any value? If so, what is the type: a regular value or a function.</li></ul><p>There are other key things not covered in this article such as Arrow Functions, Closures and the like. These will be dealt with in the subsequent articles.</p><p>This is my first article on medium and am really opened to any corrections and/or questions.</p><p>A big thanks to <a href="https://medium.com/u/695782484bda">Nader Dabit</a> . I’ve been deeply inspired by your articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6149409489ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>