Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: C# function overloading

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this next section, let's look at functions
0:03 and how we can have them behave differently
0:05 depending on the arguments we pass in.
0:08 So for example, here's one called SayHello
0:11 and we can just call this with no arguments
0:14 here's one where we can call SayHello("Zoe").
0:16 I'm going to say SayHello("Zoe", 5), and then "Zoe", 5 and then some
0:19 arbitrary number of things, doesn't matter
0:21 how many there are. I guess I'll put four there.
0:25 How do we do that?
0:27 And here we can also have it behave differently
0:29 on the type of argument we're passing in
0:31 integer versus a double.
0:33 So this is part of C#'s static typing
0:36 it's pretty easy for us to do this
0:38 and it's flexibility in defining functions.
0:41 So let's look at it first in C#
0:43 and then we'll go recreate this as much as we can in Python.
0:47 So let's just run it real quick
0:49 and we can see what the output is.
0:51 So when we call it void, it just says
0:53 hello there friends, and it had some default values
0:57 and it has extra values it could pass over.
1:00 These are the params, arguments that we'll see.
1:02 If we call it with name, the name's value is Zoe
1:05 so instead of saying friend, it says Zoe
1:09 and then we called it with names and 5 times
1:11 so it says hello there Zoe, and this value is 5
1:14 and so on. And then we had, I didn't update this
1:16 1, 2, 3, 4, so the extra values
1:19 that got passed here were 1, 2, 3, and 4.
1:23 Then when we call it integer, we SayHello(5)
1:25 it actually does that five times.
1:28 And if we call it with a double
1:29 it repeats the number of times
1:31 but it says hello there double times.
1:33 This is a totally different function
1:35 if we pass a double than an integer
1:37 which is also different than any of these.
1:39 But these actually turn out to be the same one
1:41 with default values and params.
1:43 Alright, so let's look really quickly at that.
1:45 So down here for the one that gets used
1:47 most of the time, we have the name
1:49 which is required, and we have a times
1:51 which has a default value of 1
1:53 so it's optional, and then anything else
1:55 that gets passed gets captured in this param
1:57 object array called extras.
2:00 It'd be nice to just print out what that is
2:01 but it just says it's a list or an array of objects
2:04 it doesn't tell you what the values are
2:06 so I had to write this little function
2:07 in C# to print that out.
2:10 We want to call it void, here's the void overload
2:13 that just says hello friend.
2:14 And you see it delegates back to this one
2:17 by passing friend.
2:18 This one, if you pass in times, it says hello there
2:21 some number of times, and if you pass in a double
2:24 same basic thing, but it says hello there double time.
2:27 So we're able to select between this function
2:29 this function, this, and this, based on the arguments
2:33 that we give it.
2:34 And we got those different behaviors
2:36 that you just saw there. Like that.
2:39 That works pretty well, and it's pretty common in C#.
2:43 You want to see how to do it in Python? That's next.