5 User-Defined Functions

This chapter is a work in progress.

All program examples that have been presented up to this point have been in the form of a “script”, which is a largely unstructured code sequence  in a single file. That is sufficient for short, simple programs. For larger, more complex programs, there are advantages to a modular approach, in which the code is divided into sub-programs, each of which performs a task that makes up a portion of the overall algorithm. In various programming languages, these units of code may be called subroutines, procedures, or methods. In MATLAB, they are called functions.

The benefits of a modular program design include:

  • easier debugging and maintenance
  • more compact and readable code
  • facilitation of teamwork

This chapter introduces the basics of writing a user-defined function in MATLAB. Chapter 8 explores the concept of program structure in more detail.

Lecture Video 5.1 – Introduction to User-Defined Functions

 

Relationship of a Function to a Main Program

It is common for a program to perform a given task numerous times. Rather than duplicate the code to perform that task, it can be incorporated into a function, which is called repeatedly. In fact, the scripts that we have presented previously contained many function calls. For example, in this script, every line is actually a function call:

x = linspace (-5, 5, 100);

y = 2.5*sin(4*pi*x);

figure()

plot (x,y)

xlabel('x values')

ylabel('y values')

title ('sine function')

linspace, sin, figure, plot, xlabel, ylabel, and title are all functions that are included in MATLAB. Some, like linspace, are actually written in ordinary MATLAB code, while others, like plot, are implemented in lower-level code. The latter type is called a built-in function. If there is no MATLAB function to perform a task, a user can write a user-defined function, which can be called from various programs just like any other function.

Most function calls, like those in the previous example, have certain elements in common. The general form of a function call is

outputs = function_name (inputs)

Consider the first line of the script:

x = linspace (-5, 5, 100);

The name of the function, linspace, appears just to the right of the equals sign; the output, x, appears to the left of the equal sign; the inputs, -5, 5, 100, are enclosed within parentheses after the function name. The meaning of those values depends on how the function is defined. In the case of a MATLAB built-in or library function, you must refer to the documentation to determine what value(s) to pass to the function and in what order, and what output(s) it returns. The first step in creating a user-defined function is to define its inputs and outputs.

Notice that not all functions return an output – for example, xlabel, ylabel, and title. There are some functions, like figure, that do not require an input.

Defining a Function

In this chapter, all user-defined functions will be external functions – that is, each function will be in a separate file, and the name of the file must match the name of the function. Chapter 8 shows other ways that functions can be organized.

To see the structure of a user-defined function, consider the standard MATLAB function, linspace, which is reproduced below. This function uses many MATLAB features that are covered in later chapters. The key points are highlighted in the comments on the pdf. The most important ones to take note of are:

  • The file name (linspace.m) must match the name of the function (linspace)
  • A function definition always begins with the word function. In an external function, there must be no code before this function definition line
  • The input values are passed to the function by the caller (main script) through its input arguments. Comparing the list of input arguments (d1, d2, n) with the input values (-5, 5, 100) in the function call in the example above, the values of these variables are d1 = -5, d2 = 5, and n = 100
  • At some point in the body of the function, the output argument (y) is assigned a value (or an array of values in this case). That determines what is returned by the function. Unlike other languages, no return statement is required.

 

There is one more point that is worth reiterating – the main reason for writing a function is so that this lengthy block of code does not have to be repeated every time it is used. Instead, only a single line is needed to call the function each time.

Lecture Videos 5.2-5.4 – Writing and Calling Functions

 

Checkpoint – user-defined function definition

 

Checkpoint – user-defined function call

 

Find an error? Have a suggestion for improvement? Please submit this survey.

License

MATLAB Programming for Engineering Applications Copyright © by James Toney and jayakumar5. All Rights Reserved.

Share This Book