Programming Primer

Modern programming has a set of terms that every wannabe programmer must know. Master these terms first. Once you get these terms down pat, it is easier to understand the jargon of each specific programming language.

Variable:

A variable is a named location in memory. We create variables to hold data that we need to access again at some later point. The compiler keeps a list of memory addresses to access these variables, but we use spoken-language-based names since most people would have a hard time memorizing hexadecimal codes.

As part of the declaration of a variable, we must define the data type and the name of the variable. Optionally, we can also define the accessibility level and the initial value (or values, in the case of arrays) of the variable.

Examples:

  • private int iVariable = 1;
  • double dVariable = 2.45643;
  • DIM fVariable AS float = 1.78
Function:

(1) A function is a named piece of executable code.

(2) A function is a distinct chunk of code that we can call multiple times within our main program.

One of the primary benefits of function calls is that we do not have to type and retype a useful section of code.

Some languages use different terms for functions, depending on whether or not calling the function results in a value being returned to the calling program code. For instance, Visual Basic refers to 'void' functions (functions that do not return a value) as subroutines. The C# and Java languages use the same concept as C/C++ (that is, that every callable piece of code is a function) but use the term 'method' instead.

As part of the function definition, we must define the return type, name, and any parameters. Optionally, we can also specify the accessibility level. Additionally, C++ requires that we define the scope of the function if we are creating a class.

Examples:

  • public int FirstFunction( void )
  • double SecondFunction( int iFirstParam, single sSecondParam )
  • Public Sub MyVBSubroutine ( ByVal fParam1 As Float )
  • Public Function MyVBFunction ( ByVal fParam1 As Float ) As String
Function call:

When programmers use the term 'function call' what they are really saying is "I am now executing the function named [func_name]," where [func_name] would be replaced by the name of the function. To the compiler, this means that control of the flow of execution is passed from the main (or 'calling') program code to the 'called' function code.

Function header:

A function header defines the various aspects of the function, such as the name, the scope, the return type, and any parameters necessary for proper execution of the function's code.

Method:

See Function

Subroutine:

See Function

Parameter:

A parameter is a special type of variable used within the declaration of a function. This variable defines the type and order of values that can be 'passed' to the function. A parameter only exists in the definition of the function; when we call the function, the value passed into the function is then considered to be an argument.

Argument:

An argument is the actual value being passed into a function call. The data type of the argument must match the data type specified in the function header, or must be of a type that is capable of being promoted to the correct type.

Promotion:

Promotion is an implicit conversion of a variable from one data type into another. Promotion only occurs if the original type is considered to be a sub-set of the type to be converted to. For instance, since the data type double can hold the entire scope of variables available to the data type single, a variable that was defined as a single can be promoted to a variable of type double. The opposite is NOT true, however.

Passing data:

When we use the phrase "passing a variable," what we are saying is that we are calling a function with some value as part of the function call, as defined by the parameters in the function definition. There are two ways to pass data to a function: by value or by reference. When we pass a variable by value, we are actually giving the function a copy of a variable. Any code that manipulates that variable is actually manipulating the copy. If we pass a variable by reference, we are giving the function the memory location of the variable. Any code that changes the data we passed in is actually changing the original variable. Be very aware of which style you are using, since the results of passing by value and passing by reference can (and probably will) be very different.

Accessibility level:

The term accessibility level defines what level of code can access a variable or function definition. The two most common levels are public and private. Variables and functions defined as public can be accessed by any other chunk of code that can execute the main program code. Private variables and functions can only be accessed by code within the same level as they are declared. This is an important part of a paradigm known as OOP, or Object Oriented Programming. In a nutshell, we can use accessibility levels to hide variables or functions from code that we do not want to give direct access to those variables or functions.

Compiler

A compiler is a specialized program that generates machine-readable (executable) code from program code.

Scope:

The term 'scope' refers to the valid lifetime of a variable within a program. There are (in general) only two levels of scope: global & local.

Global variables exist for the lifetime of the program itself. They are created outside of any method, and as such are accessible by every method. As a general rule, global variables should be avoided in anything but the most simple programs, with the exception of constant variables.

Local variables have a very limited existence. Depending on when a variable is created, it can be local to a class, method, or a control statement such as a for() or while() loop. When the class definition ends, when the method completes and returns control to the calling code, or when the control statement completes, any variables defined within them are said to be "out of scope." When a variable goes out of scope, most programming languages are set up to deallocate any memory (or other resources) that variable previously controlled.

Comments (0)

There are no comments for this entry.

Java Classes

If you are familiar with creating classes in C/C++, I have some good news and some bad news...

Good news: The concept of classes is the same in every OOP language, so you're at least one step ahead of new programmers.

Bad news: The syntax you used in C/C++ is different enough that you're probably going to spend a lot of time cursing at the Java compiler...

Similarities

Java is loosely based on C++, and implements OOP in more or less the same way. You can implement your code logic (say, from pseudo-code or UML diagrams) without much trouble, even if it was developed with specific C++ libraries in mind, as an equivalent library is probably available in the JRE (Java Runtime Environment).

Differences

While Java takes quite a bit of its base syntax from C and C++, it is a different language, with its own quirks and strengths. One of the things that confused me when I first started to learn Java was the keywords it uses for its class structure. For instance, instead of namespace, Java uses package. Instead of inherits, you get extends.

Take a gander at this sample I put together for you:

SampleClass.java

class SampleClass
{
   public SampleClass( void ) // constructor
   {
   /* place code here that needs to execute
   when an instance of our class is created */
   }
 
   // private internal variables and functions go here
   private int age;
   private char[] name = new char[50];
 
   /* public shared variables and external functions
   go here */
   public int GetAge( void )
   {
     return age;
   }
 
   public void SetAge( int value )
   {
     age = value;
   }
} 

The low-down...

This is a prototype for a java function/method:

return type function-name ( parameter type parameter-name )

I will try to add more later...

Comments (0)

There are no comments for this entry.

Visual Basic

Depending on who you ask, VisualBasic.NET (VB.NET) is either the next iteration of VisualBasic 6.0, or is a complete replacement. The conflict arises from certain complaints VB6 programmers have made; namely, that their legacy code is severely broken (won't compile, or executes incorrectly) by changes made to the core of the language. These changes were necessary to implement VB in the .NET environment. First released in 2001 as part of the original offering of Microsoft's new .NET Framework, subsequent versions have addressed some of those complaints...

Advantages?

While it pains me to do so, I will admit that VB.NET does have some redeeming qualities. First and foremost, VB.NET code is much easier to read than most of its contemporaries. For this reason alone, I would recommend it as a starting point for beginning programmers.

Drawbacks?

My biggest beef with VB.NET (and BASIC in general) is that BASIC is a relatively weakly-typed language. What that means is that variable types are allowed to be somewhat ambiguous, and data can be passed between two or more variables that are not necessarily compatible. The main problem with THAT issue is that it is easy to truncate our data; if we take a floating point value from one variable and copy it to a single-precision variable, we have lost every number past the tenths place. Big deal? YES!! 3.1 is most certainly not the same as 3.1417 when you are doing area calculations for circular objects...

VisualBasic.NET Keywords

Here is a list of all VB.NET keywords as of .NET 3.5:

AddHandler AddressOf Alias And
AndAlso Ansi As Assembly
Auto Boolean ByRef Byte
ByVal Call Case Catch
CBool CByte CChar CDate
CDec CDbl Char CInt
Class CLng CObj Const
CShort CSng CStr CType
Date Decimal Declare Default
Delegate Dim DirectCast Do
Double Each Else ElseIf
End Enum Erase Error
Event Exit False Finally
For Friend Function Get
GetType GoSub GoTo Handles
If Implements Imports In
Inherits Integer Interface Is
Let Lib Like Long
Loop Me Mod Module
MustInherit MustOverride MyBase MyClass
Namespace New Next Not
Nothing NotInheritable NotOverridable Object
On Option Optional Or
OrElse Overloads Overridable Overrides
ParamArray Preserve Private Property
Protected Public RaiseEvent ReadOnly
ReDim REM RemoveHandler Resume
Return Select Set Shadows
Shared Short Single Static
Step Stop String Structure
Sub SyncLock Then Throw
To True Try TypeOf
Unicode Until Variant When
While With WithEvents WriteOnly
Xor #Const #ExternalSource #If...Then...#Else
#Region - & &=
* *= / /=
\ \= ^ ^=
+ += = -=

Source: Microsoft Developers Network

Comments (0)

There are no comments for this entry.

Java: Multi-Platform Powerhouse

From Wikipedia:
Java is a programming language originally developed by Sun Microsystems and released in 1995 as a core component of Sun's Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to byte-code which can run on any Java virtual machine (JVM) regardless of computer architecture.

Java = Portable

Java is nearly ubiquitous with the World Wide Web. Because of the write once, run anywhere philosophy Sun has maintained since the beginning, Java is easily ported from one operating system to another with a minimum of fuss, making it an ideal development platform for ensuring maximum compatibility. While there has been a shift of some developers moving towards newer web-centric languages, such as Ruby on Rails (or RoR), Java remains the go-to language for a majority of businesses.

As far as the examples I can show you right now, Java varies very little from C and C++. Java's class syntax is quite a bit different, as well as the code used in its GUI (graphical user interface) implementation.

Programmer's Notes

I have put together a short list of notes about the Java language, with an emphasis on the various types available in the core language:

Java Types

Primitive
  1. Integer
  2. Floating point
  3. Character - char
  4. Boolean - bool
Arrays
  1. Must be initialized to be used.
  2. Can be created in two ways:
  3. Multi-dimensional array
  4. String
  5. StringBuffer - similar to StringBuilder in C# - allows strings to be modified
Logical operators
  1. Equal to ==
  2. Not equal to !=
  3. Greater than >
  4. Less than <
  5. Greater than or equal to >=
  6. Less than or equal to <=
Boolean logical operators
  1. AND &
  2. OR |
  3. XOR ^
  4. OrElse ||
  5. AndAlso &&
  6. NOT !
  7. AND assignment &=
  8. OR assignment |=
  9. XOR assignment ^=
  10. Equal to ==
  11. Not equal to !=
  12. Ternary if-then-else ?:
Constant variables
  1. Use keyword final.
  2. Must be initialized when declared.
  3. Convention dictates that constants be identified by using UPPERCASE notation:
    final type VARIABLE-NAME = value;
Inheritance
  1. Use keyword extends to derive from a parent (base) class.
  2. Use keyword super to access the parent (base) class from a derived (child) class.
  3. Abstract defines a class or method that cannot be instantiated.
  4. Final defines a class that cannot be derived (inherited) from.
  5. Use the keyword package to define a namespace.
  6. Use the keyword import to include an external package (project) into the current project.
Standard Library
  1. Contained within the java package.
Interfaces
  1. Can be thought of as an action plan for your classes; that is, it defines WHAT your class must do, but not HOW it does it.
  2. Any class that uses the interface must implement all aspects of the interface.
  3. Interface methods/functions must be public.
  4. Interfaces cannot have instance variables. Interfaces can have variables, but they are implicitly static and final.
  5. Interfaces can derive from other interfaces, using the extends keyword.
  6. Use keyword implements to add an interface to a class object.

Want a copy of my notes?

If for some odd reason you find this useful, you can download a .doc version here.

Comments (0)

There are no comments for this entry.

C# : Creating a Custom Class

If you are familiar with creating classes in C/C++, I have some good news and some bad news...

Good news: The concept of classes is the same in every OOP language, so you're at least one step ahead of new programmers.

Bad news: The syntax you used in C/C++ is different enough that you're probably going to spend a lot of time cursing at the C# compiler...

Similarities

Like I mentioned before, C♯ uses the same OOP model that you learned in C/C++. You still specify the various accessibility levels of your object's attributes and actions. You must still provide constructors for your class objects. Also, inheritance and polymorphism are implemented in much the same way as in C/C++.

Differences

The most glaring difference between a C# class and a C/C++ class is, obviously, the syntax. While the basic OOP concepts are the same in C# as in C/C++, the code to implement a class is quite different.

Take a gander at this sample I put together for you:

SampleClass.cs

class SampleClass
{
public SampleClass( void ) // constructor
{
/* place code here that needs to execute
when an instance of our class is created */
}
 
public ~SampleClass( void ) // destructor
{
}
 
// private internal variables and functions go here
private Int32 mySampleValue;
 
/* public shared variables and external functions
go here */
public Int32 GetSample()
{
return mySampleValue;
}
 
public void SetSample( Int32 value )
{
mySampleValue = value;
}
 
}

The low-down...

In C#, classes are implicitly public. While I believe the C# compiler will allow you to make a class private, it ultimately is an exercise in futility to do so, as you will never be able to create an instance of your class if it is private.

One thing I don't miss about C/C++ when I code in C#: there are no header files. C# classes are self-contained. Also, methods/functions in C# must be contained within a class, so no more global functions. C# does implement static functions, so it is possible to have a shared method/function.

Comments (0)

There are no comments for this entry.

Page: 12345Next

Invalid login. Please try again...

Forgot password? Sign up