Why Use Functions

Why Use Functions

Lesson Details:
July 10, 2020


I: Introduction

A: Why use functions

1. Functions are used to organize code

2. Functions can be reused

3. Functions can be made up of multiple lines of code

4. Functions help prevent bugs

B: But, there are some downsides to using functions

1. Function names must be descriptive enough to describe the functionality of the function

2. Functions should be organized in a logical manner

II: Body

A: Why use functions

1. Functions are used to organize code

Functions allow the programmer to group similar blocks of code together into one place. This helps organize the program and makes finding specific functions easier. When using functions, variables that are declared within the function are only accessible within that function. This helps maintain data integrity by preventing accidental or malicious changes to variables that are not intended to be changed. Also, this allows the programmer to change the variable’s value without affecting other parts of the program which depend on the variable. Functions also provide a way to break down complex problems into smaller problems that can be solved separately. This is done by writing small, focused functions that rely on an underlying set of functions that carry out all of the heavy work, ultimately making it easier for the programmer to identify possible errors in the code. Finally, functions allow programmers to write code that is able to be used in many different programs. A good example of this is C++’s standard library which provides many functions that can be used in any program written with C++. This allows programmers to write code once and use it again without having to rewrite it.

2. Functions can be reused

Functions are used to make code reusable, allowing programmers to reuse code without having to rewrite it each time it is used. This leads to saving time by not having to rewrite the same thing more than once, which saves money for companies because they don’t have to pay their programmers as often. Also, this leads to less bugs being present in the final product since any bugs found will already have been fixed once before being used in the next program. Another advantage of developing reusability is that it reduces complexity because multiple components can be developed independently so long as they adhere to a standard interface. By doing this, new features are added faster because developers are working more on independent components rather than trying to figure out how they all fit together. This also leads to fewer errors and less testing because the components will already have been tested separately and won’t need to be retested after they are integrated into a system.

3. Functions can be made up of multiple lines of code

Multiple lines of code can be written inside a function, which allows for better organization of code and less chance for error. It also improves readability because the code is placed in nice readable chunks instead of all on one line like an assignment statement. This also makes it easier for the programmer to see the logic behind the code, which helps catch mistakes before they happen and allows them to easily find an error if it does occur. It also alleviates confusion when different parts of a program need multiple lines of code because it makes finding what you need much easier and reduces frustration and time spent searching through multiple files for a specific piece of code. Finally, it separates different parts of a program so that they do not overlap and lead to confusion as well as reduce complexity by combining related tasks into one section of code instead of having them scattered throughout the program.

4. Functions help prevent bugs

Functions help prevent bugs by providing a single location to change something related to that part of the program instead of having multiple copies spread throughout the program where changing one might affect another causing unintentional changes somewhere else in the program causing a bug. Also, functions provide a way for programmers to check how different parts of a program interact with each other reducing the occurrence of a bug caused by a simple misunderstanding between two different parts of a program. Also, functions help avoid common coding mistakes such as missing end-brackets or mismatched types by requiring that all statements belong to a function which then makes it easier for programmers to detect these errors earlier during development before any code is created.

loader
Course content