Function Placement

A

Al Vazquez

Should one first declare empty functions in main and define the
functions below main like so:


int main()
{
int function1(), function2();
function1("hello world");
printf("%d",function2(1,2));
return 1;
}

int function1(char s[])
{
printf("%s";s[]);
return 1;
}

int function2(int a, int b)
{
return a+b;
}



or should one define the functions above main and leave main at the
bottom of all the code? Is this defined in the standard?

TIA,
A.V.
 
P

polimera

One has to declare the function before using whether it is before main
or inside the main.
If the definition itself is present before the usage of function it is
well n good.
 
Z

Zoran Cutura

Al Vazquez said:
Should one first declare empty functions in main and define the
functions below main like so:


You don't declare empty functions, a declaration just tells the
implementation (usually a compiler) that there exist a function
somewhere that needs to be used in a certain way (i.e. arguments
expected and the return type)


int main()
{
int function1(), function2();

Such declarations (though they are allowed in here) are usually done at
file scope (outside any functions) and need to be available before the
functions are called.

But you have declared your functions in a wrong way. If you don't tell
the compiler what parameters the function expects, it cannot check
whether you use it correctly or not. So you really should have

int function1(char []);
int function2(int, int);

here.
function1("hello world");
printf("%d",function2(1,2));
return 1;

When you return from main, you should only use 0, EXIT_SUCCESS or
EXIT_FAILURE. The later two are defined in stdlib.h which needs to be
included.
}

int function1(char s[])
{
printf("%s";s[]);

This is a syntax error. You probably meant

printf("%s\n", s);

note that I also added a newline character to be sent to output, because
otherwise the output might not appear.
return 1;
}

int function2(int a, int b)
{
return a+b;
}



or should one define the functions above main and leave main at the
bottom of all the code? Is this defined in the standard?

That is merrily a matter of taste. With bigger projects you'll discover
that you need more than one source file and functions from one file will
be used in another, so you're probably going to provide the declarations
through some header-file that will be included in the source-files that
need 'em.
 
R

Richard Bos

Al Vazquez said:
Should one first declare empty functions in main and define the
functions below main like so:
No.

int main()
{
int function1(), function2();

It's rarely wise to declare functions inside one other function.

It's never wise not to use proper prototypes, unless one is forced to be
compatible with compilers from the late Cretaceous.
function1("hello world");
printf("%d",function2(1,2));

Calling a variadic function without a prototype in scope invokes
undefined behaviour. You need to #include said:
return 1;

Using anything else than 0, EXIT_SUCCESS or EXIT_FAILURE here returns an
undefined value to the environment.
}

int function1(char s[])
{
printf("%s";s[]);
return 1;
}

int function2(int a, int b)
{
return a+b;
}

or should one define the functions above main and leave main at the
bottom of all the code?

That's one option. Another is to learn to use real prototypes:

#include <stdio.h>

int function1(char s[]);
int function2(int a, int b);

int main(void)
{
function1("hello world");
printf("%d",function2(1,2));
return 0;
}

int function1(char s[])
{
printf("%s";s[]);
return 1;
}

int function2(int a, int b)
{
return a+b;
}

If you have many functions, or you want to use them in other source
files, you will want to put the prototypes in a header file and #include
that.
Is this defined in the standard?

Which is better isn't. The use of prototypes and #include is.

Richard
 
B

Ben Pfaff

Al Vazquez said:
Should one first declare empty functions in main and define the
functions below main like so:

In general, code should be written as it is intended to be read.
Put your functions in the order that makes the code easiest for
another programmer to understand them. That often means that the
functions that get invoked first, such as main() and other
high-level functions, should be early in the source file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top