how to make functions in mainscript

Here is My question and my code. I just want to know how can I make functions in main script
The main script should call:
 a function getData that accepts and returns two positive integer numbers.
 a function adjust that puts the larger in N and smaller in M.
 a function GCD that takes M and N then returns the gcd.
 a function printResult to print the gcd.
% Asks the user for input and takes only positive numbers into account
a = input('First number: ');
b = input('Second number: ');
a = abs(a);
b = abs(b);
n = max(a,b);
m = min(a,b);
% This is the real trick, normally performed a number of times
r = n - m*floor(n/m);
% Repeats the operation until updates of a equal updates of b
while r ~= 0
n = m;
m = r;
r = n - m*floor(n/m);
end
% Displays the result
GCD = m

 Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Jul. de 2017

0 votos

If you are using R2016b or later, you can just put the code for the function at the end of your script.

6 comentarios

Abdulrehman Khan
Abdulrehman Khan el 28 de Jul. de 2017
Can U explain how to do.
At the command line, give the command
edit
In the edit window that comes up, put in your code for the script, and then put in your code for your functions in the same file. Then click to Save the file. In the box that comes up, give a name that is different than the names of any of the functions in the file, and which follows the rules for MATLAB identifiers (starts with a letter, and letters and digits and underscore are permitted after that.)
I tried to use nested function but there are error in my code.
function main
Getdata3
just
GCD
print
function Getdata3
a = input('First number: ');
b = input('Second number: ');
a = abs(a);
b = abs(b);
just
function [n,m] = just
n = max(a,b);
m = min(a,b);
GCD;
function r = GCD
r = n - m*floor(n/m);
while r ~= 0
n = m;
m = r;
r = n - m*floor(n/m);
end
print
function print
GCD = m
end
end
end
end
end
Walter Roberson
Walter Roberson el 29 de Jul. de 2017
You nested too deeply. You have the just function defined inside Getdata3, so it cannot be called from your function main
Note that you already have your function GetData3 calling just -- which it is not supposed to do. Your GetData3 is responsible only for getting the numbers, and it has to return the numbers from the function. Also, when they name particular function names in the assignment they are going to give you lower marks if you use any other function name. For example they asked for adjust not for just
Abdulrehman Khan
Abdulrehman Khan el 30 de Jul. de 2017
Editada: Abdulrehman Khan el 30 de Jul. de 2017
Thanks for guiding me Sir but there is one more problem. I just need the code to print the answer as GCD = 9 but it prints the min value also. Here the screenshot
The code you posted contains
just
function [n,m] = just
so you call just() and it returns two values, but you do not assign the values to variables and you do not have a ";" on the end of the line. The default in MATLAB when you call a function that returns one or more outputs is to display the content of the first output.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 26 de Jul. de 2017

Comentada:

el 30 de Jul. de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by