Embedded Software Colin Walls
Colin Walls has over thirty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, Colin is an embedded software technologist with Mentor … More » Function parametersNovember 16th, 2015 by Colin Walls
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:
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: 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). 3 Responses to “Function parameters” |
It looks like much of the article is missing, unless the browser is not rendering some strange continuation widget ? It seems to end with “which is, of course, what C++ is doing behind the scenes)”.
Sorry that you seem to be confused Tim. The posting does, indeed, end at that point.
Ah, ok, mea culpa. Given the intro, I thought might provide more details and guidance – it’s an interesting subject and one that might have provided some help and tips for the audience, not sure we have here (beyond a bit of hand-waving and a “don’t do this”). Thanks for getting back to me though Colin.