# Write a function called under_age that takes two positive integer scalar arguments: age that represents someone's age, and limit that represents an age limit. The function returns true if the person is younger than the age limit. If the second arg

1,044 views (last 30 days)
Abhishek singh on 22 Mar 2019
Answered: Jessica Trehan on 21 Sep 2020 at 19:29
function too_young = under_age(age,limit)
limit = 21
if age <= limit
too_young = true;
elseif age >= limit
too_young = false;
else
fprintf('invalid\n')
end

Rik on 21 Aug 2020
Have you read the documentation for nargin? One of the requirements of this program is to work for either 1 or 2 inputs, so you need a way to distinguish those.
Shagun Agarwal on 29 Aug 2020 at 11:04
function too_young=under_age(age,limit)
if nargin<2
limit=21;
if age<limit
too_young=true;
else
too_young=false;
end
end
if nargin==2 && age<limit
too_young=true;
else
too_young=false;
end
The above code is giving a wrong output at age=20, can someone please help me out in finding the mistake.
Rik on 29 Aug 2020 at 13:47
Did you step through the code line by line? Then you will notice that that it is possible for the second to last line to run when it shouldn't. It also helps to use the smart indent to see the nesting:
function too_young=under_age(age,limit)
if nargin<2
limit=21;
if age<limit
too_young=true;
else
too_young=false;
end
end
if nargin==2 && age<limit
too_young=true;
else
too_young=false;
end
It is also generally a bad idea to repeat code. If you remove the repetition you will probably clearly see where your mistake is.

John D'Errico on 23 Mar 2019
This is not an error. It is a failure of your code to work as it is supposed to work by your goals.
What did I say? What has EVERYONE said so far? You cannot set a default as you did.
Even though you pass in the number 18 as the limit, the first thing you do inside is reset that value to 21.
Instead, you need to think about how to test to see if limit was provided at all.

Abhishek singh on 24 Mar 2019
getting error for 22
Amal P on 5 Aug 2020
Why nargain < 2? Why not some other integer?
Rik on 5 Aug 2020
Have you read the documentation for the nargin function to see what it does? It returns the n[umber of] arg[uments you provide as] in[put]. So if it is smaller than 2, that means the limit was not provided as an input argument.

Saurabh Kumar on 28 Mar 2019
Write a function called under_age that takes two positive integer scalar arguments:
1. age that represents someone's age, and
2. limit that represents an age limit.
The function returns true if the person is younger than the age limit. If the second argument, limit, is not provided, it defaults to 21. You do not need to check that the inputs are positive integer scalars.
function x = under_age(age,limit)
if nargin < 2
limit = 21;
if age < limit
x = true;
else
x = false;
end
end
if nargin == 2
if age < limit
x = true;
else
x = false;
end
end

ARUN KUMAR GUPTA on 4 Oct 2019
function [too_young] = under_age(age,limit)
if nargin<2
limit=21;
end
if age<limit
too_young = true;
else too_young = false;
end
Rohan Singla on 18 Apr 2020
function [too_young] = under_age(age,limit)
if nargin<2
limit=21;
end
too_young =age<limit
end
Rohan Singla on 18 Apr 2020
why to have a long codes when same function can be performed by small ones

mayank ghugretkar on 7 Jun 2019
this can work too...
A bit compact approach
function too_young = under_age(age, limit)
if nargin < 2
limit = 21;
end
if age < limit
too_young=true;
else
too_young=false;
return
end
end

Ajith Thomas on 20 Jun 2019
function too_young=under_age(age,limit)
if nargin<2
limit=21;
end
if age<limit
too_young=true;
else age>=limit
too_young=false;
end
if nargin==2
age<limit
too_young=true;
elseif age==limit
too_young=false;
else age>limit
too_young=false;
end
why this code is not working?
its not working for 18 vs 18 and 20
Geoff Hayes on 21 Jun 2019
Ajith - look at your second if statement
if nargin==2
age<limit
too_young=true;
Do you really need the nargin check? And the age < limit does nothing. I would just remove this block (the if/elseif/else) and use the one that you have already coded at
if age<limit
too_young=true;
else age>=limit
too_young=false;
end
There should be no reason to use both.
Ajith Thomas on 29 Jun 2019
thank you goeff :)

Astr on 8 Sep 2019
function too_young = under_age(age, limit)
if nargin == 1
limit = 21;
end
too_young = gt(limit, age);

function too_young= under_age(age, limit)
if (nargin<2)
limit=21;
end
if (age<limit)
too_young=true
else
too_young=false
end
end

The problem is the less than or equal to operator. It should be only less than. The right resolution is:
function [a too_young] = under_age(age,limit)
a = true;
too_young = true;
if nargin < 2
limit = 21
end
if nargin == 2 && age < limit
a = true;
elseif nargin < 2 && age < limit
a = true;
else
a = false;
end
if nargout == 2 && age < limit
too_young = true;
else
too_young = false;
end
end
Kilaru Venkata Krishna on 2 May 2020
function too_young = under_age(age,limit);
if (nargin<2)
limit=21;
end
if age < limit
too_young=true;
else age >= limit
too_young=false;
end
Kilaru Venkata Krishna on 2 May 2020
its taking age >= limit as older
.......and ......age<limit as young

Siddharth Joshi on 23 Apr 2020
function too_young = under_age(age,limit)
if nargin<2
limit=21;
end
if age<limit
too_young=true;
else
too_young=false;
end
end
too_young = under_age(18,18)
too_young = under_age(20)
too_young =
logical
0
too_young =
logical
1

sudeep lamichhane on 27 Apr 2020
function too_young = under_age(age, limit)
if nargin<2
limit=21;
end
if age < limit
too_young= true;
else
too_young= false;
end

#### 1 Comment

Krashank Kulshrestha on 14 May 2020
correct but how u do it

Sai Hitesh Gorantla on 1 Feb 2020
Edited: Rik on 17 Jun 2020
function [too_young] = under_age(age,limit)
if nargin == 2
if age<limit
too_young = true;
else
too_young = false;
end
elseif nargin<2
if age<=21
too_young = true;
else
too_young = false;
end
end

mohammad elyoussef on 4 Apr 2020
function c = under_age(a,b)
if nargin < 2
b = 21;
end
if b > a
c = true;
else
c = false;
end

maha khan on 9 Apr 2020
function [too_young]= under_age(age,limit)
if (nargin < 2) || isempty(limit)
limit = 21;
end
if age>21
too_young=false;
elseif age < limit
too_young=true;
elseif age==age
too_young=false;
elseif age<=21
too_young=true;
elseif age < age
too_young=false;
elseif age<=21
too_young=true;
else
too_young=true;
end

#### 1 Comment

Walter Roberson on 9 Apr 2020
Suppose the user passes in a limit of 35, such as testing for eligibility to be President of the United States. And suppose the age passed in is 29. Then if age>21 would be if 29>21 and that would be true, so you would declare too_young=false but clearly the answer should be true: 29 < the specified limit.

Jack Derz on 3 May 2020
guys how to write their functions anyone can help me?

Jack Derz on 3 May 2020
do you have time to write its function for me because tomorrow i should submit it.
Walter Roberson on 3 May 2020
No, I never have time to write code for students to submit as their own code. However, I often have time to assist students when they ask questions.
Jack Derz on 3 May 2020
ok anyway thank you :)
i will manage to do it!

Mir Mahim on 7 May 2020
function a = under_age(age,limit)
if nargin < 2
limit = 21;
end
a = age < limit;
end

MD.Ismail Emon on 16 May 2020
function too_young = under_age(age,limit)
if nargin < 2
limit = 21;
end
if age < limit
too_young = true;
else
too_young = false;
end

Aasma Shaikh on 26 May 2020
function too_young= under_age (age, limit)
if nargin<2
limit=21;
if (age<limit)
too_young = true;
else
too_young = false;
end
elseif ((nargin==2) && (age<limit))
too_young = true;
else
too_young = false;
end
end
% Copy, paste and Run

jaya shankar veeramalla on 29 May 2020
function too_young = under_age(age,limit)
if (nargin < 2) || isempty(limit)
limit = 21;
end
if age < limit
too_young = true;
else age >= limit
too_young = false;
end

AYUSH MISHRA on 4 Jun 2020
function too_young =under_age(age,limit)
if nargin <2
limit=21;
end
if age<limit
too_young=true;
else
too_young=false;
end
SOLUTION ;
under_age(18,18)
ans =
logical
0
under_age(20)
ans =
logical
1

Keshav Patel on 8 Jun 2020
function too_young =under_age(age,limit)
if nargin<2
limit=21;
if age<limit
too_young=true;
else
too_young=false;
end
end
if nargin ==2
if age<limit
too_young=true;
else
too_young=false;
end
end

Kulasekaran N on 15 Jun 2020
function too_young = under_age(age,limit)
if nargin < 2
limit = 21;
end
if age < limit
too_young = true;
else
too_young = false;
end

Jack Derz on 15 Jun 2020
can you help buddy?
Walter Roberson on 16 Jun 2020
What assistance are you hoping for, Jack Derz ?

saurav Tiwari on 17 Jun 2020
Edited: Rik on 17 Jun 2020
function [a]=under_age(n,m)
a=n<m;
if a==1;
fprintf('true')
end
if nargin<2;
m=21;
end
end

saurav Tiwari on 17 Jun 2020
awesome
Rik on 17 Jun 2020
Why did you decide to put the part with nargin at the end?

AKASH SHELKE on 9 Aug 2020
Edited: AKASH SHELKE on 9 Aug 2020
function too_young = under_age(age,limit)
if nargin<2
limit = 21;
end
if age < limit
too_young = true;
else
too_young = false;
end

function too_young = under_age(age,limit)
if nargin < 2
limit = 21;
end
if age < limit
too_young = 1 == 1;
else
too_young = 1 ==2;
end
end

saurav Tiwari on 10 Aug 2020
Cool bro
Rik on 10 Aug 2020
That's a creative way to write true and false.
But why did you decide to post it? It doesn't seem to show a new strategy to solve this problem. Note that Answers is not a homework submission service.
Walter Roberson on 10 Aug 2020
too_young = 1 == 1;
You should
doc true
doc false

Omkar Gharat on 11 Aug 2020
function [too_young] = under_age(age,limit);
% age = input('Enter age of applicant : ')
% limit = input('Enter age limit of applicant : ')
if nargin < 2
limit = 21;
end
if age < limit
too_young = true;
else
too_young = false;
end
end
This is my code .And it is 100% correct

Khom Raj Thapa Magar on 6 Sep 2020 at 11:02
Make sure your indentation is correct while coding
function too_young = under_age(age,limit)
if nargin<2
limit = 21;
if age < limit
too_young = true;
else
too_young = false;
end
end
if nargin == 2
if age < limit
too_young = true;
else
too_young = false;
end
end
Calling functions
>> too_young = under_age(18,18)
>> too_young = under_age(20)
Output:
too_young =
logical
0
too_young =
logical
1

Jessica Trehan on 21 Sep 2020 at 19:29
function too_young=under_age(age,limit)
if nargin<2
limit=21;
if age<limit
too_young=true;
else
too_young=false;
end
end
if nargin==2
if age<limit
too_young=true;
else
too_young=false;
end
end
%THE PERFECT CODE

R2017b

### Community Treasure Hunt

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

Start Hunting!