XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)

Topic: Topic.3.ModulesAndFunctions
Example: Ex_9.ByValByRef

Functions: Passing Arguments By Value, and By Reference



1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source code.

When the game starts, you'll see a screen that looks similar to this:

In addition to the standard "exit when you push the Back button" functionality, you can adjust the size of the right soccer ball by pushing the right thumbstick left or right, and you can move both soccer balls up or down by pushing the right thumbstick up or down..  Notice that this "game" is extremely simple - we have written it purely to demonstrate the difference between passing an argument to a function by reference, and passing an argument by value.

2. Examining The Program:

Let's examine the C# source code that produces the behavior we see on-screen


  1. Start from a blank starter project (1000.201, if you need it), and re-do the code from memory as much as possible.  On your first try, do what you can, and keep the above code open so that when you get stuck, you can quickly look up what you forgot (and that after you finish a line, so that you can compare your line to the 'correct' line).  On the next try, do the same thing, but try to use the finished code less.  Repeat this until you can type everything, without refering the tutorial's code.
  2. Adding Parameters To A Function
    For this exercise, you should use the same project that was explained in the above tutorial.

    1. What happens when you only remove the ref keyword from the call to the UpdateRadiusByRef function, in UpdateWorld (but keep the ref keyword in the function definition)?
    2. What happens when you only remove the ref keyword from the function definition (but keep the ref keyword in the call to the UpdateRadiusByRef function, in UpdateWorld )?
    3. What are some advantages of being forced, by the C# language, to include the ref keyword in both the definition, and at all locations where the function is called?
    4. Going back to the UpdateWorld function, and instead of creating new local variables, instead try to pass m_ValSoccer.Radius directly to the UpdateRadiusByVal method, and then try to pass m_RefSoccer.Radius directly to the UpdateRadiusByRef method

Project home page : The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
Michael Panitz
Business And Information Technology
Cascadia Community College

Microsoft Logo This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.