Thread: extern function

  1. #16
    Registered User
    Join Date
    May 2021
    Posts
    51
    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);
    }

  2. #17
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by Rahul11 View Post
    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.
    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);
    }
    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.

  3. #18
    Registered User
    Join Date
    May 2021
    Posts
    51
    Quote Originally Posted by rstanley View Post
    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.
    .
    Maybe I almost understood use of extern keyword in c language

    When I ran cod I get output 18
    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;
    }
    warning: implicit declaration of function 'addition' [-Wimplicit-function-declaration]
    result = addition (x, y);

    How to remove warning ?

    I think I am missing prototype function in main.c

  4. #19
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by Rahul11 View Post
    Maybe I almost understood use of extern keyword in c language

    When I ran cod I get output 18
    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;
    }
    warning: implicit declaration of function 'addition' [-Wimplicit-function-declaration]
    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
    Code:
     
    //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;
    }
    foo.h
    Code:
    //foo.h
    
    int addition (int a, int b);
    file1.c
    Code:
     //file1.c
    #include"foo.h"
    
    int addition(int a, int b)
    {
        int c;
        c = ( a + b );
    
        return c;
    }

    compile command:
    Code:
    gcc  -Wall -Wextra -Wpedantic -o foo foo.c file1.c
    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()!
    Last edited by rstanley; 08-28-2021 at 03:02 PM.

  5. #20
    Registered User
    Join Date
    May 2021
    Posts
    51
    Quote Originally Posted by rstanley View Post
    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()!
    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

  6. #21
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by Rahul11 View Post
    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
    Code:
     
    int addition (int a, int b);
    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)
    {
        int c;
        c = ( a + b );
    
        return c;
    }
    You then need the line "return c;" to actually return the value to the calling function.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-25-2017, 10:25 AM
  2. Using extern on function so it can be used in other files
    By deathslice in forum C Programming
    Replies: 3
    Last Post: 02-29-2016, 12:59 PM
  3. In .h files, should function declarations be 'extern' ?
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 02-28-2011, 07:56 AM
  4. question about declaring function extern
    By movl0x1 in forum C Programming
    Replies: 2
    Last Post: 07-25-2007, 05:16 AM
  5. extern outside of a function
    By spank in forum C Programming
    Replies: 7
    Last Post: 07-10-2007, 09:43 AM

Tags for this Thread