#Defining functions below main().

126 messages · Page 1 of 1 (latest)

vital shoal
#

Hi lets say I have a basic function add(). I want to have it below main. This is possible, but I forgot how.

#include <stdio.h>

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

int main()
{
    printf( "%d", add(2,2) );
    return 0;
}```
cyan archBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

mellow island
#

Put the function declaration above main but the implementation below

vital shoal
#

How can I put a function declaration above?

#

I tried it with int add() {} but that does not work.

#

@mellow island

#
#include <stdio.h>

int add() {}

int main()
{
    printf( "%d", add(2,2) );
    return 0;
}

add(int a, int b)
{
    return a + b;
}
mellow island
#

Almost

#

It should be

int add(int a, int b);

#

At the top

vital shoal
#

Ah okay so I have to predefine the arguments...

#

Anyway thanks, works now!

#

Does it matter if I put int before add below?

#

Nevermind. Seems to be required.

mellow island
#

Otherwise it is a different function

#

You have to put the return type

#

Functions have arguments and a return type, all have to be identicle

vital shoal
#

I see, good to know.

#

I am starting to learn C and right now I am building some basic blocks so I can combine them later to create working code.

#

_ _
So since I really dislike having anything above my main() function (distasteful style in my opinion) it might be better to just import the code.

#

So next up is to have a file that contains all the nasty imports and only import this so I can have a clean main function that I am working on.

mellow island
#

Just use header files

vital shoal
#

Ah! Heard about these.

mellow island
#

Where are you learning about c?

#

Or how rather?

#

I recommend Beej's guide to c

#

It's free online

vital shoal
#

So far I use online resources such as complete cources CS50 e.g. on youtube.

mellow island
#

As long as you have a single cohesive resource you should be good

vital shoal
#

Yes. My first programming language was Python and I feel like I can do a lot with it quickly enough now.

#

However performance of Python is slow.

#

That is why I want to learn C. I will continue to use Python for quick scripting.

#

And C to do fast calculations.

mellow island
#

Cool yeah

#

I'm writing a program in c now to run on a supercomputer

#

Using MPI

#

OpenMPI

#

I've told my housemate I will teach him c tomorrow so he can do the same

vital shoal
vital shoal
mellow island
#

But using multiple sources can make it easier to confuse yourself early on.

#

For a lot of things

#

Different people have different ways of doing things

#

Tbh with c it's probably not too bad

#

But for other things (like maths and physics) it's important

#

So I'm a little stricter on thay front

vital shoal
#

I think since I already learned one language I have some sort of pattern in mind. First of all I focus to learn to do what I already can do in Python.

mellow island
#

(I'm a physics student)

vital shoal
#

Oh I am doing a lot of maths to. So I know what you mean.

#

Currently doing information entropy.

#

Not quite sure if that is the correct term. Well part of it is for example Huffman Coding.

cyan archBOT
#

@vital shoal Has your question been resolved? If so, run !solved :)

vital shoal
mellow island
#

Lol I could

vital shoal
#

And for what are you using that supercomputer?

mellow island
#

Like galaxy collision simulations

mellow island
vital shoal
#

Sounds cool.

mellow island
#

Pointers and const correctness

#

Are 95% there

vital shoal
#

I looked for the guide of Beej.

#

Am I overlooking something?

#

I have found a weird html website.

#

Can you provide the link?

mellow island
vital shoal
#

Oh wow, that's nice.

#

Can you tell me how I can import a header?

#

Probably: #include "header.h"

#

why not <header.h>

mellow island
#

Yep

#

The angle brackets are for standard library things

#

BTW include literally tells the preprocessor to copy and paste from header.h to the file

vital shoal
#

Awesome. It works.

mellow island
#

Before compilation starts

vital shoal
#

Better than having to deal with ugly code while production.

mellow island
#

Yep

vital shoal
#

What about #include "main2.c"

#

I can also include .c stuff, but why would I do it?

mellow island
#

Makes compilation easier sometimes

#

I mean I'm doing that in my program

vital shoal
#

How do I know if I should use a header.h or a main2.c

mellow island
#

Header files normally only contain function declarations

#

You can do what you like

vital shoal
#

Oh hm...

mellow island
#

I guess it will come with experience whichever is better

#

For you

vital shoal
#

Well my header.h contains functions such as:

int add(int a, int b)
{
    return a + b;
}
mellow island
#

Yeah then it's a .c file

#

Not a header file

vital shoal
#

OH...

mellow island
#

Header files only declare functions

#

By convention

vital shoal
#

Yea no I continue convention for now. Probably there is good reason.

#
sub.c:(.text+0x0): multiple definition of `add'; /tmp/cc69VYR2.o:main.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status```
#

I renamed it to "sub.c"

mellow island
#

Yeah the linker is confused. You have two definitions of add, which one should it use? It doesn't know and throws an error

#

Show me the code of both files?

vital shoal
#
// main.c
#include "sub.c"

int main()
{
    printf( "%d", add(2,2) );
    return 0;
}
// sub.c
#include <stdio.h>

int add(int a, int b)
{
    return a + b;
}
mellow island
#

Change add to myadd

vital shoal
#

Well I only know of one add function.

#

Okay.

mellow island
#

Still get the error?

vital shoal
#
sub.c:(.text+0x0): multiple definition of `myadd'; /tmp/ccVnv5MD.o:main.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status```
#

Yep..

mellow island
#

How are you compiling?

#

I think your compiler is being dumb

#

If you change it back to sub.h it works?

vital shoal
#

I will try.

mellow island
#

If so it's your compiler being too clever for it's own good

#

I think what is happening is when it is called sub.c instead of sub.h your compiler is compiling both sub.c and main.c into object files then trying to link them, but becuase main.c also contains add (becuase of the include) there are two definitions

vital shoal
#
#include "header.h"
#include "myadd.c"

int main()
{
    printf( "%d", add22(2,2) );
    return 0;
}```

```c
#include <stdio.h>

int add22(int a, int b)
{
    return a + b;
}
int add(int a, int b);```
#
myadd.c:(.text+0x0): multiple definition of `add22'; /tmp/cclVVaDU.o:main.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status```
mellow island
#

I need to see how you are compiling

vital shoal
#
mellow island
#

Ah

#

Okay

vital shoal
#

I just use an online compiler for quick results.

mellow island
# vital shoal ```c #include "header.h" #include "myadd.c" int main() { printf( "%d", add2...

Can I get back to you tomorrow? I need to head to bed. Header files are basically a promise to the compiler that you know what a function takes as arguments and outputs, but you will provide it later (by linking). So you either use header files or include sub.c

By using both the compiler is getting confused.

Set it up on your own system and have a play around with it.

Use either msvc, gcc, or clang.

Compiling c programs is the hardest part

vital shoal
#

Sure. I will sleep now too.

#

Good night.

#

I have sent you a friendrequest.

cyan archBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.