A function is a programming or a part that you build and store it in the matlab saved file location.Now this function can be called by other functions or can be run by the user to check for the output.
Thus a function has the following parts:
1.The reserved word 'function'
2.The function name
3.The input variables(optional)
4.The returned values(optional).
Hence the syntax is:
function [rvar1 rvar2...rvarn]=function_name (ivar1,ivar2...,ivarm)
%the programming
end
The input variables:
The input variables are the variables that you may require for computing and manipulating purposes.Here in the syntax they are assigned to as ivar1,ivar2,...,ivarm for m variables.
The returned variables:
Te returned variables are the variables that you want to get as the output after the execution of the function.Here in the syntax they are assigned to as rvar1 rvar 2 ...rvarn fo n returned variables.
**Calling a function
You can simply write the name of the function and type in the values for the input variables in correct order and of correct number of variables(if there are any).
1.You can use it in another function hence rite it in another function.
2.You can run the function in the command window by the same method.
Here is an example where the function takes a complex number as an input and gives the absolute value and the phase value or theta of the complex number as the output.
function [absolute,theta]=to_polar(n)
absolute=abs(n)
p=angle(n);
theta=(p/pi)*180
and when you type:
to_polar(9+5i)
in the command window and run it to see the answer.
PLEASE LEAVE A COMMENT :)
Thus a function has the following parts:
1.The reserved word 'function'
2.The function name
3.The input variables(optional)
4.The returned values(optional).
Hence the syntax is:
function [rvar1 rvar2...rvarn]=function_name (ivar1,ivar2...,ivarm)
%the programming
end
The input variables:
The input variables are the variables that you may require for computing and manipulating purposes.Here in the syntax they are assigned to as ivar1,ivar2,...,ivarm for m variables.
The returned variables:
Te returned variables are the variables that you want to get as the output after the execution of the function.Here in the syntax they are assigned to as rvar1 rvar 2 ...rvarn fo n returned variables.
**Calling a function
You can simply write the name of the function and type in the values for the input variables in correct order and of correct number of variables(if there are any).
1.You can use it in another function hence rite it in another function.
2.You can run the function in the command window by the same method.
Here is an example where the function takes a complex number as an input and gives the absolute value and the phase value or theta of the complex number as the output.
function [absolute,theta]=to_polar(n)
absolute=abs(n)
p=angle(n);
theta=(p/pi)*180
and when you type:
to_polar(9+5i)
in the command window and run it to see the answer.
PLEASE LEAVE A COMMENT :)
0 comments:
Post a Comment