• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 15. Combining Statements into Fu... > Writing Better Code with Functions

Writing Better Code with Functions

A function is an entire block of code that you can call, or invoke, from anywhere in a program. Calling the function causes the code in the function to execute. You can call a function using its name, if it has one. You can also call functions via variables, array elements, or object properties. All these ways of referring to a function are much more concise than the function itself. Thus, functions effectively give you short, convenient “handles” for potentially complex behaviors.

Functions make it much easier to maintain programs. For instance, suppose you create a displayError() function to display error messages. If you ever want to change your error display function, you can make the change in only one place, in the displayError() function declaration. If you had instead repeated a block of code multiple times, you would have to edit each duplicate block individually.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint