Just wondering? Is it just to keep the code looking cleaner so you don't cram everything into main? It seems like programming without functions is easier.? thanks
Just wondering? Is it just to keep the code looking cleaner so you don't cram everything into main? It seems like programming without functions is easier.? thanks
O_o
Go and program something that isn't trivial. Anything. Try a "dict" client.
Soma
The only reason you think it's easier not too is because you haven't done it enough yet. It's easier to program with functions.
Think of a function as something that accomplishes a specific task. It begins within the whole with a purpose (and possibly inputs); it has a relationship to the whole in its current state. It becomes the active aspect and can change the state of the whole, even ending the program. It may return data into the procedural stream where it began.
Also, there are a lot of points where "clean code" is synonymous with good, well optimized, efficient, effective code.
Last edited by MK27; 04-21-2010 at 07:31 PM. Reason: speeling
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Supppose there is some sub-task that is part of your program, some action that takes, say, 25 lines of code (just picking a number out of the air), and suppose that same action has to be performed 100 times every time the program runs. Would you rather put those 25 lines in a function and call the function 100 times, or do you think it would be better to write the entire 25 lines 100 times and so have 2500 lines of repetitive code to perform just that part of the job?
I kind of wish that esbo were still around to weigh in on this discussion.
How messed up is that?
I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo
This one is like asking what is the point of showering, or wearing clothes, or taking the train when you can just walk 500 miles, or sunscreen, or food, or...
To all of which I say:
Go ahead, by all means don't! Learn the hard way. This question really needs no explanation. It absolutely is not something that a little time cannot teach you.
Can you imagine explaining what the usefulness of any of the above things is to someone?
Last edited by iMalc; 04-22-2010 at 02:15 AM.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Use of functions, or other sub-program constructs, allows the libraries etc. Instead of having to code the "whole thing" yourself, you use functions/subroutines from a specialised, (or not so specialised), library.
programming without functions is near impossible. You would essentially have to write an entire OS with each program (doesn't that sound like fun?)
There is no printf, no cout, no math function, no nothing. You're just left with primitive data types, addition, subtraction, multiplication, ect (if your processor supports it). and whatever assembly you can muster (without using functions or interrupts).
Come to think of it. since interrupts are somewhat similar to function calls (Ok, they arn't the same, they are similar in that they execute a chunk of code away from the current memory location and push a return address onto the stack), I'm going to have to say that it is pretty much impossible to program completely without functions on an x86 processor, as it will send interrupts whether you like it or not.
In other words. Functions make programming easier. You don't have to worry about how cout << "Cat\n"; works because someone already figured that part out.
But those are functions just like any other. Just because it's compiled into a library doesn't mean that inside the source it's any different than your own custom functions for whatever program you are writing.
If you don't use functions, anytime you want to accomplish task X, you have to recreate every single step leading up to that point.
Do you really think that, after asking the question you just asked, you're in a position to be telling other people that what they're saying is wrong?
Those functions, in libraries(All of the standard library, EVERYTHING YOU USE IN C THAT MAKES IT C) are *exactly* the same as functions you make yourself.
"What's up, Doc?"
"'Up' is a relative concept. It has no intrinsic value."
This gets me every time. It seems like some people think that programming is limited to simple programs with a few variables and maybe a loop.
What baffles me is, where do people think all the software they use on a daily basis comes from? Aliens? Purple sea monsters?
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
WTF!?Purple sea monsters?
Everybody knows that sea monsters are blue.
Soma