Function with Parameters and Return

Some functions are designed to do a job and send a return value back to the function call.  For example, a function might check a pushbutton and return its state.  Or, a function might receive some values through its parameters, use them in a calculation, and return the answer — we will try this.

We will also try a neat coding technique: setting a variable as equal to a function call.  This is a quick and efficient way to call a function and get its return value into a variable.

  • Open the project Function with Parameters and Return Value.side.
  • Read the code.  What do you think it will do?
  • Click the Run with Terminal button, and compare the result from the Propeller with your prediction.
/*
  Function with Parameters and Return Value.c
 
  Pass parameters to a function, let it do its job, and display the result
  it returns.
*/

#include "simpletools.h"                      // Include simpletools

int adder(int a, int b);                      // Function prototype

int main()                                    // main function
{
  int n = adder(25, 17);                      // Call adder function
  print("adder's result is = %d", n);         // Display adder function result
}

int adder(int a, int b)                       // adder function
{
  int c = a + b;                              // Add two values
  return c;                                   // Return the result
}

 

How it Works

The adder function’s definition is int adder(int a, int b).  The (int a, int b) part means that a function call to adder must pass two int values.  The int at the beginning means adder will send back, or return, an int value to the function call.

Take a close look at the line inside main that sets int n equal to an adder function call.  adder(25, 17) passes those two values to the adder function's a and b parameters. There, a + b adds the values together, and the result is assigned to a local variable with int c =.  At that point, c = 42. 

return c sends the value of c back to take the place of the original function call in main. So,  int n = adder(25, 17) evaluates to n = 42.

The next line in main, a print statement, displays the value of n.  Your SimpleIDE Terminal will display "adder's result is 42"

 
 


Did You Know?

Local Options — The function’s parameter list is not your only chance to declare local variables.  For example, int n = adder... and also (int a, int b) in the adder function are local variables. 

Type Less — The adder function could have been even shorter.  You could replace the two lines in its code block with return a + b; no need for int c at all.

Return value from main? — Notice all of our programs begin with int main(), by C convention. In computer system programming, a main routine might return a 0 tell a higher-level program that it executed.  Since we are programming a stand-alone microcontroller we don't need to have main return anything.  In fact, void main() and just main() will also work, though the C compiler might give you a warning message in the Status window.


 

Try This

Here are some variations on the main function that use the result adder returns in different ways. 

  • Use the Save Project As button to make a copy of your project and save it into the My Projects folder.
  • Modify the code as shown below, and try to predict the results.
  • Use the Run with Terminal button to display the output. Are the actual results the same as what you predicted?

 

Your Turn

Try including a subtracter function. 

  • Use the Save Project As button to make a copy of your project named Adder Subtracter Functions.
  • Add a forward declaration for subtracter above main.
  • Add the subtracter function (which subtracts a from b) below the adder function.
  • Call the subtracter function from within main.