0

What is wrong with this?

It wont print the result using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static int Sum(int x, int y) { int result; result = x+y; return result; Console.WriteLine("The sum is ",result) } static void Main(string[] args) { Sum(8, 6); } } }

22nd Jan 2019, 6:50 AM
Farhan
2 Answers
+ 3
You returned the result prior and hence the WriteLine method does not execute. When you return from a function, the execution is complete and control is returned to the caller. Instead, remove the WriteLine statement in the function and print the returned value in main. static int Sum(int x, int y) { return x+y; } static void Main(string[] args) { Console.WriteLine("The sum is ", Sum(8,6)); }
22nd Jan 2019, 7:07 AM
Hatsy Rei
Hatsy Rei - avatar
0
Thanks, I have managed to do it. as I wanyed the method Sum to be able to print when called from main. Thought itll keep it cleaner. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Sum(int x, int y) { int result; result = x+y; Console.WriteLine("The sum is {0}",result); } static void Main(string[] args) { Sum(8, 6); } } }
22nd Jan 2019, 12:07 PM
Farhan