Using Functions

Functions are a way of segmenting off sections of code so that they can be run at different times during a program. A function might be used once or many times while a program is running.

A function must first be defined (written) and then it can be called (used) during the program. Let’s look at a flowchart that we saw in the Assigning variables section.

In this flowchart, we get a value from the user, and store it as a variable called celcius.

We also get a variable called fahrenheit and we assign it the value that is the result of the calculation: celcius * 9 / 5 + 32

The flowchart then outputs the value now stored in fahrenheit.

If we wanted to run this code more than once we would need to keep running the flowchart. However, if we make it a function we could just have Main call it whenever it was needed.

Function flowchart

Create a Function

We can create a function that will calculate Fahrenheit for us, but we will need to pass it the celcius value. Take a look at this function definition written in pseudocode (line numbers have been added for clarity):

 calcFahrenheit(cValue): 
       fValue = cValue * 9 / 5 + 32
       return fValue>

Line one is called the function header, and gives our function the name “calcFarenheit”. Inside the round brackets is a variable name. In a function definition this use of a variable is called a parameter. This means that when this function is used, it will receive a value which will be treated as a celcius temperature.

Line two is indented, and is the first of two statements that are run when the function is used. We have a local variable called farenheit and we also will be able to use the value from the parameter that is passed in.

The pseudocode says that we will assign farenheit the value of the calculation, “cValue * 9/5 + 32”. In line three, now that we have done the calculation, the value stored in farenheit will be returned to whatever section of the program that called it.

In Main rather than having the assignment: fahrenheit = celcius * 9/5 + 32 we will use a call to a function:

fTemp = calcFahrenheit (celcius) And then we can use fTemp to print out the fahrenheit value.

Now that we have an idea of how this works, lets create the new function.

header image

Next to the drop down list the says “Main” is a set of pink ovals with a green + sign. Click that, and the Add function box should appear.

  1. Enter the name of the new function. Note that you cannot use underscores in Flowgorithm. Use camel case instead.
  2. Next click the Add button next to the box that has the heading Parameters. Remember from our function header that we will need to add a parameter for the celcius value we will pass to the function. Edit parameter

  3. In the Edit Parameter box the parameter is named cValue rather than celcius. This is to let us know that it is the local parameter that we are going to use in the function.

  4. We have also added the type of the parameter which is Real. Note that our parameter must be of the same type that is passed in from Main in the function call.

  5. Click ok.

  6. Back on the Function Properties box we can see that cValue has been added to the Parameters box. If we had more than one parameter to add we could keep clicking the add parameters button.

  7. At the bottom of the Function Properties window there is a drop down menu for Return Type. We will be returning a value to Main with the calculated fahrenheit temperature. Since our variable in Main that will be receiving this value is a Real, we need to make our local variable a Real.

  8. Finally, type a name for our return variable. In this example we will use fValue. Enter the name of the variable and click the OK button.

Here we see the function name and in brackets is the parameter. At the bottom is the oval showing what will be returned to Main.

Edit parameter

  1. Just as we did in Main, we click on the vertical arrow to bring up the shapes menu.
  2. First we need to create the variable fValue that will store the result of the calculation to fahrenheit and that will also be used to return that value to main.
  3. Choose the Declare shape and add a variable called fValue
  4. Then click the vertical arrow again and choose the assign shape.
  5. Double click on the Assign shape and enter the expression as you see in the image below. Click OK.

Assignement properties

  1. We’re done with the function, so click on the drop down menu next to the Add Function item and choose Main to return to the Main function.

  2. Back on Main, remove any shapes that held the calculation, and make sure it looks like the image below.

Flow example

  1. Now we need to use our variables in Main to call the calcFahrenheit function. Below the Real fahrenheit shape click the vertical arrow.
  2. Choose the Assign shape and enter the expression as you see it in the image below. Assignment properties final

Important concepts to know with functions

  • Do NOT use the call shape, the function should either display something or return a value to main or the calling function
  • If you are returning a value to main (or the calling function) you will need a variable in main to store the returned value

  • A function header lists the name of the function and any parameters it takes

  • A parameter is a variable that is listed in the function header. It will receive a value from Main and store it in the parameter
  • An argument is a variable passed to the function in the function call from main or another area of the program
  • A function can only return a value to main if you specify a return variable with its associated type
  • A function can only return one value to main
  • When you want to call a function in main you need to have:

    • A variable to receive what is returned from the function
    • The name of the function
    • Any values that need to be passed to the functions. These are called arguments.
  • It is possible to name your arguments the same as the parameters, but you may find this confusing in the beginning. For example in the program the argument was celcius and the parameter was cValue.

results matching ""

    No results matching ""