Hide menu

TDDD34 Programming with Applications in Engineering

Subroutines (functions) in Matlab


Matlab sport only one kind of subroutines. They are called functions. This is a short overview of how to create functions. Functions can be executed in the same way as Matlab built-in commands.

General overview

A formal description of how to write a function in Matlab looks like this:

	function {return_value=}function_name{(parameter_list)}
	{comments}
	statements
	{end}

This calls for a short explanation of how to interpret it:

	The first word "function" is used to tell Matlab that the
	following is a definition of a function. Without it Matlab
	would think it was a normal expression.

	The curly brackets are not part of the function definition
	(they should not be written). They are used to tell you that
	the part within is optional, that is you may include it in the
	definition, but you do not have to.

        If the function produces some value as result (like "sin" for
        example) this is where you name the variable that will contain
        the result, followed by an equal sign.

        Then you must tell the name of the function. The name is later
        used to call the function and you should save the file with
        the same name.

        If you need to specify any values the function should use to
        work on you write a comma separated list of names within
        normal parentheses. Matlab will create variables with those
        names when the function is called, and assign a value to
        each. You must specify the values when calling the
        function. The variables is created to be valid only inside the
        function so they do not interfere with other variables you
        created before. The curly brackets still mean you may skip
        this entire part if you do not need it.

        Next follow comments. You may of course write comments also
        later on in the file, but those first lines of comments
        immediately after the function definition are special. If you
        later on write "help function_name" those comments will
        be shown as the response. Other later comments will not. If
        you write "lookfor function_name" only the first
        comment line is shown, so you should write it short and clear.

        Last follow all the statements that perform the actual
        work. This is just like a normal script. If you specified a
        return_value be sure to assign something to it.

        Finally you may end the function with the keyword "end".
        

Examples of a few different functions

An example of a function that do not require/accept any parameters (incoming data) nor produce any return value could be a welcome message or a simple script.

	function main
	% A script that do something

	disp('Hello world!');


	function welcome
	
	disp('Welcome to the world.');

A function that accept data but do not produce a return value:

	function print_message(message)

	disp(message);

A function only producing a value:

	function y=random
	% random - returns a random number.

	y=rand;

A function with both input parameters and a return value:

	function sum=add(a, b)

	sum = a + b;

Page responsible: Erik Nilsson
Last updated: 2012-08-07