Topic : Programming in C/C++
Author : Alexander Allain
Page : << Previous 3  Next >>
Go to page :


   
return 0;
}


This was another simple example, but it is longer than the above FOR loop. The easiest way to think of the loop is to think the code executes, when it reaches the brace at the end it goes all the way back up to while, which checks the boolean expression.

DO WHILE - DO WHILE loops are useful for only things that want to loop at least once. The structure is DO {THIS} WHILE (TRUE);

Example:
#include <iostream.h>
int main()
{
  int x;
  x=0;
  do
  {
    cout<<"Hello world!";
  }while(x!=0);        //Loop while x is not zero, ut first execute the
              //code in the section. (It outputs "Hello..." once
  return 0;
}


Keep in mind that you must include a trailing semi-colon after while in the above example. Notice that this loop will also execute once, because it automatically executes before checking the truth statement.


Lesson 4: Functions



Now that you should have learned about variables, loops, and if statements it is time to learn about functions. You should have an idea of their uses. Cout is an example of a function. In general, functions perform a number of pre-defined commands to accomplish something productive.

Functions that a programmer writes will generally require a prototype. Just like an blueprint, the prototype tells the compiler what the function will return, what the function will be called, as well as what arguments the function can be passed. When I say that the function returns a value, I mean that the function can be used in the same manner as a variable would be. For example, a variable can be set equal to a function that returns a value between zero and four.

For example:
int a;
a=random(5); //random is sometimes defined by the compiler
//Yes, it returns between 0 and the argument minus 1


Do not think that 'a' will change at random, it will be set to the value returned when the function
is called, but it will not change again.

The general format for a prototype is simple:

return-type function_name(arg_type arg);

There can be more than one argument passed to a function, and it does not have to return a value. Lets look at a function prototype:

int mult(int x, int y);

This prototype specifies that the function mult will accept two arguments, both integers, and that it will return an integer. Do not forget the trailing semi-colon. Without it, the compiler will probably think that you are trying to write the actual definition of the function.

When the programmer actually defines the function, it will begin with the prototype, minus the semi-colon. Then there should always be a bracket (remember, functions require brackets around them) followed by code, just as you would write it for the main function. Finally, end it all with a cherry and a bracket. Okay, maybe not a cherry.

Lets look at an example program:

#include <iostream.h>

int mult(int x, int y);

int main()
{
  int x, y;
  cout<<"Please input two numbers to be multiplied: ";
  cin>>x>>y;
  cout<<"The product of your two numbers is "<<mult(x, y);
  return 0;
}
int mult(int x, int y)
{
  return x*y;
}


This program begins with the only necessary include file. It is followed by the prototye of the function. Notice that it has the final semi-colon! The main function is an integer, which you should always have, to conform to the standard. You should not have trouble understanding the input and output functions. It is fine to use cin to input to variables as the program does.

Notice how cout actually outputs what appears to be the mult function. What is really happening is that mult acts as a variable. Because it returns a value it is possible for the cout function to output the return value.

The mult function is actually defined below main. Due to its prototype being above main, the compiler still recognizes it as being defined, and so the compiler will not give an error about mult being undefined, although the definition is below where it is used.

Return is the keyword used to force the function to return a value. Note that it is possible to have a function that returns no value. In that case, the prototype would have a return type of void.

The most important functional (Pun semi-intended) question is why. Functions have many uses. For example, a programmer may have a block of code that he has repeated forty times throughout the program. A function to execute that code would save a great deal of space, and it would also make the program more readable.

Another reason for functions is to break down a complex program into something manageable. For example, take a menu program that runs complex code when a menu choice is selected. The program would probably best be served by making functions for each of the actual menu choices, and then breaking down the complex tasks into smaller, more manageable takes, which could be in their own functions. In this way, a program can be designed that makes sense when read.


Lesson 5: switch case


Switch case statements are a substitute for long if statements. The basic format for using switch case is outlined below.

Switch (expression or variable)  
{  
case variable equals this:   
  do this;  
  break; 
  case variable equals this:   
  do this;  
  break;  
  case variable equals this:  
  do this;  
  break; 
  ...  
  default:  
  do this 
}


The expression or variable has a value. The case says that if it has the value of whatever is after that cases then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from testing the next case statement also.

Switch case serves as a simple way to write long if statements. Often it can be used to process input from a user.

Below is a sample program, in which not all of the proper functions are actually declared, but which shows how one would use switch case in a program.

#include <iostream.h> 
#include <conio.h> 
int main()
{   
  int input;   
  cout<<"1. Play game"; 
  cout<<"2. Load game"; 
  cout<<"3. Play multiplayer"; 
  cout<<"4. Exit"; 
  cin>>input; 
  switch (input) 
  {   
   case 1: playgame();
    break; 
   case 2:
   loadgame(); 
        break; 
   case 3:       //Note use of : not ; 
        playmultiplayer();
   break; 
   case 4:
        return 0;  
  default:    
        cout<<"Error, bad input, quitting"; 
  }
  return 0;
}

This program will not compile yet, but it serves as a model (albeit simple) for processing input.

If you do not understand this then try mentally putting in if statements for the case statements. Note that using return in the middle of main will automatically end the program. Default simply skips out of the switch case construction and allows the program to terminate naturally. If you do not like that, then you can make a loop around the whole thing to have it wait for valid input. I know that some functions were not prototyped. You could easily make a few small functions if you wish to test the code.


Lesson 6: An introduction to pointers


Pointers can be confusing, and at times, you may wonder why you would ever want to use them. The truth is, they can make some things much easier. For example, using pointers is one way to have a function modify a variable passed to it; it is also possible to use pointers to dynamically allocate memory allows certain programming techniques, such as linked lists.
Pointers are what they sound like...pointers. They point to locations in memory. Picture a big jar that holds the location of another jar. In the other jar holds a piece of paper with the number 12 written on it. The jar with the 12 is an integer, and the jar with the memory address of the 12 is a pointer

Pointer syntax can also be confusing, because pointers can both give the memory location and give the actual value stored in that same location. When a pointer is declared, the syntax is this: variable_type *name; Notice the *. This is the key to declaring a pointer, if you use it before the variable name, it will declare the variable to be a pointer.

As I have said, there are two ways to use the pointer to access information about the memory address it points to.

Page : << Previous 3  Next >>