Benefits of Creating Functions

11 views (last 30 days)
Guan Zhao
Guan Zhao on 28 Mar 2013
Good day,
I am currently working on a project that requires lots of computing for multiple variables, and it made me wonder if grouping the computing into a function and calling that function from the main program code would achieve computational efficiency (i.e. a faster program), as compared to lumping all the computational codes in the main program.
What are the pros and cons of creating a function in this sense?
Regards Guan Zhao
  1 Comment
Wouter on 28 Mar 2013
It's not necessary faster than a script, but using functions can make your code more readable / editable.

Sign in to comment.

Answers (1)

Jason Ross
Jason Ross on 28 Mar 2013
Functions are great for the following reasons (and probably more that don't spring to mind immediately)
  • You will not need to repeat yourself if you are running the same code over and over. The benefits of this are many, but the most prominent one in my mind is that duplication of code inevietably leads to bugs that don't get fixed, and these lead to debugging sessions that are literally a waste of time when you've fixed something once and forgot to update other instances of the same code that were copied and pasted. If you are programming and do a copy and paste of the same code over and over, you should really condsider making whatever you are copying and pasting into a function. It's going to save you a lot of heartache and pain later.
  • What you describe in your question is an example of "pre-optimization". Until you have code running, it's very likely that you have no idea where the performance bottlenecks are going to come from, so lumping everything into one program as a solution before you know the problem isn't going to give you any guaranteed performance improvement -- and it might even make it more difficult to optimize. In addition, if you find out that a function call is where the majority of your time is spent, you can spend time understanding how to improve the performance of that function -- and when you improve that function, you know it has been improved for the entire program, giving you a multiplicitive effect for performance improvement.
  • Reusability. As you develop more and more code that solves similar problems, you'll likely find that you are using similar code over and over again. Put that common code into functions and then re-use them on new projects. You'll get to the interesting part more quickly than having to write the same infrastructure code over and over again. You might even be able to help your group / department / company by publishing a collection of common functions that will let everyone get to the interesting parts of their work rather than having to re-write basic code that loads data files, does a common computation, etc.
  • You can test functions more easily than a whole program, and in far less time. You can also test functions outside of your main program entirely by calling them with test data to ensure you get the desired results. It's also possible to refactor the internals of the function to something that performs better, test the new implementation, then have all your existing code take advantage of it the next time you run it. Imagine the performance improvement on a large code base if some new feature is introduced that improves performance in some manner. You change it once, test to ensure the answers are the same as the previous one, and then all your code reaps the benefit of the improvement the next time it is run!
There are a lot more great reasons that functions exist, but I'd be pretty much writing a "best practices of software design" textbook at some point :)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by