Can we name functions before defining them?

#include <stdio.h>

void main()

{

    m();

}

void m()

{

    printf("hi");

}

Exit

hi

Warnings

main.c:11:10: warning: conflicting types for 'm' [enabled by default]
     void m()
          ^
main.c:7:9: note: previous implicit declaration of 'm' was here
         m();
         ^

Why does this program work successfully, although m () is called before it is defined? And what is the meaning of the warning?

+4
source share
6 answers

C89 resolves this by implicitly converting the return type of the function and parameter passed to it in int. See here .

But this does not apply to C99 and later. This is excluded from the standard. Either you must declare a prototype of your function, or define it before main. See Result here . In this case, there is a compile-time error.

+6
source

K & RC/C89, int. void, .

:

void m(void);

... , .

+6

. , . .

, Missing .

void m();.

Edit:

C .

void m(); . m .

- ( , , , ): ( wikipedia)

.

+2

- .

..

void m();

+2

, :

#include <stdio.h>

// declare m()
void m();

void main()
{
    // use m()
    m();
}

// define m()
void m()
{
    printf("hi");
}
+2

, .

, () , ().

, , :

1- : , . ( ). (), . , , , , . , .

let lastName = function (family) {
 console.log("My last name is " + family);
};        
let x = lastName("Lopez");

ES6:

lastName = (family) => console.log("My last name is " + family);
x = lastName("Lopez");

2 Slow functions: declared functions with the following syntax are not executed immediately. They are "saved for later use" and will be executed later when they are called (called). This type of function works if you call them BEFORE or AFTER where they were defined. If you call the deceleration function to where it was defined - Lift - works correctly.

function Name(name) {
  console.log("My cat name is " + name);
}
Name("Chloe");

Lift example:

Name("Chloe");
function Name(name) {
   console.log("My cat name is " + name);
}
+1
source

Source: https://habr.com/ru/post/1524758/


All Articles