if a function that doesn't pass any value and doesn't return anything.
How to use this function in main.c file
file.c
Code:void add(void) { int x = 2; int y = 5; int z; z = (x + y); }
if a function that doesn't pass any value and doesn't return anything.
How to use this function in main.c file
file.c
Code:void add(void) { int x = 2; int y = 5; int z; z = (x + y); }
You can call this function from main(), and it does execute the code in add(), however, without passing data to the function and returning a value, it really does nothing.
Whatever material you are using to learn C, please throw it away and obtain one of the three books I have mentioned in a previous post here.Code:#include <stdio.h> void add(void); // Prototype int main(void) { add(); return 0; } void add(void) { int x = 2; int y = 5; int z = 0; // Initialize all local variables! z = (x + y); }
Maybe I almost understood use of extern keyword in c language
When I ran cod I get output 18
warning: implicit declaration of function 'addition' [-Wimplicit-function-declaration]Code://file1.c extern void addition(int a, int b) { int c; c = ( a + b ); } //file1.h extern void addition (int a, int b); //main.c #include<stdio.h> #include"file1.h" //missing prtotype function ? int main() { int result; int x = 8; int y = 10; result = addition (x, y); printf("%d\n", result); return 0; }
result = addition (x, y);
How to remove warning ?
I think I am missing prototype function in main.c
First of all, you really need to stop worrying about using the keyword, "extern" with functions! ALL functions are "extern" by default, so using "extern" on the function declaration or definition, is actually redundant!
As for your code, please view the corrections I have made to your code:
main.c (I usually name the file containing the main() function, the same name as the finished program name. Lets call it foo.c.
foo.c
foo.hCode://foo.c #include<stdio.h> #include"foo.h" int main() { int result; int x = 8; int y = 10; result = addition (x, y); printf("%d\n", result); return 0; }
file1.cCode://foo.h int addition (int a, int b);
Code://file1.c #include"foo.h" int addition(int a, int b) { int c; c = ( a + b ); return c; }
compile command:
The function, addition() MUST be declared "int" not "void", in both the declaration and definition and needs to return the value contained in the local variable c from addition()!Code:gcc -Wall -Wextra -Wpedantic -o foo foo.c file1.c
Last edited by rstanley; 08-28-2021 at 03:02 PM.
That is what confuse me. can you explain why can't it be. why "int" and why not "void"
definition of extern keyword say any function is in another file then it can be access using extern keyword
I have no problem with function that declared "int" I already tested code that return int type
The int before the function name, "addition" tells the compiler what type is being returned from the function. In this case, the integer type of the local variable, "c". If that was "void", then you are telling the compiler no data will be returned.Code:int addition (int a, int b);
You then need the line "return c;" to actually return the value to the calling function.Code:int addition(int a, int b) { int c; c = ( a + b ); return c; }
You are also hung up on the keyword, "extern".
An extern function, is simply a globally declared and defined function. Please, just ignore this keyword, and concentrate on how to create a function DECLARATION, DEFINITION, and how to CALL various functions.
Look at my code above! I don't use the keyword, "extern"! I don't need it and neither do you!
If you would study a up to date book on the C Programming language, you would understand this and all the other features of the language, and how to use them.