0
How is it void and can be called at the same time ?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void SayHi() { Console.WriteLine("Hello"); } static void Main(string[] args) { SayHi(); SayHi(); SayHi(); } } }
5 Respuestas
+ 4
Void means that the function does not return any values. It doesn't mean that it doesn't do anything or cannot be called.
+ 3
`void` indicates a method doesn't return anything, but still is invokable (callable).
+ 3
Whether or not a function should return values depends entirely on its purpose. Here you have a very simple function that merely produces some output. In cases like that you have no return values. But suppose you have a function that calculates the area of a square based on the length of its sides. If you just want to print the result a void function is fine. But what if you want to use the result of the calculation further, e.g. to calculate the volume of different bodies that have the same square base, but differ in the rest of their shape. In that case you need the function to return the result of the calculation so that you can perform further operations on it, e.g. using it as input for a number of functions calculating the volume of different bodies.
+ 2
Ipang Simon Sauter Thanks for answering
+ 1
What will be the difference if it returns any value if it is basically can give the output of "Hello" ? Ipang Simon Sauter