Here's an example of two programs that do the same thing, I have been taught how to do them like the first way. Now, I'm reading an ebook and it explains to do them like the second way. Which way do you think is more proper?

thanks

#include <stdio.h>

int main()
{
[B]float convert(float);[/B]
float number,newnum;

puts("Please enter number");
scanf("%f", &number);
newnum=convert(number);
printf("Number by 2.5=%.2f", newnum);
return(0);
}


float convert(float number)
{
    return(number*2.5);
}
#include <stdio.h>

[B]float convert(float number);[/B]

int main()
{
float number,newnum;

puts("Please enter number");
scanf("%f", &number);
newnum=convert(number);
printf("Number by 2.5=%.2f", newnum);
return(0);
}


float convert(float number)
{
    return(number*2.5);
}

Recommended Answers

All 3 Replies

The first one allows you to have tighter control of the scope; that is, which functions are able to "see" that prototype. In this case, only main. If you want a function available to more than one function, however, you would need to repeat the prototype in each function that calls it.

The second makes the function visible to all functions the follow. This is more common.

It's up to the programmer, really, based on the given situation(s) of the code.

commented: Right ~~ SunnyPalSingh +3

I prefer the second form. I personally don't like prototypes in a function body. They get lost too easily.

You also don't need so many lines between code blocks. The farther they are away from each other the harder it is to compare.

commented: Same here: SunnyPalSingh +3

Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.