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 Library Studies

Propeller C Library Studies

Functions in Same File Sharing Variables

Sometimes it’s useful to group a few functions that always work together in the same file and let them share variables that are global to that file. 

 

A Global Variable for the Awesome Library

Let’s say you want your awesome function to track how many times it has been called in your application. And, you want a function named awesome_getCount to report the number of times awesome was called.  Here’s one way to make those changes to awesome.c:

  • In SimpleIDE, use the Open Project button to open libawesome.side.
  • If the Project Manager pane is not visible, click the Show Project button in the bottom-left corner of the SimpleIDE window.
  • Click awesome.c in the Project manager to open the file.
  • Modify it as shown here.

Now, each time the awesome function gets called, it adds 1 to a variable named calls.  Your application will also be able to call the awesome_getCount function to find out how many times the awesome function was called.    

 


Did You Know?

The static modifier  makes it so that only functions within the same file can see that variable.  There are advantages to making your library file variables static; for instance, you can decide to add a static variable named calls to epic.c using similar code, and not have to worry that functions of one file could read the calls variable from another. The awesome.c static calls variable would only keep track of the number of times awesome is called, and the epic.c static calls variable would only keep track of the number of times epic was called.


 

Finish the Library Update

This modified library isn’t quite ready to use yet.  Since we added a function to the library, we need to make sure to update the header file with an awesome_getCount function prototype.

  • Click the awesome.h tab, and add a function prototype for awesome_getCount.

Make sure to update your tests harness code in libawesome.c to exercise this new function and make sure it works. 

  • Click the libawesome tab and modify the code as shown below.
  • Run the program and verify that it correctly reports the number of times the awesome function was called.

If the test harness code works correctly, you can build the library for the memory models the library is designed to support. 

  • In the Project Manager’s Project Options tab, set the Memory Model to CMM, and click the Build Project button in the button bar near the top of the SimpleIDE window.
  • Repeat for other memory models you’ll support.

 

Your Turn

  • Repeat these steps with the epic.c file.  For the sake of practice, use a static variable named calls and name the function for reporting calls to epic, epic_getCount.

 


Printer-friendly version
Sharing Values in Source Files
Prev
Functions in Multiple Files Sharing Variables
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress