How is it void and can be called at the same time ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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(); } } }

30th May 2022, 11:32 AM
Mustafa Azzoz
Mustafa Azzoz - avatar
5 Answers
+ 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.
30th May 2022, 12:02 PM
Simon Sauter
Simon Sauter - avatar
+ 3
`void` indicates a method doesn't return anything, but still is invokable (callable).
30th May 2022, 12:04 PM
Ipang
+ 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.
30th May 2022, 1:47 PM
Simon Sauter
Simon Sauter - avatar
+ 2
Ipang Simon Sauter Thanks for answering
30th May 2022, 12:12 PM
Mustafa Azzoz
Mustafa Azzoz - avatar
+ 1
What will be the difference if it returns any value if it is basically can give the output of "Hello" ? Ipang Simon Sauter
30th May 2022, 12:10 PM
Mustafa Azzoz
Mustafa Azzoz - avatar