MATLAB - Local Functions



MATLAB functions serve as reusable code blocks designed to execute particular tasks or computations. They play a crucial role in structuring and modularizing code within MATLAB, thereby enhancing its readability. Functions improve code clarity by packaging complex operations into a clearly named entity. This improved code organization permits you to invoke the same function numerous times with varying inputs, promoting code reusability. Additionally, functions contribute to code maintainability and readability, simplifying the process of debugging and updating your MATLAB programs.

Creating a function in MATLAB is straightforward. You can start by using the function keyword, followed by the output variables in square brackets, the function name, input variables in parentheses, and then the function body enclosed in curly braces. Here's a simple example −

function output = myFunction(input)
   % Function body
   output = input * 2;
end

In this example, myFunction is the name of the function, input is the input variable, and output is the output variable. You can replace the function name and variable names with your specific names and define the function's functionality within the body.

Now that we know what functions are in Matlab , let us take a look at Local Functions in Matlab.

Local functions in MATLAB are functions that are defined within the scope of another function or script.These functions are not visible or accessible from outside the parent function or script, making them a useful tool for creating modular and organized code. Let us explore the concept of local functions in MATLAB with detailed explanations and examples.

Why do we Need Local Functions?

Local functions comes with several advantages which are listed below −

  • You can break down a complex task into smaller, more manageable parts by using local functions. This promotes code organization and makes your code easier to read and maintain.
  • Local functions are encapsulated within the parent function's scope, so they can access and modify the parent function's variables, making them suitable for implementing helper functions.
  • Local functions are hidden from the global workspace, preventing accidental clashes with other function names in your MATLAB environment.

Defining Local Functions

To define a local function in MATLAB, you place the function definition within the body of another function or script. Here is the syntax for it.

Syntax for Local Function

function parentFunction(inputArgs)
   % Parent function code
   
   % Local function definition
   function localFunction(inputArgs)
      % Local function code
   end
   
   % More parent function code
end

Function parentFunction(inputArgs) − This line defines the parent function named parentFunction. It takes inputArgs as input arguments, which can be any number of input arguments that the parent function requires.

% Parent Function Code − This is a comment line, denoted by %, which is ignored by MATLAB. It is used to describe or document the purpose of the parent function. The actual code of the parent function goes here.

Function localFunction(inputArgs) − This line defines a local function named localFunction within the scope of the parent function. Local functions are nested inside the parent function and can access variables and input arguments of the parent function.

inputArgs − Local functions can have their own input arguments, distinct from the parent function's input arguments. These arguments allow you to pass data specific to the local function.

% Local Function Code − Similar to the comment line in the parent function, this comment line describes or documents the purpose of the local function. The actual code of the local function goes here.

% More Parent Function Code − After the local function definition, you can include additional code for the parent function. This code executes after the local function definition and can use both the input arguments and local functions within its scope.

Examples of Local Functions

Here are some examples of local functions −

Example 1: Simple Local Function

In this example, we have a parent function calculateAverage that calculates the average of two numbers using a local function sumNumbers.

function average = calculateAverage(a, b)
   % Local function to calculate the sum of two numbers
   function sumResult = sumNumbers(x, y)
      sumResult = x + y;
   end
   
   % Calculate the sum
   total = sumNumbers(a, b);
   
   % Calculate the average
   average = total / 2;
end
calculate average

The call to calculateAverage function is as follows −

>> result = calculateAverage(5, 7)

result =

     6

Example 2: Local Function with Multiple Inputs

In this example, we have a parent function calculateHypotenuse that calculates the hypotenuse of a right triangle using a local function squareAndSum.

function hypotenuse = calculateHypotenuse(a, b)
   % Local function to square and sum two numbers
   function sumResult = squareAndSum(x, y)
      sumResult = x^2 + y^2;
   end
   
   % Calculate the squared sum
   squaredSum = squareAndSum(a, b);
   
   % Calculate the hypotenuse
   hypotenuse = sqrt(squaredSum);
end

In matlab the function is saved as follows −

calculateHypotenuse

You can call the calculateHypotenuse function as follows −

>> result = calculateHypotenuse(3, 4)

result =

     5
Advertisements