KEMBAR78
Working with functions in matlab | PPTX
Working with Functions in
MATLAB
Function prototype
• Functions in MATLAB starts with a keyword ‘function’.
• Second part of function syntax is outputs(return value),
unlike other programming lang, in MATLAB , a func can
return more than one value.
• Assignment operator.
• Then a valid function name(user defined, it should not
match any built-in function name or keyword)
• Lastly the arguments (inputs) to the function, this is
optional part.
• For example: function p=prime_num(a,b)
Simple function to find greatest of
three values.
Keyword output function name inputs
function o1 = greatest (a,b,c)
if((a>b)&&(a>c))
o1=a;
else if((b>a)&&(b>c))
o1=b;
else
o1=c;
end
end
Output:
greatest(10,4,26)
O1=26
Function with arguments and two or
more return values
outputs arguments
function [o1,o2]= myfunction(a,b,c)
o2=a*b*c
o1=a+b+c
End
output:
calling function, by typing function name in command window.
myfunction(5,6,2)
o2 = 60
o1 = 13
ans = 13
Note: To see all the outputs, we must not put semicolon after the output statement.
Note: if we put semicolon, we will see only the first output.
Function with arguments and no
return value
%function to find sum of even values between given range m-n
function [ ] = sum_of_even( m,n )
var temp;
temp=0;
for i=m:n
if(rem(i,2)==0)
temp=temp+i;
end
end
disp(temp); %display statement to show the result.
end
output:
sum_of_evem(1,10)30 ans
Note: since function is not returning any value so wecannot store the result into other
variable like we did in other function program.
Function within function
function [ sum ] = func_within_func( )
sum=0;
disp('we are in function "func_within_func"');
disp('now we will jump to other function, which is "sum_of_odd"');
calling function(sum_of_odd)
sum=sum_of_odd(1,10);
%Here we are finding sum of odd values between 1 and 10 %
end
Output: we are in function "func_within_func"
now we will jump to other function, which is "sum_of_odd"
ans =
25
Storing value returned by function into
other variable.
function big=greater(a,b)
if(a>b)
big=a;
else
big=b;
end
End
Output:
n=greater(45,67)
n=5
>>n*n %reused the function result without calling function
ans =25
Points to Remember
• Function name and file name must be same.
• Unlike other programming languages, Function in MATLAB can return more than
one value.
• Syntax: function outputs=function_name(inputs)
• There is no starting and ending curly braceS to enclose the body of function,
instead, there is an end statement which signifies end of function.
• For example function o1=greatest(a,b,c)
• Other example: function [o1,o2] = 2greatest(a,b,c,d,e)
• To run a function we type a function name with valid arguments in command
window area.
• If function returns two or more ouputs,to see all the outputs, we must not put
semicolon after the output statement. Note: if we put semicolon, we will see only
the first output.
• We can store the value returned by function into other variable using assignment
statement like: big_value=greatest(5,4,7,9,13) then print variable big_value.
Advantage is we can use the function result again and again by using variable
name instead of function call.
Working with functions in matlab

Working with functions in matlab

  • 1.
  • 2.
    Function prototype • Functionsin MATLAB starts with a keyword ‘function’. • Second part of function syntax is outputs(return value), unlike other programming lang, in MATLAB , a func can return more than one value. • Assignment operator. • Then a valid function name(user defined, it should not match any built-in function name or keyword) • Lastly the arguments (inputs) to the function, this is optional part. • For example: function p=prime_num(a,b)
  • 3.
    Simple function tofind greatest of three values. Keyword output function name inputs function o1 = greatest (a,b,c) if((a>b)&&(a>c)) o1=a; else if((b>a)&&(b>c)) o1=b; else o1=c; end end Output: greatest(10,4,26) O1=26
  • 4.
    Function with argumentsand two or more return values outputs arguments function [o1,o2]= myfunction(a,b,c) o2=a*b*c o1=a+b+c End output: calling function, by typing function name in command window. myfunction(5,6,2) o2 = 60 o1 = 13 ans = 13 Note: To see all the outputs, we must not put semicolon after the output statement. Note: if we put semicolon, we will see only the first output.
  • 5.
    Function with argumentsand no return value %function to find sum of even values between given range m-n function [ ] = sum_of_even( m,n ) var temp; temp=0; for i=m:n if(rem(i,2)==0) temp=temp+i; end end disp(temp); %display statement to show the result. end output: sum_of_evem(1,10)30 ans Note: since function is not returning any value so wecannot store the result into other variable like we did in other function program.
  • 6.
    Function within function function[ sum ] = func_within_func( ) sum=0; disp('we are in function "func_within_func"'); disp('now we will jump to other function, which is "sum_of_odd"'); calling function(sum_of_odd) sum=sum_of_odd(1,10); %Here we are finding sum of odd values between 1 and 10 % end Output: we are in function "func_within_func" now we will jump to other function, which is "sum_of_odd" ans = 25
  • 7.
    Storing value returnedby function into other variable. function big=greater(a,b) if(a>b) big=a; else big=b; end End Output: n=greater(45,67) n=5 >>n*n %reused the function result without calling function ans =25
  • 8.
    Points to Remember •Function name and file name must be same. • Unlike other programming languages, Function in MATLAB can return more than one value. • Syntax: function outputs=function_name(inputs) • There is no starting and ending curly braceS to enclose the body of function, instead, there is an end statement which signifies end of function. • For example function o1=greatest(a,b,c) • Other example: function [o1,o2] = 2greatest(a,b,c,d,e) • To run a function we type a function name with valid arguments in command window area. • If function returns two or more ouputs,to see all the outputs, we must not put semicolon after the output statement. Note: if we put semicolon, we will see only the first output. • We can store the value returned by function into other variable using assignment statement like: big_value=greatest(5,4,7,9,13) then print variable big_value. Advantage is we can use the function result again and again by using variable name instead of function call.