Python Function and Matlab Function | applied electronics engineering


Python Function and Matlab Function

By Applied Electronics - Monday, February 13, 2017 No Comments
Here we show how to make a function in python and matlab programming language. They are actually very similar and creating function is easy.

In python one creates and save a function in a file with .py extension. In matlab functions are created and saved in .m extention.

Function in Python

In python a function is created using the keyword def. The def keyword is then followed by the name of the function. Let call it add to add two numbers. Then arguments for the function are written within parenthesis. Following the argument we have to put colon. Like so,

def add(x,y):

Then the next line starts with indentation and it is this indentation that tells the python interpreter that what follows next is the block of code of the function. So indentation is important in python. In many programming language like C or C++ the curly braces {} are used to define the block of code of a function. There is no such thing in python programming language.

After intendation in the next line we write what the function is supposed to do. Say we want to add x and y parameters passed into the function add and save it in variable called z. Then we return the variable z.This is done in the following manner:

def add(x,y):
    z = x+y
    return z

When you hit enter after return z there will be no indentation. Python IDE like spyder and others will know that the block of function is completed.

Finally we save the file as To use the function we import the adder module and use the add function defined in it. This is as follows.

>>> import adder
>>> adder.add(3,4)

Function in Matlab

Now we show how the same function is written in Matlab programming language.

A function in Matlab is created using the keyword function followed by the output name and then assignment = operator followed by the name of the function and then the aguments within parenthesis. Like so,

function z = add(x,y)

Then we write the code for the function. We want to add x and y and store it in z. There is no return statement but there is end for the function.

function z = add(x,y)
    z = x+y;

Now to use this function we have to save it as an .m file. Also we have to save it with a filename same as the function which is add.

To use the function we simply specify the function name and pass the required arguments. This is as follows.

>> add(3,4)

ans =



There are lots of difference between writing function in python and matlab. First to create function in python we use def keyword while in matlab we use the function keyword. Matlab function ends with end keyword while in python this is not required. In python the return keyword is required which is not required in matlab. A python function can be saved as module with any desired name. In matlab the function must be saved with a file with filename same as that of the function. This is not required in python. Indentation and return keyword marks the block of the function and end of the function. In matlab indentation is optional and block of statements of a function is marked using the end keyword.

No Comment to " Python Function and Matlab Function "