Computer Science

Share this article:

Computer Science

  • Join our comunity:

Function Parameters

By: , Posted on: November 16, 2015

When you use a function in C/C++ (or most other programming languages) you are likely to pass it some parameters – data which may be processed by the function or give it information on what action is required. For the majority of programmers, outside of the world of embedded software, parameters are just a fact – no real issues. They are likely to be quite uninterested in how parameters are passed and how efficient and secure that process that might be. Embedded developers tend to want details. Efficient code is essential and avoiding possible bugs is always desirable. So, maybe a look at how parameter passing actually works might be useful …

When embedded systems were all programmed in assembly language, the programmer had complete freedom with parameter passing and a number of options were available:

• Use global variables – not generally recommended, but might be most efficient in some circumstances.

• Use registers – this can be fast and efficient; the required data might already be in a register (which is almost the same as using a global variable, so it needs care).

• Use the stack – SP-relative addressing may be efficient in many processors; this approach leads to reentrant code.

• Use a dedicated memory block – this approach may make sense, if the addressing modes of the processor are limited, but probably results in non-reentrant code.

In C, some aspects of parameter passing are specified by the language definition; others are implementation dependent and may vary from one compiler to another. There are no specific constraints on the parameter passing mechanism. It is left to the compiler writer to choose between stack (which is most common), registers or something else. It is bad programming practice to make assumptions about how parameters are passed. You could write code like this:

function parameters

This function simply takes the address of the first parameter, assumes that it is on the stack and indexes off of that address to find further parameters. Please do not do this.

In C/C++ parameters are normally passed by value – the value passed as a parameter is copied into the formal parameter of the function. C++ gives the option of passing by reference, which means that a called function can access variables which are passed as parameters by the calling function. To achieve this in C, you need to use pointers (which is, of course, what C++ is doing behind the scenes).

Read more from Colin about embedded software on SciTech Connect


Embedded Software The Works new coverColin’s most recent publication, Embedded Software: The Works is available now on the Elsevier Store. 

Save 30% on his book and other Newnes Press and embedded systems books. Use discount code “STC3014″ at checkout. 

About the Author

Colin WallsColin Walls (@Colin_Walls) is an embedded software technologist at Mentor Graphics (@mentor_graphics), the leading EDA software company.

You can read more about Colin and his work on embedded systems at The Colin Walls Blog at Mentor Graphics here. Connect with Colin online here:

facebook google plus linkedin slideshare twitter wordpress

Computer Science

Computing functionality is ubiquitous. Today this logic is built into almost any machine you can think of, from home electronics and appliances to motor vehicles, and it governs the infrastructures we depend on daily — telecommunication, public utilities, transportation. Maintaining it all and driving it forward are professionals and researchers in computer science, across disciplines including:

  • Computer Architecture and Computer Organization and Design
  • Data Management, Big Data, Data Warehousing, Data Mining, and Business Intelligence (BI)
  • Human Computer Interaction (HCI), User Experience (UX), User Interface (UI), Interaction Design and Usability
  • Artificial intelligence (AI)
Morgan Kaufmann companion resources can be found here You can also access companion materials and instructor’s resources for all our new books on the Elsevier Store. Search by author, title or ISBN, then look for the “Resources” tab on any book page. Looking for companion materials or instructor’s resources for these titles? Connect below: