variables and functions, classes and methods

Discuss Programming
Post Reply
User avatar
Calum
guru
guru
Posts: 1348
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

variables and functions, classes and methods

Post by Calum » Thu Dec 15, 2005 10:00 am

i am just starting to learn about coding and one thing i can't figure out is why classes and methods are any different (or any better) than variables and functions.

the course started off with variables and functions and quickly moved on to classes and methods, which seem to be much the same, but with a different sort of layout on the page. What's the point? i know there is one (most likely) but what?

i think one thing that's not helping me, is that in this course they're wshowing us the specification for things, but rarely the implementations, at least for the classes, and we get mostly to work with "user" code. essentially i feel like i am just blindly doing what i am told.

not to mention that i need to use windows to test my code (proprietary course code, with its own interpreter and GUI...) which means that most of my time is spent working on paper (which i prefer anyway)

anyway, so what's the deal? i like to be able to understand the fundamentals at least (not necessarily the nitty gritty of the details, but the underlying basics for sure) and i feel like they are starting us at a much more end-user type of level, so i am a little lost at sea.

ZiaTioN
administrator
administrator
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm
Contact:

Post by ZiaTioN » Thu Dec 15, 2005 12:00 pm

Variables are objects that are given memory space and cpu time upon execution. They generally hold values for later, or multiple use.

Functions are a set of instructions that never change and can be called from anywhere in your program. They are generally designed to process some input values and return an output value.

Classes and methods get you into OOP (Object Oriented Programming).

Classes are simply an organizational methods used by the programmer to represent an entity (a group of related objects). For example, a class that represents the entity of members at voids BBS could be represented by their attributes such as, NAME, ID, POST COUNT, JOIN DATE. If you are familiar with perl, most modules you will see are OOP modules that use classes. You can also use a class to represent or reference a group of related functions.

Example:

Code: Select all

package DBClass;
use strict;  # Strict.pm, used to keep the programmer in line
use DBI;   # Database perl module

sub new {
    my $pkg = shift;
    my $obj = { '_output' => undef, error => '' };
    bless $obj, $pkg;
    return $obj;
}

sub Connect {
   # Connection Code Here
}

sub AddTable {
   # Table Addition Code Here
}

Sub DeleteTable {
   # Table Deletion Code Here
}
Now the above example is your class (or in perl called package or module).

Now to call the class:

Code: Select all

#!/usr/bin/perl -w

use strict;
use DBClass;

# Create the reference to the package for later use in calling the functions/subroutines.
my $dbref = DBClass->new();

# Create db connection handle
my $con = $dbref->Connect();

# Add Table To Database
$dbref->AddTable($con, 'TABLE_NAME');

# Delete Table From Database
$dbref->DeleteTable($con, 'TABLE_NAME');
As you may be able to tell, after we create the reference to the package or class we can use that reference when calling subroutines or functions in the class. The last two calls to the class, I send the connection handle to the functions because that is the way the function needs to work when dealing with the database. If I were to write out the support code to those functions you could see why.

Classes are a great way to do "modular programming". It allows you to seperate your code into numerous files and be able to reach them in the scope of your main() function. They allow for greater future expansion to the application and allow for easier troubleshooting. If you can write a set of instructions once and call them many time, that is better than writing it many time throughout the program each time you want to use it.

The difference between classes and functions are functions have a specific set of tasks or instructions and are limited in what they can do as far as receive data, perform some instructions on it, and return it. A class can be a set of funtions, a set of variables, etc.

In this next example I will show you a completely different class setup and usage.

Code: Select all

package UserData;

sub new {
  my $pkg = shift;
  my $obj = {
      'NAME'                => q!ZiaTioN!, 
      'EMAIL'               => q!ziation@email.com!,
      'AGE'                  => q!97!, 
      'LOCATION'         => q!Mars!, 
      'GENDER'            => q!Female!, 
  };
  bless $obj, $pkg;
  return $obj;
}

1;

Now to call it:

Code: Select all

#!/usr/bin/perl -w

use strict;
use UserData;
my $ud = UserData->new();

print qq~
            My name is $ud->{'NAME'}.
            My email address is $ud->{'EMAIL'}.
            I am $ud->{'AGE'} years old.
            I live on $ud->{'LOCATION'}.
            I am of the $ud->{'GENDER'} gender.
           ~;
Now the class I wrote above is not a set of functions, but rather a set of objects or variables that can now be referenced from anywhere in the application regardless of scope. This is all done in perl with anonamys hashes and arrays and whatnot, but in other languages is done a little more cleanly (Java, C++, ADA, etc). Perl has to tweak it a bit to allow for OOP because perl is based on C and C does not have OOP capabilities.

It is all a lot to deal with but if you can get the hang of it your code will thank you. If anyone can better my explanation or find faults in my explanation please point it out. I am not the best teacher. I am a doer and not a teacher.

User avatar
Calum
guru
guru
Posts: 1348
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Thu Dec 15, 2005 4:10 pm

thank you! thanks for your time!

this is the sort of answer i am interested in. i am very much a beginner, so an answer like this, i do have to think about to start to understand, but this is good. In my course, they are teaching us an idealised code language and often i see the coursebook mentioning something like "many computer languages allow you to do xyz but we won't touch on that in this course and it is not allowed using the course code."

I do appreciate they are introducing me to the idea of coding one step at a time but i also find it easier to understand one thing if i can see it from another angle. your answer (and hopefulyl more answers like it from other users) really helps me to see real world examples of the idealised approach my course is teaching.

Post Reply