Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Propeller C – Functions

Propeller C – Functions

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.

Printer-friendly version
Function with Parameters and Return
Prev
Multicore Example
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress