Like Tree3Likes
  • 1 Post By phantomotap
  • 2 Post By anduril462

Can functions be used too much in a program?

This is a discussion on Can functions be used too much in a program? within the C Programming forums, part of the General Programming Boards category; Can functions be used too much in a program? The reason I ask is I like using functions, and having ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    30

    Can functions be used too much in a program?

    Can functions be used too much in a program?

    The reason I ask is I like using functions, and having the main contain very few lines.

    I was left wondering though if the use of so many functions is as efficient as writing the code directly into the main. (On bigger projects I can see that functions are the only way to keep organized, but from lack of wisdom I do not know if it is more or less efficient in that environment as it is in a small program.)

    In what situations are functions used too much, and the opposite?

    At what point would you consider functions unnecessary?

    Performance wise is it better to have functions which contain multiple different processes or to have multiple functions with less processes?

    I hope you can see what I am questioning in my mind. I do not want to use functions too much in the wrong situations, and I want to use them in the best situation.

    I do not know what those situations are.


    Any opinions to help me develop my understanding on this would be greatly appreciated.
    Last edited by Cron; 06-28-2013 at 01:14 PM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    At what point would you consider functions unnecessary?
    O_o

    At what point would you consider your genitals unnecessary?

    *shrug*

    That is a really weird question.

    Anyway, as for your other questions: "It depends.", "It depends.", and "It depends.".

    A good "rule of thumb" is "Functions should do one thing and do it well.". Sure, you can go too far with techniques--like "functional decomposition"--which reduce complexity of individual functions, but until you have a lot of experience under your belt you aren't going to have a good feel for the "cut off" point even if someone tells you. Stick with a good, abstract "rule of thumb" and you should be fine while you are learning.

    Soma
    Cron likes this.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Quote Originally Posted by phantomotap View Post
    O_o

    At what point would you consider your genitals unnecessary?

    *shrug*

    That is a really weird question.

    Anyway, as for your other questions: "It depends.", "It depends.", and "It depends.".

    A good "rule of thumb" is "Functions should do one thing and do it well.". Sure, you can go too far with techniques--like "functional decomposition"--which reduce complexity of individual functions, but until you have a lot of experience under your belt you aren't going to have a good feel for the "cut off" point even if someone tells you. Stick with a good, abstract "rule of thumb" and you should be fine while you are learning.

    Soma
    Sorry what I was meaning to ask in that very ambiguous question was if there was a size of program in which it would be more beneficial to not use functions at all.

    Thanks for the reply.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by Cron View Post
    Can functions be used too much in a program?

    The reason I ask is I like using functions, and having the main contain very few lines.
    I would say that if your program looks like this, you've gone too far:
    Code:
    #include <stdio.h>
    
    void print_hello_world(void)
    {
        printf("Hello, world!\n");
    }
    
    int main(void)
    {
        print_hello_world();
        return 0;
    }
    One way that I sometimes think about my main function is, if I had to describe only the highest-level tasks for my program, like I was giving a 1 minute summary to somebody.
    Quote Originally Posted by Cron View Post
    I was left wondering though if the use of so many functions is as efficient as writing the code directly into the main. (On bigger projects I can see that functions are the only way to keep organized, but from lack of wisdom I do not know if it is more or less efficient in that environment as it is in a small program.)
    Never concern yourself with the efficiency of your program until you have incontrovertible proof that it is too slow. Even when you know it's too slow, function call overhead is so tiny, it's hardly going to make a difference in most cases; best to look at ways to improve your algorithms, which often gives huge speed ups. Besides, if you really need to avoid function call overhead (say, for a function called in a really tight loop that gets run millions or billions of times), you should use the inline keyword, which allows you to have the organizational benefits of functions without the function call overhead (note, as with most things, there is a trade-off).

    Quote Originally Posted by Cron View Post
    In what situations are functions used too much, and the opposite?

    At what point would you consider functions unnecessary?
    I rarely see functions used too much, it's far more common to see not enough use of funcitons. Any time you find yourself re-writing what is essentially the same piece of code you have elsewhere in your program, you should put it in a function. I also use "one screenful" of code as a rough guideline for how long a function should be (note, screenful for me is 70ish lines). There's also the idea of cyclomatic complexity, as a way to gauge when your functions have become too big, complicated or messy.

    Quote Originally Posted by Cron View Post
    Performance wise is it better to have functions which contain multiple different processes or to have multiple functions with less processes?
    Processes is probably the wrong word here, usually in computer speak, that refers to an instance of a running program. If you mean one function with lots of logic doing lots of things, versus several small functions, each doing their own thing, then the answer is several small functions. Refer again to cyclomatic complexity. A function should do one thing and do it well. That way it's easy to design, code, test and maintain. It also facilitates reuse, via separation of concerns, i.e. keeping your code loosely coupled, so changes to A don't automatically break B.
    Quote Originally Posted by Cron View Post
    I hope you can see what I am questioning in my mind. I do not want to use functions too much in the wrong situations, and I want to use them in the best situation.

    I do not know what those situations are.

    Any opinions to help me develop my understanding on this would be greatly appreciated.
    Experience is one of the best teachers. Write, rewrite and maintain your programs, trying new designs and fixing problems with your old design. Always analyze what you do, ask yourself how your structure and breakdown of work into functions facilitates or hinders updating and maintaining your code. Read bad code to learn what not to do. Read good code to learn what you should do. Ask experts for their opinion on your code and how they might improve it.
    Last edited by anduril462; 06-28-2013 at 01:46 PM.
    Cron and Salem like this.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Quote Originally Posted by anduril462 View Post
    I would say that if your program looks like this, you've gone too far:
    Code:
    #include <stdio.h>
    
    void print_hello_world(void)
    {
        printf("Hello, world!\n");
    }
    
    int main(void)
    {
        print_hello_world();
        return 0;
    }
    One way that I sometimes think about my main function is, if I had to describe only the highest-level tasks for my program, like I was giving a 1 minute summary to somebody.

    Never concern yourself with the efficiency of your program until you have incontrovertible proof that it is too slow. Even when you know it's too slow, function call overhead is so tiny, it's hardly going to make a difference in most cases; best to look at ways to improve your algorithms, which often gives huge speed ups. Besides, if you really need to avoid function call overhead (say, for a function called in a really tight loop that gets run millions or billions of times), you should use the inline keyword, which allows you to have the organizational benefits of functions without the function call overhead (note, as with most things, there is a trade-off).


    I rarely see functions used too much, it's far more common to see not enough use of funcitons. Any time you find yourself re-writing what is essentially the same piece of code you have elsewhere in your program, you should put it in a function. I also use "one screenful" of code as a rough guideline for how long a function should be (note, screenful for me is 70ish lines). There's also the idea of cyclomatic complexity, as a way to gauge when your functions have become too big.


    Processes is probably the wrong word here, usually in computer speak, that refers to an instance of a running program. If you mean one function with lots of logic doing lots of things, versus several small functions, each doing their own thing, then the answer is several small functions. Refer again to cyclomatic complexity. A function should do one thing and do it well. That way it's easy to design, code, test and maintain. It also facilitates reuse, via separation of concerns, i.e. keeping your code loosely coupled, so changes to A don't automatically break B.

    Experience is one of the best teachers. Write, rewrite and maintain your programs, trying new designs and fixing problems with your old design. Always analyze what you do, ask yourself how your structure and breakdown of work into functions facilitates or hinders updating and maintaining your code. Read bad code to learn what not to do. Read good code to learn what you should do. Ask experts for their opinion on your code and how they might improve it.

    Awesome information. Thanks for your time.

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    DRY principle - Don't Repeat Yourself. Say you write a few lines of code to get yesterday's date, and need use it over and over in a large application---a simple example:
    Code:
    char yesterday[32]={0x0};
    time_t lt=time(NULL);
    lt-=86400;
    strftime(yesterday, 32, "%B %d", localtime(&lt));
    You could copy and paste that code into other places in your code blocks where you need it, or into separate source files, even. Or have a single copy that lives in just one function in one piece of code that all of your othere code references, passing some kind of string to fill in with "yesterday". Maybe like this:
    Code:
    char *yester(char *dest)
    {
      time_t lt=time(NULL);
      lt-=86400;
      strftime(dest, 32, "%B %d", localtime(&lt));
      return dest;
    }
    The DRY principle refers to the second option. It means you have to go to one place to validate or check your yesterday code, one place to make changes. Instead of going on a hunting expedition for 10 places (or was it 11?) sprinkled everywhere. And making 10 or 11 changes.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    It's very difficult to over-use functions, and it's very easy to under-use them.
    That should tell you which way to aim towards.
    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"

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    It's a fine balance that honestly depends on the programmer's personal preference. I do not think that there is a deterministic method of saying that functions are over or under-used, it all comes down to experience. However, without experience, I'd recommend that one err on the side of too many functions rather than too few.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    132
    If it doesn't slow your program down noticeably, then no.
    What most people do is write that crappy program, then
    after they make the first $ million, they go back and refactor it before someone notices.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    I noticed that there is a related thread in the Tech Board: Should functions be _this_ small?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Functions For My Program
    By nelly26 in forum C Programming
    Replies: 3
    Last Post: 04-03-2012, 03:24 AM
  2. Im new at this and trying to write a program using functions
    By wablackwell in forum C Programming
    Replies: 3
    Last Post: 03-21-2012, 11:40 AM
  3. c program help - functions
    By Sleepwalker817 in forum C Programming
    Replies: 4
    Last Post: 04-27-2008, 05:32 AM
  4. Help please: program using functions
    By ZeroBurn7 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2005, 05:38 PM
  5. Help with program-functions
    By akalvarado in forum C Programming
    Replies: 5
    Last Post: 10-13-2002, 11:36 AM

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