Reputation: 165
I am really new to programming and while learning Dart function I come to a program which has but I couldn't understand what makes them to use different function one with return value and another without. Both will called during main program and both subprograms complete the task without returning any value(at least I didn't see anything like that), Can anyone point out why one have to use return type function.
import (....)
void main() => runApp(XylophoneApp());
class XylophoneApp extends StatelessWidget {
//Void Fuction
void player(int songNumber) {
final player = AudioCache();
player.play('note$songNumber.wav');
}
//Function with return
Expanded buildKey({colors, songNumber}) {
return Expanded(
child: FlatButton(
color: colors,
onPressed: () {
player(songNumber);
},
),
);
}
//Call in main function
buildKey(colors: Colors.red, songNumber: 1),
Upvotes: 2
Views: 983
Reputation: 6524
The XylophoneApp.player
function doesn't return any value since it's a void
function (Instead it will just play a sound), where the XylophoneApp.buildKey
function will return a value of type Expanded
, then it's up to you whether you want to use the returned value or not.
Upvotes: 1
Reputation: 460
Functions in general - not only in dart - consist of 3 things:
It is like in math for example if you have (y = x + 1). This is a function that takes 'x' as an input and then do a process on that 'x' which is adding '1' to it and thing return the output to 'y'. In this simple example the input type is a number, and the output type is also a number. However, in programming this might not always be the case. For example, you can have a function that takes a word as an input then counts how many character in that word and return the total number of character in that word. But maybe you don't need to know that number. Maybe you just need to store it in a database and come back to it later. In that case, you function would not have to return the number or return anything. It should just save it to the database. What about if you already have that word in the database and you don't have its length there. Your function in that case does not have to take the word as an input it should go to the database by itself, take the word, count the characters, and save the result in the database. You see, fundamentally we still have an input, process, and an output, but the input & output are not being passed in or out to or from the function directly, the functions is taking care of getting its input and returning is output. So it really depends on what you want your function to do.
About the code you posted. Look at the play
function it takes songNumber
as an input and its type is int
, then it is doing some processing like creating a Player
object and using the songNumber
as part of the actual song file name. Then its output is the song being played from your speaker. The sound you hear is the actual output. You don't need any other output beside the sound. That is why in the code its returning void
. In the other hand the output of the second function buildKey
is a widget
of type Expanded
that can be used inside to code. buildKey
function also takes a color
& a songNumber
as an input. Inside buildKey
Expanded
widget is being created. Inside that widget FlatButton
is being created also. The first input (color
) is being used to specify the FlatButton
Color while the second input (songNumber
) is passed the FlatButton
onPressed
function. onPressed
function when are triggered by pressing the button it will call player
function giving it the songNumber
previously passed.
Upvotes: 1
Reputation: 2447
The method player
simply plays a song and returns back to the caller, i.e doesn't return any value to the caller method. Hence return type is Void
.
Whereas, the buildKey
method creates a Button which needs to be rendered in the UI.
buildKey
method is creating a widget that is a FlatButton, which when clicked, plays a song. This method returns this button which needs to be placed in the Widget hierarchy for flutter to render it on screen.
Upvotes: 1