Posts

Showing posts with the label Functions

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....

Returning several values from a single function call: BEHIND THE SCENES!

Image
Hi! Welcome to this new session where we will learn HOW TO RETURN SEVERAL VALUES FROM A FUNCTION! Let’s get started! What we’ve learned so far is that we can return a value from a function to the scope that called the function and then save it to a variable for later use. But… what if we want to return more than one value? This is where our newly discovered friends come into play , TUPLES AND LISTS! If we return a tuple or a list from a function, we can return several values and assign them individually to their corresponding variables (We will see this in detail in the next diagrams).  We’ve learned that we can display the values returned by a function using print(). Since lists and tuples are iterables, we can iterate over their elements using a for/in loop like the examples below. We can also access each individual element by its corresponding index. But… wait a minute! What if we want to store the values returned by the funct...

The map() function: BEHIND THE SCENES!

Image
Hi! First of all, congratulations! You are on Week 3 of the course, and on your way to a wonderful future in CS!. Welcome to this section. We will discuss  the powerful function map()  and how it can help us during our coding endeavors. Let’s get started! Step by Step Example Here is an example broken down step by step of  how the function map work behind the scenes NOTE:  you won’t see them actually being executed when you call map, but you will see the final result, the list or iterable with the resulting values of applying the function to each element in the list provided as argument. Hope it helps! If you have any questions, please post them in the forums. Your classmates and Community TAs will be there to help you! :) Estefania.

Return vs. Print: BEHIND THE SCENES!

Image
Return vs Print Hi! Welcome to this section. We will talk about the  difference between Returning and Printing a value inside a function . We will discuss what happens when we don’t explicitly return anything from a function and how to assign the value returned to a variable. Let get started! In the diagram you can see some essential characteristics of return and print (be sure to use lowercase letters for these keywords, otherwise Python will throw an error) Let's talk about  return : This is basically how the return statement works: Here you can see a diagram of the being picture of how return works and how you can assign the returned value to a variable. Let's talk about  print Now… let’s talk about “print”. Surely you’ve used this statement before for debugging. But what happens if we only use a print statement and not a return statement? Let’s see what happens! In this case, when a print statement is inside a funct...