function declare

Mr. Andrei Chichak groups at chichak.ca
Wed Jun 26 18:53:37 UTC 2019


Assume that you are using C and C has a facility to bind together object modules from different files.

You define a function in one file, it has a bunch of parameters and doesn’t return anything.

In another file you use that function but don’t have a prototype that tells the compiler anything about the function.

Now assume that the compiler was written in 1972, and doesn’t benefit from things like mind reading.

C assumes, by default, that a function has one integer parameter and returns an integer, unless otherwise told. What other choices has it got? Nothing really. 

So in later versions of C, they introduced a thing called a prototype that gives the compiler a hint about the parameters and return values of a function.

All that you needed was to take your function header, put a semicolon on the end and paste it before the call to the function and the compiler would have done what you wanted instead of what you said.

Better yet, take that prototype and put it into a header file and whenever you want to call the function, put in an include statement, the compiler get’s the information about the parameters and return values, and overrides the int func( int) default nature.

A

> On 2019-June-26, at 12:41 AM, Jython <googcheng at gmail.com> wrote:
> 
> I have a function named  oled_write_temp in a.c, if i dont declare it in a.h , then i will get the wrong supply value in the function within main call, why?
> 
> if i  declare it in a.h, then all is right
> 
> void oled_write_temp(float set_temp, const float supply, float return_temp)
> {
> 
>     int base_x = 12, base_y = 60;
> 
>     char buffer[20] = {0};
> 
>     printf("supply_temp %f at %s\n", supply, __FUNCTION__);
>     
>     snprintf(buffer, 20, "SUPPLY:%.2f", supply);
>     printf("supply_temp string %s\n", buffer);
> 
>     oled_clear_area(base_x, base_y + 16, 122, base_y + 32);
>     OLED_ShowString(base_x, base_y + 16, buffer, 16, 1);
> 
>     memset(buffer, 0, sizeof(buffer));
>     snprintf(buffer, 20, "RETURN:%.2f", return_temp);
>     OLED_ShowString(base_x, base_y + 36, buffer, 16, 1);
> 
>     memset(buffer, 0, sizeof(buffer));
>     snprintf(buffer, 20, "SET   :%.2f", set_temp);
>     OLED_ShowString(base_x, base_y + 56, buffer, 16, 1);
> 
> }
> 
> ps: watch line  printf("supply_temp %f at %s\n", supply, __FUNCTION__);
> _______________________________________________
> users mailing list
> users at rtems.org
> http://lists.rtems.org/mailman/listinfo/users


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/users/attachments/20190626/17ad0bd7/attachment.html>


More information about the users mailing list