Python for the .NET Developer Transcripts
Chapter: The Python Language
Lecture: C# function overloading
0:00 In this next section, let's look at functions and how we can have them behave differently depending on the arguments we pass in.
0:09 So for example, here's one called SayHello and we can just call this with no arguments here's one where we can call SayHello("Zoe").
0:17 I'm going to say SayHello("Zoe", 5), and then "Zoe", 5 and then some arbitrary number of things, doesn't matter
0:22 how many there are. I guess I'll put four there. How do we do that? And here we can also have it behave differently
0:30 on the type of argument we're passing in integer versus a double. So this is part of C#'s static typing it's pretty easy for us to do this
0:39 and it's flexibility in defining functions. So let's look at it first in C# and then we'll go recreate this as much as we can in Python.
0:48 So let's just run it real quick and we can see what the output is. So when we call it void, it just says
0:54 hello there friends, and it had some default values and it has extra values it could pass over. These are the params, arguments that we'll see.
1:03 If we call it with name, the name's value is Zoe so instead of saying friend, it says Zoe and then we called it with names and 5 times
1:12 so it says hello there Zoe, and this value is 5 and so on. And then we had, I didn't update this 1, 2, 3, 4, so the extra values
1:20 that got passed here were 1, 2, 3, and 4. Then when we call it integer, we SayHello(5) it actually does that five times.
1:29 And if we call it with a double it repeats the number of times but it says hello there double times. This is a totally different function
1:36 if we pass a double than an integer which is also different than any of these. But these actually turn out to be the same one
1:42 with default values and params. Alright, so let's look really quickly at that. So down here for the one that gets used
1:48 most of the time, we have the name which is required, and we have a times which has a default value of 1 so it's optional, and then anything else
1:56 that gets passed gets captured in this param object array called extras. It'd be nice to just print out what that is
2:02 but it just says it's a list or an array of objects it doesn't tell you what the values are so I had to write this little function
2:08 in C# to print that out. We want to call it void, here's the void overload that just says hello friend. And you see it delegates back to this one
2:18 by passing friend. This one, if you pass in times, it says hello there some number of times, and if you pass in a double
2:25 same basic thing, but it says hello there double time. So we're able to select between this function
2:30 this function, this, and this, based on the arguments that we give it. And we got those different behaviors that you just saw there. Like that.
2:40 That works pretty well, and it's pretty common in C#. You want to see how to do it in Python? That's next.