Global Sources
EE Times-India
Stay in touch with EE Times India
 
EE Times-India > Embedded
 
 
Embedded  

Understanding function parameters

Posted: 11 Jun 2014     Print Version  Bookmark and Share

Keywords:parameters  C function  code  Programmers  embedded developers 

Passing parameters to a C function may seem quite simple. The details of how that process takes place have a significant impact on the behaviour and performance of the code. Programmers of desktop computers do not care about this impact; embedded developers do not have that luxury. This article looks at parameter passing mechanisms and how to use them optimally.

Passing parameters
Programming in general is all about writing some code which acts upon some data. Although, in an embedded application, the data may be obtained from an external device, it is much more likely that the code is in the form of a subroutine or function, which receives its input by means of parameters.

Different programming languages offer a selection of options with regard to parameter passing.

Arguably the most flexibility is available when programming in assembly language, so it is worth appreciating the options in this context in order to understand the impact of the possibilities in C and C++.

Parameters in assembly
It was once common to code entire embedded applications in assembly. That practice is now relatively rare and only small amounts of such low level code are used. However, considering how such code may be written clarifies what is going on "under the hood" with high level languages.

To pass data to an assembly language subroutine, there are normally four broad options:

Global variables. It is very easy to understand the mechanism of just sharing storage between parts of the code, but use of global variables is generally frowned upon, as it can make code of non-trivial complexity more difficult to understand and maintain. However, in very simple cases, it may be an acceptable and efficient approach.

Registers. For most CPUs, registers offer very fast access and flexible addressing modes, so they are an attractive parameter passing mechanism, if only a small amount of data needs to be transferred. It can often work out well, if the required data is already in an appropriate register. Without care, however, this mechanism can become as unmaintainable as the use of global variables. Clear and well documented programming standards need to be implemented so that the use of registers is clear and rational.

Stack. Most CPUs use the stack to store the return address during a subroutine call, so it seems a logical place to store parameters. Indeed many processors are designed so that addressing relative to the stack pointer (SP-relative addressing) is efficient. However, some smaller devices do not have such addressing modes. This approach yields reentrant code, which can be vital.

Dedicated memory block. This approach makes sense if the CPU's addressing modes are limited.

There are, of course, hybrid approaches that combine elements of these four. For example, a block of memory (which may have been dynamically allocated) can be used to pass the bulk of the data, the address of which is passed in a register or on the stack.

Parameters in C
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, as it is usually efficient and reentrant), registers or something else.

Passing by value
Parameters in C are always passed by value. This means that each parameter's value is evaluated and the result passed to the function. This may not be apparent if you look at a call like this:

afun(x, y);

But it is much more obvious if the call takes this kind of form:

afun(x+1, y+2);

This passing mechanism means that a function like this will not work as intended:

void swap(int a, int b)
{
 int temp;

 temp = a;
 a = b;
 b = temp;
}

A call like this will have no effect:


 swap(x, y);

To achieve the expected result requires the use of pointers:

void swap(int*a, int*b)
{
 int temp;

 temp =*a;
 *a = *b;
 *b = temp;
}

And a call would need to look like this:

 swap(&x, &y);

Pointers to x and y are evaluated and their values passed as parameters to the function. This achieves the effect of passing parameters by reference.

Passing by reference
Passing parameters by value is mostly useful, but there are times when a reference is better. Sometimes this is for convenience, as shown above, and pointers enable the required result. On other occasions it is more efficient to pass larger quantities of data by reference. This may also be done using pointers, thus:

ptr = (int *)malloc(1024);
...
afun(ptr);
free(ptr);

1 • 2 Next Page Last Page



Comment on "Understanding function parameters"
Comments:  
*  You can enter [0] more charecters.
*Verify code:
 
 
Webinars

Seminars

Visit Asia Webinars to learn about the latest in technology and get practical design tips.

 

Go to top             Connect on Facebook      Follow us on Twitter      Follow us on Orkut

 
Back to Top