How can we use return function twice or more at a time | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can we use return function twice or more at a time

Why we can't use return twice and more in program for example: var a = parseInt(prompt("enter your 1st number")); var b = parseInt(prompt("enter your 2nd number")); function add (n1,n2){ var total= n1+n2; var sub= n1-n2; return total ; //here we can't use together return sub ; /* how we can use this if i want to execute both iin same Function? */ } document.write(add(a,b) ); https://code.sololearn.com/WQYi37noWgeQ/?ref=app https://code.sololearn.com/WQYi37noWgeQ/?ref=app

23rd Jan 2020, 11:02 AM
piyush raj
piyush raj - avatar
2 Answers
+ 3
piyush raj An function only return one value but you can use if and else statements to do so like this is an example add it with your code. var entry = prompt("Choose addition or subtraction"); if(entry=="addition"){ var a =prompt("Enter number 1") var b =prompt("Enter number 2") var c =(+a)+(+b); document.write("The answer is "+c); }else if(entry=="subtraction"){ var d =prompt("Enter number 1") var e =prompt("Enter number 2") var f =d-e document.write("The answer is "+f) }else{ alert("Error!"); }
23rd Jan 2020, 11:08 AM
DishaAhuja
DishaAhuja - avatar
+ 5
You can't, it is only possible once.
23rd Jan 2020, 11:06 AM
Aymane Boukrouh
Aymane Boukrouh - avatar