Chapter 07

Chapter 07 - Chapter 7 User Defined Functions To this...

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

View Full Document Right Arrow Icon
Chapter 7 User Defined Functions To this point, the programs you have written have relied almost exclusively on sequential control. And while you have used control constructs such as loops and conditional statements to alter that sequential processing, there has been one constant in your programming: you have written single m-files to accomplish your task. For small programs, this is not an issue. But imagine you’re writing a program that contains thousands of lines of code or that you’re working on this program with several people. In this scenario, several problems arise. You find that you can’t work on the same m-file simultaneously. Your group members tend to use the same common variable names, so important data is overwritten and lost in the program. Common tasks are programmed repeatedly. In short, you find that using a single m-file creates an inefficient and confusing jumble of code. Thankfully, there is a more efficient way to organize your programming. That method is to use functions. From the moment you started working with Matlab, you have been using functions. Matlab has hundreds of pre-programmed functions to perform common tasks. This chapter will introduce you to a number of concepts geared toward the creation of your own unique functions. 7.1 Introduction to User Defined Functions A function is nothing but a small program that performs a specific task. Recall that in top-down design, algorithm creation is a two-step process. First, you decompose your task into a series of sub-problems and then you perform stepwise refinement to break those sub-problems down even further. Software engineers will typically create a function for every sub-problem in their algorithm. This organization provides a number of advantages: Independence in Writing and Testing Each function acts as a stand-alone program, so when your algorithm is divided into a series of sub-problems or sub-tasks, each programmer could work on an individual task, programming and debugging their functions simultaneously. The separate functions are brought together at the end and used as a seamless program. Perhaps more important than the time savings offered by the division of labor is the independence of variables. An important side effect of using functions is that because each function acts as a stand-alone program, variables are kept separate
Background image of page 1

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

View Full DocumentRight Arrow Icon
and independent. This means that identical variable names can be used in more than one function, but their values won’t be confused or overwritten. This is an extremely important computer science concept known as variable scope, and will be discussed at length later in the chapter. Ability to Reuse Code It is often the case in programming that a single task is needed over and over again in a program. A loop can be used to repeat a task, but that loop might only occur once in a program. By writing that task as a function, you can execute that task at any time in your program. This saves an enormous amount of time because your task only needs to be programmed and debugged once.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/08/2008 for the course CS 103 taught by Professor Fitzpatrick during the Spring '07 term at Vanderbilt.

Page1 / 17

Chapter 07 - Chapter 7 User Defined Functions To this...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online