How to call the second function inside the first function under the same file

2 views (last 30 days)
% That is my code
% the expected result is :
Please enter the first user name: abby
Please enter the first user name: bob
The first user abby have 15 points
The second user bob have 15 points
% but there are bug on my code
function [p1,p2,Name1,Name2]= Playerpoint(point1,point2)
point1 = 10;
point2 = 11;
p1 = card1 + 5;
p2= card2 + 4;
Name1 = name1;
Name2 = name2;
fprintf("The first user %s has %d points\n",Name1,c1);
fprintf("The second user has %d points\n",Name2,c2);
end
function [name1,name2] = Playername(n1,n2)
name1 = n1;
name2 = n2;
end
% driver files
n1 = input('Please enter the first user name: ','s');
n2 = input('Please enter the second user name: ','s');
[name1,name2] = Playername(n1,n2);
card1 = 0;
card2 = 0;
[p1,p2,Name1,Name2]= Playerpoint(point1,pointt2);
% How to improve my code
% Thank you

Accepted Answer

Image Analyst
Image Analyst on 15 Apr 2022
You don't need the second function. And your arguments were all messed up.
Try the attached.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% That is my code
% The expected result is :
% Please enter the first user name: abby
% Please enter the first user name: bob
% The first user abby have 15 points
% The second user bob have 15 points
n1 = input('Please enter the first user name: ','s');
n2 = input('Please enter the second user name: ','s');
card1 = 10;
card2 = 11;
[p1,p2,Name1,Name2]= Playerpoint(card1, card2, n1, n2);
% The function definition
function [p1,p2,Name1,Name2]= Playerpoint(card1, card2, name1, name2)
p1 = card1 + 5;
p2 = card2 + 4;
Name1 = name1;
Name2 = name2;
fprintf("The first user, %s, has %d points.\n", Name1, p1);
fprintf("The second user, %s, has %d points.\n", Name2, p2);
end
% End of m-file.

More Answers (0)

Categories

Find more on Card games in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!