What is the point of functions?

This is a discussion on What is the point of functions? within the C++ Programming forums, part of the General Programming Boards category; Just wondering? Is it just to keep the code looking cleaner so you don't cram everything into main? It seems ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    What is the point of functions?

    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

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    O_o

    Go and program something that isn't trivial. Anything. Try a "dict" client.

    Soma

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    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?

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by R.Stiltskin View Post
    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?
    Good example. How do you call a function a bunch of times? put the call into a loop?

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    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

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by nick753 View Post
    Good example. How do you call a function a bunch of times? put the call into a loop?
    Sure, why not?

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by nick753 View Post
    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
    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"

  9. #9
    Registered User
    Join Date
    Mar 2010
    Location
    Denmark
    Posts
    52
    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.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    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.

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by Cogman View Post
    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.
    That's not what I mean by functions. I meant the type of function that you have to write a prototype for, and a call for. I wasn't referring to the libraries

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Quote Originally Posted by nick753 View Post
    That's not what I mean by functions. I meant the type of function that you have to write a prototype for, and a call for. I wasn't referring to the libraries
    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.

  13. #13
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by nick753 View Post
    That's not what I mean by functions. I meant the type of function that you have to write a prototype for, and a call for. I wasn't referring to the libraries
    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."

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    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);
    //}

  15. #15
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    Purple sea monsters?
    WTF!?

    Everybody knows that sea monsters are blue.

    Soma

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 06:05 PM
  2. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  3. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 01:37 PM
  4. trouble with overloaded operator
    By kkurz in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2003, 11:59 AM
  5. Point passed rectangle...
    By Hunter2 in forum Game Programming
    Replies: 15
    Last Post: 10-10-2003, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21