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...
instead of splitting it up :
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?
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:
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.
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.
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?
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.
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)
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)
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)
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)
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)
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)
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)