Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Concept: A simple functions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's review this concept of functions
0:02 and functions that take inputs
0:03 and have return values.
0:05 We're going to encapsulate the idea of playing around.
0:09 This was basically the inside
0:11 of that loop that we built in the previous example.
0:13 And in order to do that
0:14 we're going to have to pass in
0:16 the names of the two players.
0:17 Here we just embed, or have, the roles right here.
0:21 This is kind of isolated so it's not exactly
0:22 the same as before
0:23 but we have these three roles
0:25 and then we're going to print out what they were
0:27 just so that folks can see what roles they can play.
0:30 And then there's some logic to find the winner
0:32 that's probably getting to role from the one
0:35 or both of the players.
0:37 And then, comparing whether or not
0:38 you know, if they rolled rock
0:40 did the other paper roll paper?
0:42 Whatever it was, once we figure that out
0:43 we set some variable we're not talking about explicitly.
0:46 player_1 wins, or player_2 wins.
0:49 And if player_1 wins, we're going to return player_1's
0:52 the string or the name of the player
0:54 otherwise player_2.
0:56 And if player_1 doesn't win
0:57 and two doesn't win
0:58 well, must be a tie.
1:00 So we're going to return none like we did before.
1:02 This is a bit of a simplification
1:03 but the things I want you to take away here
1:05 are we start all functions with the keyword def.
1:08 D-E-F.
1:09 And then we have a space.
1:10 And then we give it a name.
1:11 And this name can be any valid, variable, name, in Python.
1:15 For example, it can't start with a number
1:17 but the rules are pretty open, right?
1:19 After that.
1:20 And then it's going to take zero or more pieces of data.
1:23 In this case it takes two
1:24 the name of player_1, and the name of player_2.
1:27 And then it's going to return some data.
1:29 Now when we talk about the function up at the top line
1:32 we don't indicate returning any data.
1:34 Some languages do, they actually say
1:36 This function takes these two players and it returns
1:40 you know, the name string, or something like that.
1:42 In Python we don't do that.
1:43 We just either return it or we don't.
1:45 So down in the bottom
1:46 we're going to either return a string
1:48 or we're going to return none.
1:49 So we can check whoever calls this.
1:51 Did somebody win?
1:52 Here they are, otherwise, nobody won
1:54 that must be a tie.
1:55 This is how we create simple functions.
1:57 And they're incredibly useful as you saw
1:59 throughout that example.