2

I want to declare a function for use in my code in ANSI C. Am I allowed to define the function after using it in main()? For example an absolute value function that I wrote: can anyone tell me if it's sound?

#include <stdio.h>

int main()
{
    double value1 = -5;
    printf("%lf",
           abs(value1));
}

double abs(double number)
{
    if (number < 0) {
        (number * (-1) = number);
    }
    return number;
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
Andrew Hu
  • 113
  • 1
  • 8

2 Answers2

5

You need to fix a couple of things:

#include <stdio.h>

double abs(double number); // <<< 1. prototype here

int main()
{
    double value1 = -5;

    printf("%lf",
           abs(value1));
}

double abs(double number)
{
    if (number < 0) {
        number = number * -1.0; // <<< 2. fix here
    }
    return number;
}
  1. the abs function needs a prototype so that the compiler knows its function signature before you call it

  2. your expression for negating number was back-to-front

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • +1. As a side note: it is not necessary required to declare a function prototype in C, although it is definitely a good practice: http://stackoverflow.com/a/2575204/814702 – informatik01 Apr 17 '13 at 21:32
  • @informatik01: in this case it *is* necessary, as default int parameters and function result will obviously not work. – Paul R Apr 18 '13 at 06:39
  • In this case I agree. It was just a *general* side note. Couldn't help that, sorry ) – informatik01 Apr 18 '13 at 10:52
  • 1
    @informatik01 It is however necessary to _declare_ a function before calling it, as explained in the answer you linked. A function prototype is a function declaration which specifies the type of the parameters. Obsolete versions of C allowed function definitions even without a declaration. This idiocy was removed in the C99 standard. Unfortunately, you are still allowed to declare functions with no parameters (non-prototype format), but it is flagged as an obsolete feature in the C standard. – Lundin May 20 '15 at 09:41
  • @Lundin With all due respect, I never told about that you don't have to _declare_ a function before calling it. The **only** thing I wanted to empathize (and it was clearly stated in the answer I linked to, in the first sentence) is about function **prototype**. Although, as the author of that answer noted in his additional note, for *some* functions it is a requirement. And I of course agree with Paul R that in his example it was necessary. Anyways, thank you for you comment. – informatik01 May 20 '15 at 12:52
1

Functions must at least be declared before they are used, preferably using prototype syntax (which specifies the number and types of arguments, as well as the return type). So at the very least, you need a line like

double abs( double number ); 

somewhere in your code before you call abs in main.

You may also define a function before it is used:

double abs( double number )
{
   // compute absolute value
}

int main( void )
{
  ...
  x = abs(x);
  ...
}

This is actually my preference; everything in the same translation unit (file) should be defined before it's used. This way you don't have to worry about keeping declarations and definitions in sync.

John Bode
  • 119,563
  • 19
  • 122
  • 198