0

It is assumed that the void function does not return a value, but in the following code, the sum is returned

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sololearn { class Program { static void num(int A = 5,int B = 4){ int Z = A+B; Console.WriteLine(Z); } static void Main(string[] args) { num(); } } }

2nd Jul 2022, 1:02 PM
RAMADAN : رمضان
RAMADAN : رمضان - avatar
4 Réponses
+ 2
The meaning of returning is that a value of the Methode get "returned" to the Main Methode(Methode from where you called it). You can see that a value is returned when you can use it in main. Example: void num(int A = 5, int B = 4) { Int Z = A+B; } static void Main(string[] args) { num(); //Error: Main have no variable Z Console.WriteLine(Z); } void int(int A = 5, int B = 4) { Int Z = A+B; return Z; } static void Main(string[] args) { num(); //No Error Console.WriteLine(Z); }
2nd Jul 2022, 3:24 PM
Felix Alcor
Felix Alcor - avatar
+ 2
No, nothing is returned. The calculation result is directly printed on screen, but it is not returned. You should have a line like this return Z; In num() body to have the method return something to its caller.
2nd Jul 2022, 2:12 PM
Ipang
+ 1
Sry I made a mistake in the Text before. You need to give the returned value of Z to a new variable. Also: ... int Z = num(); Console.WriteLine(Z); Sry
2nd Jul 2022, 3:36 PM
Felix Alcor
Felix Alcor - avatar
0
Ipang If you enter num(5,3); Then the sum of 5 + 3 = 8 will be returned
2nd Jul 2022, 3:03 PM
RAMADAN : رمضان
RAMADAN : رمضان - avatar