Posts

Showing posts with the label Week 2

Recursion: Behind the Scenes

Image
Welcome to Recursion! Hi! Welcome to this introduction to recursion, it’s great to have you here. 😀 This is a very powerful concept in programming…  a function that calls itself!  I know what you must be thinking: What?! The function calls itself? Why would you want to do that? 😅 Recursion is used when the  result of a problem depends on the result of smaller versions of the same problem . Sounds strange? Don't worry! You will learn what it means with this tutorial. 😀 Let’s dive into the details  with an example, calculating factorial. Meet Factorial 👋 The factorial of a positive integer n is the result multiplying all the integers less than or equal to n. For example, the factorial of 3 is 3 * 2 * 1 (by convention, the factorial of 0 is 1) Factorial is denoted as  n!  in mathematics. This is the code we will be working with. Please take a moment to analyze it. Base Case I’m sure that you noticed that there is a call to factorial...

Higher-Order Programming: Behind the Scenes!

Image
Welcome to Higher-Order Programming! Hi, Welcome! In this section we are going to dive into Higher-Order Programming. You will learn how to pass functions as parameters to other functions and how to return functions from functions. Let’s get started! In the diagram below you can first see two of the functions we will be working with. These are regular functions  f  and  g  that perform a simple operation on their parameter. In the purple rectangle you can find an example of  passing functions as arguments  to other functions. In this case we pass the functions  f  and  g  to the function  h . In the blue rectangle you can find an example of  returning functions  from functions. Let’s look at each one in more detail! : ) Passing Functions as Arguments The function  applyFunctions  takes three parameters ( f ,  g  and  p ).  f  and  g  are functi...

Closures: Behind the Scenes!

Image
Welcome! Hi, Welcome! We will discuss a very interesting concept related to functions,  “Closures” , what they are and how to use them. Let’s get started! First of all, what is a Closure? The concept might be a little bit abstract at first, so let’s jump to a concrete example: What does a Closure look like? Here you can see the general structure of a Closure: We have an enclosing function ( returnMultiplyFunction ) with a nested function ( multiplyBy ). The nested function uses a variable that is part of the enclosing function’s scope (in this case,  multiplyBy  uses x, the parameter passed to the outer function) The inner function is returned by the enclosing function. NOTE:  A Nested function is a function defined inside another function. You may ask, what happens when we call  multiplyBy ? We have two variables but we only pass in one argument,  y . What value will  x  have? Let’s find out! : ) Ready...

How to use Functions returned by Functions: BEHIND THE SCENES!

Image
Welcome! : ) Hi! We will discuss an amazing tool, H ow to use Functions returned by Functions.  Let's get started! Code: This is the code we will be working with. We have an  add(x, y)  function and a  multiply(x, y)  function that perform their corresponding operation on their parameters and return the result. The function where the magic starts is  determineAddOrMultiply(a) . This function takes in an argument and if that argument is greater than 5 it returns the  FUNCTION   add . Else, if the argument is equal to or less than 5 it returns the  FUNCTION   multiply . Note:  I’m emphasizing that we are returning  Functions  because, as you can see in the diagram above, we are not using parentheses after the name of the function and therefore we are not calling the function. By using this syntax we are referring to the function itself, a reference to the entire structure that the function represents....