Write a MATLAB function that returns the square of a number.
Show older comments
funciton square = sqr (x)
square = x * x;
end
Is it correct ?
Answers (1)
> Is it correct ?
funciton square = sqr (x)
square = x * x;
end
Feedback
- "function" is spelled incorrectly which I'm sure is a typo.
- What is x? Is it a scalar (single value) or a vector/matrix/array? If it's not a scalar, then no, this is not correct.
- While x*x is effectively squaring a scalar, why not square the value using an exponent?
- This is minor, but stylistically, there's no need for the space between sqr and (x).
- Instead of asking if it's correct, test it!! Provide a wide variety of inputs including large numbers, small numbers, negative numbers, 0, etc.
1 Comment
Deniz Efe
on 14 Mar 2021
Categories
Find more on Mathematics and Optimization in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!