Memory Functions Can Share

So far, this Propeller C - Functions tutorial has used local variables declared inside of functions.  There are advantages to using local variables, since they only use memory temporarily while the function using the variable is executing. This can be useful with microcontrollers, where memory is limited as compared to computers.

Sometimes it is necessary to have variables that more than one function can access and modify.  These shared variables are called global variables.  The multicore Propeller can execute different functions with different cores (also called cogs) at the same time.  These functions operating in parallel can use global variables to exchange information (which we will do in the next lesson).

The example program below uses a modified version of the adder function from the previous lesson, but with all local variables replaced with global variables throughout the code.

  • Click Open Project and select Global Exchange from ...SimpleIDE\Learn\Examples\C Intro\Functions. 
  • Click the Run with Terminal button, and verify that the main function displays the result that adder stores in the global n variable.
/*
  Global Exchange.c
 
  Functions exchange information with global variables.
*/

#include "simpletools.h"                      // Include simpletools

void adder(void);                             // Forward declaration

int a, b, n;                                  // Global variables

int main()                                    // main function
{
  a = 96;                                     // Set values of a & b
  b = 32;
  adder();                                    // Call adder function
  print("n = %d\n", n);                       // Display result
}

void adder(void)                              // adder, no parameters or return
{
  n = a + b;                                  // Use values of a & b to set n
}

 

How it Works

The adder function definition void adder(void) ; does not declare parameters or a return value.

Next, a, b, and c are declared as global variables.  Since they are all the same type, int variables, they can be declared in a single statement separated by commas. 

Instead, a and b are assigned values in separate statements inside the main function, just before the adder function call.  The code jumps to the adder function, where the result of a + b is assigned to the global variable n.

Code execution then resumes on the next line in the main function, a print statement that displays the value of n.

Since a, b, and n were all declared before the first function, they were accessible to all the functions in the program. So, there was no need to pass parameters or return values between functions.

 


Did You Know?

Declare & Initialize — In this example, a and b were first assigned values from within the main function. However, that could have been done right in the original declaration, like this: int a = 96, b = 32, n;

Space vs. Speed — Global variables take more memory, but using them can make code execute faster in some cases.  Setting up and releasing memory for local variables takes a little bit of time.  If your project uses lots and lots of local variables, these bits of time can add up.

Scope — The terms local and global describe a variable's scope, the context in which the variable can be used validly in a program.

BUG ALERT! 
Do not put int (or any other variable type) in front of a global variable while using it inside a function!  That would create a local variable with the same name, but only that function would use it. It might not have the same value as the same-named global function, and changes made to the local variable's value would not be stored in the global same-name function. 


 

Try This

  • Modify this program by creating a subtracter function that uses the global variables, and add some code to your main function to test it.