In this lesson we will discover funk.
This is just like our action here.
Also a generic delegate.
So the difference between action and funk and that one is really important, is that the funk returns
a value.
What does that mean.
Basically if we would now take a look at that action here we have that action.
It simply writes something to the console.
It does nothing else.
If we now try to get a value from that by saying, okay, we have a var result here equals to whatever
action returns.
You can see we get an error.
It says like cannot assign void to an implicitly typed variable, because our action that we have right
here is a delegate void.
And a void does not return anything.
So actions do not return a value.
So we cannot do it that way.
But very often you do not only want to run an action, say okay, do something here, like create some
something in the console, create a log.
Let's assume you would want to upload something.
In that case, you would want to have a function, right?
That function would start uploading and as soon as it's done it would return a status code saying okay,
I'm done with the upload, for example.
So you want to have a return value, same as you know, if you have a some method and you want to sum
up one and three, you don't want to got printed out into the console, you want to get four as the
return value.
Just wanted to give you a more real world example.
So let's just try to create a funk here.
Write down funk again.
You can see it's delegate, but this time it's of t result as our return value.
Right?
So funk.
Then we define on our actual outcome that we want to have.
So what kind of return type do we want to have from that.
In our case we want to have a string as our return value.
That's the last parameter.
And since we have no input parameter, the only parameter that we have right here will be the output.
This will become clear just in a minute.
So funk string, let's give it a name.
Let's say get uh well we can call it get name.
That will be fine.
Then we have empty parameters.
Here goes to we can simply return Yannick for example.
Now this simple function Getname returns a value.
Yannick.
And if I just remove that string here, it doesn't work anymore, right?
We have to explicitly define that.
So let's say our function Getname returns a string.
This is basically just like we would create a method that says uh string getname.
There we go.
And here we would also return Yannick basically the same from the structure.
We define the return type right.
The return value type string.
And then we return string.
We did that here too.
Now we can simply write down var result or var.
My name can do it that way.
My name is Getname.
Now we get the value.
And then we can simply Console.WriteLine.
My name which should write down Yannick into the console.
Right?
We call that function.
We get a return value from that function.
We save it in a local variable called my name.
We log it into the console.
You can see Yannick right here.
Great.
For sure you can get rid of that local variable and simply do it that way.
Getname no worries.
Still works fine right?
That's the first part.
Now let me remove it.
Let's just assume that we want to create a sum function that sums two numbers and then provides back
a value.
Right.
We would have.
Let's do this with integers.
That's always pretty simple.
That would mean that we have three parameters input a, input b.
Those numbers we want to like sum up or x and y however you want to name them.
And then we need to provide the information of what kind of return value do we want to provide.
So func.
This time as I said, we want to sum an integer.
Let's call it a right.
Then we have another integer for b and then finally we return.
You can see here how would return.
What would we like to give back to the user would be another integer.
And if we would add a comma now it would switch.
Then those three would be input and the next one would be the output.
I'll get to that again.
So let's call it sum equals to.
Let's write down x and y then goes to add parentheses here.
And then we return x plus y just like this.
So pay attention on the highlight now int the one highlighted here is x.
The second parameter int is y.
But what's the third one.
Well the third one is the output the return value the return type.
So basically this here or that here to be more specific is the third one int.
If we would now say we would want to return a string.
So we pass in an integer.
We pass in another integer and we want to expect the string.
We could say something like your result is.
Then we just concat a string here x plus y dot two string.
Right.
Or if you just want to have that number printed as a string, you can simply do it that way x plus y.
And then to string because we are expecting a string, if you hover above that you can see t one is
integer t.
Think of type in that case like a parameter parameter one is int, parameter two is also int and then
t result.
The return value is of type string, and that's always the last one.
No matter how many parameters you you add, the last one is always defining the output type.
The t result already.
As I said, just like with an action, you can add up to 16 arguments or parameters here.
So we could create 16 frequent parameters.
Probably you will never see that most of the time I think the highest one that I've seen was 7 or 8
or whatever, and that was already kind of crazy.
But I think you got the point right.
And that's it.
You can now simply call Console.WriteLine.
Right.
And we simply run some provide three and five.
And finally we get the return as a string.
You can see this here our sum returns a string right.
And then we can simply start.
And we will be able to see eight as a string inside of our console as a string.
As I said, cool.
So this is basically how you can create a func.
And the main difference between action and func is that action does not return anything because the
underlying structure is built on a void, but the function has a return value, and therefore we have
to specify the return type of that function as the last parameter while we declare that function.
Alrighty, and that's it for generic func in C sharp.