{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Functions1 - What is a Function A function is a subprogram...

This preview shows pages 1–3. Sign up to view the full content.

What is a Function? A function is a subprogram that completes a specific task. In fact, main is really just a function. It's special in that it IS the only function that is directly executed. main's specific task is to run the entire program. However, in writing a program you may find it useful to use a function. Once a function is written (we'll talk about how to do this a bit later), we can use it. In fact, if someone else has written a function for us, we can use it as well! All you have to do to use a function is adhere to the rules of CALLING that function. When we are calling a function, an analogy that might simplify our work is the black box analogy. You can think of a function as some sort of device you don't necessarily understand the inner workings of, such as a cell phone, the inside of which, is essentially a black box to you since you don't understand all the complex math and the materials that goes into transforming sound energy into electrical energy and then sending that signel through the air, etc. BUT, most of you can use a cell phone perfectly well. All you have to do is follow the directions: For example, if you want to make a call, dial in the 10 digit number and then hit the send button. In order to use a function, the rules we will have to follow are as follows: 1) Give the function information it needs to finish its task. 2) Use the information the function returns properly.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
I will talk about these more as we see examples of functions, but I want to at least give you an idea of what pre and post- conditions are, and why they are important. One analogy that I often use to describe a function and its relationship to an program is a boss/employee analogy. Consider that you are the boss of a company. Now, as we all know, the boss does NOT do all the work. (In fact, many would argue that they do little compared to others involved in the company!) However, the boss must have a good idea of the goal of the company, and also have a “game plan” for achieving that goal. Certainly, there is too much work for the boss to do all by himself. However, he has employees!!! Thus, the boss’s job is to find a way to utilize these employees so that he can achieve the company’s goal. This sounds easy enough, but there is some difficulty here. First of all, it seems extremely unlikely that each employee will understand the “big picture” the way the boss needs to. It would be difficult to understand the “big picture” AND be able to carry out specific tasks with the attention to detail that the employee is expected to observe. Thus, the boss can not assume that the employee understands what the overall goal of the company is and how his/her specific task relates to that goal. (Granted this should never be true in real life, but it aids my
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 13

Functions1 - What is a Function A function is a subprogram...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online