Practical 5: Function and Script M-files

Script M-file

Steps to Create

Steps for creating function M-files

1. The function name and its M-file name must be identical (except that the M-file must end with .m).

2. The first executable statement must be a function declaration of the form

function <outputVariables> = <functionName>(<inputVariables>)

Exercise

> One of the following function declarations has been taken from the MATLAB rem.m function M-file. Determine which one must be correct declaration:

function rem = remainder(x,y)

function out = rem(x,y)

out = function rem(x,y)

function out(x,y) = rem(x,y)

    Exercise: Writing Function Declarations

    Write down the <outputVariables>, <functionName> and <InputVariables> for the two function M-files from the previous exercises and verify they match the function declaration statement.

    Function M-file 1: p(x) = sin x + x3

    Function Name: _______________________

    Output Variables: _______________________

    Input Variables: _______________________

    Function M-file 2: Creating a Customised Random Number Generator

    Function Name: _______________________

    Output Variables: _______________________

    Input Variables: _______________________