Trouble passing args to functions in other files

This is a discussion on Trouble passing args to functions in other files within the C Programming forums, part of the General Programming Boards category; I have a rather large program that is divided into multiple source code files, and compiled using a Makefile. I'm ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    4

    Trouble passing args to functions in other files

    I have a rather large program that is divided into multiple source code files, and compiled using a Makefile. I'm having problems passing arguments to functions that are in other source code files. The called function seems to ignore the values that are passed in. Indeed it doesn't even care if the right number of arguments are passed in at all.

    The following code should illustrate my problem:

    first file: main.c
    Code:
    #include <stdio.h>
    
    main()
    {
      write_arg(1.0,2.0,3.0);
    }
    second file: sub.c
    Code:
    #include <stdio.h>
    
    int write_arg(float x)
    {
      fprintf(stdout,"value passed in is %g\n",x);
    }
    Makefile:
    Code:
    CC = /usr/bin/gcc
    
    test: main.c sub.c
            $(CC) -o test main.c sub.c
    If I move the function write_arg in the same file as main(), then the program correctly fails to compile, because it is being called with too many arguments. But if I put write_arg in a separate file, as I did here, then the program compiles and runs successfully, and I get this output:

    value passed in is 0

    In other words, all of the (too many) values that I passed in were ignored. Can anyone explain to me why?

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,439
    Quote Originally Posted by Midnight Coder
    In other words, all of the (too many) values that I passed in were ignored. Can anyone explain to me why?
    Because you called the function without declaring a prototype for it. You should include the header file that you created for the function prototype, or write out the prototype before the main function definition in main.c.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Sure enough, adding a function prototype fixed the problem.

    Thank you!

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Actually, now I have a followup question.

    I will need to go back through my source code files and make sure that I have added function prototypes to all of them. Is there a compiler directive that I can issue to gcc that will make it warn me if I forget to add a prototype for some function, rather than just compiling and acting like there is no problem?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    -Wall works (and gives some other handy things beside). There's probably one specific for prototypes (I think it's -Wno-prototypes or something similar).

    Edit: No, I'm wrong according to the manual, it's -Wimplicit-function-declaration.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Also, main returns int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Thank you all for your help! You've saved me a lot of time and aggravation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Passing functions between files
    By Duskan in forum C Programming
    Replies: 9
    Last Post: 04-17-2007, 07:44 AM
  3. Passing data/pointers between functions #2
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 08:49 PM
  4. Passing data/pointers between functions
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-01-2001, 11:59 PM
  5. functions & external files
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-24-2001, 03:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21