This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Function with different types of arguments

Hello!
In the program writen in Keil uVision 4, I decided to redo some of the functions and added the structure:

typedef struct
{
  GPIO_TypeDef * GPIOx;
  uint16_t PINx;
}GPIO_PINdef;

Previously, the function looked like this:

void GPIO_InitPIN (GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin_x)

Now it looks like this:

void GPIO_InitPIN (GPIO_PINdef GPIOx)

So here is the question, calling a new function now works both in the old and new way, i.e. to call a function in this form 

GPIO_InitPIN(GPIOB, GPIO_Pin_8)

and in this form 

GPIO_PINdef SPI_CS;
GPIO_InitPIN (SPI_CS);

the compiler does not swear and the program runs correctly.

But everything was fine until I tried to fix "warning" during compilation, namely "function" GPIO_InitPIN "declared implicitly". I copy the function description into the header file and after that everything stops working:

void GPIO_InitPIN (GPIO_PINdef x);

 - adding this description, swears that there are too many arguments when calling this function 

GPIO_InitPIN(GPIOB, GPIO_Pin_8)

How can this function be written in the header?

  • You're assuming arguments are pushed on a stack, in an optimized build, the parameters to the 2 parameter function are probably in registers, and have no address relation to each other.  

    One way; you could do a

    union funcTypes { void (*INitStruc)( GPIO_PINdef ); void (*InitParams)( GPIOB, uint64_t ); } ;

    void realFunction( GPIO_PINdef );

    #define paramCall( a, b )   ((union funcTypes*)realFunction).InitParams( a, b ) 

    #define strucCall( a )   ((union funcTypes*)realFunction).InitStruc( a ) 

    but really - just standardize on one or the other and don't do any of the above :)