MrSchmid.com

Functions in Python

Posted On: Mon, 2008-04-28 14:49 by mrschmid

Functions

 So far we’ve been using built in functions such as int() and raw_input(), but now it’s time for us to create our own functions. By creating functions we will save us time and money (well not actually any money, but saving time is nice). We can create small working functions and use them multiple times throughout our program. We can also reuse functions between programs.

 

Defining Functions

We define a function by using the following line-

Def functionname():

Anything indented under the def command will be part of the function.

The next line is documentation. Text in triple quotes will explain what the function does.

Complete the instructions program

 

Click Read More for the rest...

Receiving and returning values

Functions would be fairly useless if they didn’t take information in and return information out. Functions deal with data in a unique way known as encapsulation. Encapsulation basically means the variable names inside a function are fenced off from the rest of the program. The only variables that get in are ones passed to the function in parenthesis and the only variables that get out are ones that are returned.

 

Complete the Receive and Return program.

 

Positional vs. Default Parameters

Positional parameters are values defined in a function based on their position (or order). In the following example program you’ll see that the order that you give the function data defines what the data will be used as in the program.

A different way to set up our function is with keyword parameters. Keyword parameters are much clearer and define the parameters by setting them equal to the variable name used in the function.

Another way to deal with functions is to define a default value to use unless a different value is given. In the def line, set the variables to the default value.

 

Complete the Birthday Wishes Program

 

Global Variables and Constants

Remember, Encapsulation is a very important concept. As mentioned before, the values in side a function are kept separate from the rest of the program. That separation is known as a namespace or scope. Each function has its own name space. Even if two functions use the same variable names, they won’t interfere with each other because they are in separate namespaces. It’s really a bad idea to use the same variable names in different name spaces, but you could if you wanted to.

We can access a global value if we like by using the global command. Global followed by the variable name will bring that variable into the function’s namespace.
Complete the Global Reach Program

 

Tic-Tac-Toe

Steps to making the tic-tac-toe game

Write the pseudo code

Show the Instructions
determine who goes first
create an empty tic-tac-toe board
display the board
while nobody's won and it's not a tie
get the human's move
update the board with the move
calculate the computer's move
update the board with the move
display the board
switch turns
congratulate the winner or declare a tie

List of Functions to make

display_instruct()

Displays the game instructions.

def ask_yes_no(question)

Asks a yes or no question. Receives a question. Returns either a "y" or a "n".

def ask_number(question, low, high)

Asks for a number within a range. Receives a question, a low number, and a high number. Returns a number in the range from low to high.

pieces()

Determines who goes first. Returns the computer's piece and human's piece.

new_board()

Creates a new, empty game board. Returns a board.

display_board(board)

Displays the board on the screen. Receives a board.

legal_moves(board)

Creates a list of legal moves. Receives a board. Returns a list of legal moves.

winner(board)

Determines the game winner. Receives a board. Returns a piece, "TIE" or None.

human_move(board, human)

Gets the human's move from the player. Receives a board and the human's piece. Returns the human's move.

computer_move(board, computer, human)

Calculates the computer's move. Receives a board, the computer piece, and the human piece. Returns the computer's move.

next_turn(turn)

Switches turns based on the current turn. Receives a piece. Returns a piece.

congrat_winner(the_winner, computer, human)

Congratulates the winner or declares a tie. Receives the winning piece, the computer's piece, and the human's piece.

 

Most of these functions will make sense, but the toughest one will be the computer_move function. Let’s take a look at the logic behind this one. Here’s the strategy used:

1) The computer should take a move that wins

2) The computer should stop the human from winning

3) The computer should take the best square remaining. (the best squares will be the center, corners and then the rest)

To make this program, I’ll give you some functions and others you’ll have to create yourself.

display_instruct()

def ask_yes_no(question)

def ask_number(question, low, high)

pieces()

new_board()

display_board(board)

legal_moves(board)

winner(board)

human_move(board, human)

computer_move(board, computer, human)

next_turn(turn)

congrat_winner(the_winner, computer, human)

Functions in red are ones that you'll have to create
Functions in blue can be found here
Functions in orange can be found here
Functions in green are found here

The main function is here

Exercises

Modify HiLo to use a functions.

- Recreate our HiLo program so that it uses functions. Start by writing pseudocode, then decide on what functions you’ll need, then put it all together.

Practices: Instructions, Receive and Return, Birthday Wishes, Global Reach, TicTacToe
Exercises: HiLo functions

AttachmentSize
computer-move-nextturn-cong.gif14.8 KB
legalmoves-winner-humanmove.gif8.54 KB
main.gif5.82 KB
number-pieces-newboard-disp.gif10.72 KB
instructions.gif9.13 KB
receive-and-return.gif10.98 KB
birthday-wishes.gif7.91 KB
global-reach.gif9.82 KB
( categories: | )