Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Concept: A simple functions
0:00 Let's review this concept of functions and functions that take inputs and have return values. We're going to encapsulate the idea of playing around.
0:10 This was basically the inside of that loop that we built in the previous example. And in order to do that we're going to have to pass in
0:17 the names of the two players. Here we just embed, or have, the roles right here. This is kind of isolated so it's not exactly the same as before
0:24 but we have these three roles and then we're going to print out what they were just so that folks can see what roles they can play.
0:31 And then there's some logic to find the winner that's probably getting to role from the one or both of the players. And then, comparing whether or not
0:39 you know, if they rolled rock did the other paper roll paper? Whatever it was, once we figure that out
0:44 we set some variable we're not talking about explicitly. player_1 wins, or player_2 wins. And if player_1 wins, we're going to return player_1's
0:53 the string or the name of the player otherwise player_2. And if player_1 doesn't win and two doesn't win well, must be a tie.
1:01 So we're going to return none like we did before. This is a bit of a simplification but the things I want you to take away here
1:06 are we start all functions with the keyword def. D-E-F. And then we have a space. And then we give it a name.
1:12 And this name can be any valid, variable, name, in Python. For example, it can't start with a number but the rules are pretty open, right? After that.
1:21 And then it's going to take zero or more pieces of data. In this case it takes two the name of player_1, and the name of player_2.
1:28 And then it's going to return some data. Now when we talk about the function up at the top line we don't indicate returning any data.
1:35 Some languages do, they actually say This function takes these two players and it returns you know, the name string, or something like that.
1:43 In Python we don't do that. We just either return it or we don't. So down in the bottom we're going to either return a string
1:49 or we're going to return none. So we can check whoever calls this. Did somebody win? Here they are, otherwise, nobody won that must be a tie.
1:56 This is how we create simple functions. And they're incredibly useful as you saw throughout that example.