When to define functions in C?


 
Thread Tools Search this Thread
Top Forums Programming When to define functions in C?
# 1  
Old 03-28-2014
When to define functions in C?

Hey everyone. So I'm looking at a few C programming resources, and it seems, by convention how you should write and define a function, is first declare it's existence before your main...then call it somewhere in your main, and then define after, at the end of the program? Is this necessary? I mean can't you declare AND define it in one block of code before your main function? So...
Code:
void exampleFunction ( example parameters..) 
{code code code
exit 0;};

instead of splitting it up :

Code:
void exampleFunction (example parameters..)

int main ()
{blah
void exampleFunction
 blah blah}

void exampleFunction (example parameters...)
{code code code
exit 0;}

It just seems easier to declare and define it all in one step before you even get to the main funtion rather than save it's definition for the end of the code?
# 2  
Old 03-29-2014
The actual function code has to precede the function that calls it. So if main() is below all of the functions that main() calls, your idea will work. Functions can be coded (placed) anywhere in the source file, top or bottom of the code file.

Where all of this becomes messy is in the concept of a call tree. Well written functions do one thing very well. So it is desirable for some bit of repeated logic to be placed in a function and called from different places in the code. In this instance you could have problems with placing a function in your code physically before it gets called by another function, because it may be called in lots of others functions. The model you don't seem to like overcomes this issue. You no longer need to worry about the order of functions written in your code when you create function "prototypes". And place them near the start of the source file, before any function code.

It is typical to place all function prototypes (or whatever you wish to call them) at the very beginning of the file, after the #include section. This also allows you to place this stuff in a separate header file. That lets you then call a bunch of the code in your current code file in other C program files. This is the idea behind a code library: DRY coding - don't repeat yourself. Write a function once, use it forever.

Linking them is beyond the scope of this little message.
These 2 Users Gave Thanks to jim mcnamara For This Post:
# 3  
Old 03-29-2014
And what happens when there are something like 300+ functions, each one from 10-1000 lines long?

All in one file?
# 4  
Old 03-30-2014
In that case, partition your code into a number of source files according to functional area. Create headers (files) corresponding to the functions defined in each source file.

Include the appropriate header in a source file if you need to use a function defined in another source file.
# 5  
Old 03-30-2014
Quote:
Originally Posted by fpmurphy
In that case, partition your code into a number of source files according to functional area. Create headers (files) corresponding to the functions defined in each source file.

Include the appropriate header in a source file if you need to use a function defined in another source file.
OP was supposed to answer that.... Smilie
# 6  
Old 03-30-2014
Quote:
The model you don't seem to like overcomes this issue. You no longer need to worry about the order of functions written in your code when you create function "prototypes". And place them near the start of the source file, before any function code.
.
First off. thanks for the reply, it does make sense, but when you say this.. placing the prototype of the function before the main function. When I declare and define the function doesn't that accomplish the same thing as the prototype? Like if I have a list of prototypes before main, and then they are defined at the end of the file...I'm not seeing how that's different than having a list AND definition in the beginning of the file? Don't they accomplish the same thing?
# 7  
Old 03-30-2014
If you're re-using a library's function, you wouldn't have its definition (code) in your file at all. It's not uncommon to not even have access to a function's code, only the prototype in a header, the compiled binary, and (hopefully) some documentation.

Regards,
Alister

---------- Post updated at 08:49 PM ---------- Previous update was at 08:46 PM ----------

Quote:
Originally Posted by Lost in Cyberia
I mean can't you declare AND define it in one block of code before your main function?
You can. However, for larger projects, that type of organization quickly becomes an impediment.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Define Variables

Hi, I just define the variable in script and use those script in another script but the variable not recognize. test1.sh #!/bin/bash DB="test_db" USR="test_user" PWD="test_pwd" HST="24.254.87.12" test2.sh #!/bin/bash ./test1.sh mysql -u $USR -p $PWD -h $HST... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Define variable from file.

HI I have file A.txt _1A _2A _3A _4A I want define all as different variable. $1A=_1A $2B=_2A $3C=_3A $4D=_4A Now i can use any variable in my script. (3 Replies)
Discussion started by: pareshkp
3 Replies

3. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

4. Programming

#define in c

Hi, I had a head file, looks like #define MIN_NUM 10 #define MAX_NUM 10 is there any way to get "MAX_NUM" from 10? thanks. peter (9 Replies)
Discussion started by: laopi
9 Replies

5. Programming

help with #define in C

if i do this in C #define NUM 1234512345 then how come i cant print it out using int main(int argc, char **argv) { printf("%d\n", NUM); return 0; } well the result is -1219236538, why isnt it 1234512345 ? (7 Replies)
Discussion started by: omega666
7 Replies

6. Programming

#define

Hello, I would like to conditionaly comment in my code source some fields from arrays. So I use the property ## from the #define definition. my code: ... #define slet /##* #define etsl *##/ ... const T_SVT_ADLL_A653_DESC A_DESC = { { slet qwerty etsl SLICING,... (3 Replies)
Discussion started by: cypleen
3 Replies

7. UNIX for Dummies Questions & Answers

#define in perl

Hi friends, I am not sure if perl questions can be raised here. :rolleyes: But I have a doubt if there is a way to do "#define" in perl, like in C. Does anyone know if it is feasible (without CPAN modules)? Thanks, Srini (7 Replies)
Discussion started by: srinivasan_85
7 Replies

8. Programming

mysterious #define

in the header file orville.h, outside of the #ifdef #endif , there is the following #define JOB_CONTROL /* support job-control */ As you can see, the JOB_CONTROL macro has no value associated with it. Here is what I go when I ran grep on the entire source code. $ grep -iR... (6 Replies)
Discussion started by: frequency8
6 Replies
Login or Register to Ask a Question