Thread: advantages of functions!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    12

    advantages of functions!

    What are the advantages of using functions in c programming? eg what advantage does putting a maths equation in a function have compared with putting it in the main body of code!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    For one, if the function is needed multiple times, you save on code size (no repeated code) by using a function. Which would make for easier maintainability of the code.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by yellowmania View Post
    What are the advantages of using functions in c programming? eg what advantage does putting a maths equation in a function have compared with putting it in the main body of code!
    It is all about suitable abstraction.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by yellowmania View Post
    What are the advantages of using functions in c programming? eg what advantage does putting a maths equation in a function have compared with putting it in the main body of code!
    Modularity...

    Create and test a function once, use it multiple times throughout your code.

    Local variables are contained and valid only as needed.

    Activity inside the function does not impact code outside the function.

    Need to change the calculation? Change it in one place, recompile, and everything is updated.

  5. #5
    Registered User mdj441's Avatar
    Join Date
    Aug 2011
    Location
    NYS, USA
    Posts
    18
    And just think about functions you already use: imagine if you had to rewrite the entire code for printf() in main() every time you wanted to print some text on the screen. Pretty ridiculous! Where would you even start?

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Quote Originally Posted by yellowmania View Post
    What are the advantages of using functions in c programming? eg what advantage does putting a maths equation in a function have compared with putting it in the main body of code!
    Look at it one way, you are making a simple program that adds two numbers (i.e. c = a + b).

    And let's say, you are doing it in multiple places, and for arguments sake lets say you added 1,000 numbers, and your code is now 50,000 lines long.

    Now you are running the program and find out that the calculation is off.

    The question is now, which one of the 1,000 (c = a + b) did you screw up and where in the 50,000 lines of code is the mistake at? With a function it allows you to change your code at one spot vs multiple spots.

    That is one example, but there a numerous other examples.

    And FYI - The question sounds a lot like a HW question. If you were in need of a HW question being answered, read your text book first. The answer would have been within the first couple of pages on your chapter about functions.
    Last edited by Strahd; 12-01-2011 at 10:49 AM.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    One advantage is that you can give a better idea of how your programme works!

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by mdj441 View Post
    And just think about functions you already use: imagine if you had to rewrite the entire code for printf() in main() every time you wanted to print some text on the screen. Pretty ridiculous! Where would you even start?
    You know what, before I read this, it was exactly what I was going to write myself!
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advantages of using C first?
    By dxfoo in forum General Discussions
    Replies: 46
    Last Post: 03-26-2010, 09:57 PM
  2. advantages of using IDE
    By surrounded in forum C Programming
    Replies: 8
    Last Post: 03-03-2009, 06:36 AM
  3. advantages and disadvantages in c
    By cnu_sree in forum C Programming
    Replies: 4
    Last Post: 11-06-2007, 09:29 AM
  4. What advantages does C# have?
    By jsrig88 in forum C# Programming
    Replies: 10
    Last Post: 01-13-2007, 07:00 PM
  5. functions (advantages/disadvantages)
    By aramsahai in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2004, 09:45 AM

Tags for this Thread