Thursday, January 3, 2013

5.3. Parameters of a function


Quite often we will need to tell our function some special data to work with. For example, if we often write real numbers on screen, we can create an auxiliary function to help us to show them with a certain format. We could do it as follows:

  public static void displayRealNumber( float n )
  {
    Console.WriteLine( n.ToString("#.###") );
  }

And this function could be used from the body of our program, like this:

  displayRealNumber(2.3f);

(remember that the suffix "f" is used to tell the compiler to treat that number as a "float", because otherwise if would be considered as "double", and we would get a compilation error).

The full program could look like:

/*---------------------------*/
/*  C# Example #48:          */
/*  example48.cs             */
/*                           */
/*  Function                 */
/*    "displayRealNumber"    */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example48
{

  public static void displayRealNumber( float n )
  {
    Console.WriteLine( n.ToString("#.###") );
  }

  public static void Main()
  {
    float x;
   
    x= 5.1f;
    Console.WriteLine("The first real number is:");
    displayRealNumber(x);
    Console.WriteLine("and another:");
    displayRealNumber(2.3f);
  }
 
}

These additional data that the we indicate to the function is what we call its "parameters". As shown in the example, we must provide a name for each parameter (there may be several) and a data type. If more we need than one parameter, we indicate the type and name for each one, and separate them with commas:

public static void displaySum ( int x, int y ) {
  ...
}


Suggested exercises:
·       (5.3.1) Create a function to draw a square on the screen, with the width (and height) indicated as a parameter. Create a Main function to test it.
·       (5.3.2) Create a function to draw a rectangle on the screen, with the width and height indicated as parameters. Complete the test program with a Main function.
·       (5.3.3) Create a function to draw a hollow rectangle on the screen, with the width, height and border symbol indicated as parameters. Complete it with a Main to request that data to the user and draw the rectangle.

Tuesday, January 1, 2013

5.2. Basic concepts about functions


In C#, as in C and other languages ​​derived from it, all the "parts of a program" are functions, including the body of the program, which is always called "Main". In fact, the basic way of defining a function is indicating its name followed by empty parentheses, as we did with "Main", and preceded by certain keywords, like "public static void", whose meaning we will see soon. After that, and delimited with curly braces, we indicate the steps that this "piece of program" must take.

For example, we could create a function called "greet", to display several messages on the screen:

public static void Greet()
{
  Console.WriteLine("Welcome to the");
  Console.WriteLine("example program.");
  Console.WriteLine("I hope you are fine.");
}

Now from within the body of our program, we could "call" this function:

public static void Main()
{
  Greet();
  …
}

This way, we make our main program easier to read.

An important detail: both the usual function "Main" and the new "greet" would be part of our "class", so the complete source would look like this:

/*---------------------------*/
/*  C# Example #47:          */
/*  example47.cs             */
/*                           */
/*  Function "Greet"         */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example47
{

  public static void Greet()
  {
    Console.WriteLine("Welcome to the");
    Console.WriteLine("example program.");
    Console.WriteLine("I hope you are fine.");
  }

  public static void Main()
  {
    Greet();
    Console.WriteLine("Finished.");
  }
 
}

As a more detailed example, the main part of a small database manager like the ones we created in the previous chapter, could look like:

readDataFromFile();
do {
  displayMainMenu();
  getOptionFromUser();
  switch( option ) {
    case 1: searchData(); break;
    case 2: modifyData(); break;
    case 3: addData(); break;
    …


Suggested exercises:
·       (5.2.1) Create a function named "ClearScreen", to clear the screen by drawing 25 blank lines. It should not return any value. Create a "Main" to test it.
·       (5.2.2) Create a function named "Draw3x3rectangle", to display three rows, with three asterisks each. Create a "Main" to test it.
·       (5.2.3) Split into functions any of the databases created in the previous topic (such as the one to store people or files), so that the "Main" function is shorter and more readable. (Hint: the variables which are shared by several functions will have to be out of those functions, and preceded by the word "static").

Friday, December 28, 2012

5.1. Modular design of programs: Modular Decomposition


 So far we have been thinking which steps we should take to solve a certain problem, and creating programs translating each step intro commands. This is reasonable when the problems are simple, but may not be the best choice when it comes to something more complicated.

From now on we will start trying to break down problems into smaller pieces that are easier to solve. This should mean several advantages for us:

ñ  Each independent "piece of program" will be easier to program, as it will have a brief and specific function.
ñ  The "main program" will be easier to read, because it does not need to contain all the details on how to do everything.
ñ  We can distribute the workload so that each person is responsible for making a "piece of program", and we finally integrate the individual work of each person.

These "pieces" of the program are called "subroutines", "procedures" or "functions". The most often used name  in C language and its derivatives (as C#) is "functions".

Tuesday, October 30, 2012

Introduction to game programming

Before proceeding to chapter 5...

You can find here an introduction to game programming in C#, with text-mode examples using Console and graphic games using SDL (and Tao.SDL):