Thread: problem with functions

  1. #1
    Registered User
    Join Date
    Nov 2007

    Unhappy problem with functions

    I can run the following program

    int i,j;
    printf("enter number \n");
    int we(int a)
    return a;
    It does not have a function prototype but it works
    But I applied the same concept in using arrays accessing a function which takes a float as an input and produces a float as an out put it is as follows

    #define p 3.14
    int n,i=0;
    float rad[100],area[100];
    printf("enter rad\n");
    printf(" rad?\n");
      if (rad[i]<0)
    printf(" rad2?\n");
    printf("summary of results\n");
    for (i=0;i<=n;++i)
    printf("rad = %f  area = %f\n",rad[i],area[i]);
    float process(float r2)
    float a;
    return (a);
    But this program compiles but does not calculate the value


    because when I checked inside PROCESS the r2ís value=0

    can anybody help me please?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2007
    There's almost no reason at all not to use prototypes. They've been standard since 1989. Using prototypes (properly) will fix your problems, as I assume you know.

    At any rate, if you don't use a prototype, the compiler will assume your function looks like:
    int f();
    The int of course means it returns an int. The () means the arguments are unspecified. One side-effect of this is that arguments are subject to the default argument promotions. The upshot in this case is that floats are promoted to doubles. Your function (whose definition contains a prototype) says it expects a float as an argument, but that's not possible. Any float, due to the lack of a prototype at the time of the function call, will be passed as a double.

    The same problem rears its head in the return value, too: the compiler thinks your function returns an int, but it doesn't. That's a problem. Even in the K&R days you needed to declare functions that returned something other than int, and today you should not only declare, but prototype. The reason your first example worked is because it returned an int (which the compiler assumed) and it was passed an int (which isn't promoted in the absence of a prototype: an int stays an int).

    To make a long story short: Use prototypes. List prototypes before you call functions. Be happy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 08:04 AM
  2. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 10:40 AM
  3. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM
  4. Problem with functions
    By lizardking3 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2003, 04:34 PM
  5. problem with functions and "parse errors"
    By bart in forum C++ Programming
    Replies: 3
    Last Post: 08-27-2001, 08:52 AM