Jump to content

hakron

Members
  • Posts

    1
  • Joined

  • Last visited

Posts posted by hakron

  1. OK, this is all about strong-typing.

    Some languages allow you to define scalar variables (may contain any data type). For example, in VBScript you might write:

    Dim myVariable
    myVariable = 1
    myVariable = 1.5
    myVariable = "hello"

    Notice how it first contained an integer (whole number), then a decimal, then a text string - this is called weak typing.

    A language implementing strong-typing forces you to explicitly define what data type a variable is. For example:

    int number_one;
    number_one = 1;

    In this example, I have declared a variable called number_one which can only contain whole numbers (int). If I were to add a third line as follows, it would throw an error telling me I tried to assign the wrong type to my variable:

    int number_one;
    number_one = 1;
    number_one = "1";

    There are two kinds of methods you will encounter, those which return a value, and those which do not (voids).

    I might want to write a method to get a value, for example:

    int getSum(pIntOne, pIntTwo)
    {
        return (pIntOne + pIntTwo);
    }

    I pass in two parameters (which I start with a lower 'p'), I then return an integer. This was defined when I wrote int getSum. Had I tried to do the following, the program would error:

    int number_one = 1;
    int number_two = 2;
    int sum = getSum(number_one, number_two);
    
    string getSum(pIntOne, pIntTwo)
    {
        return (pIntOne + pIntTwo);
    }

    The other kind of method, is one which does not return a value. This is called a void method. It is simply called to perform a task. For example, one for printing error messages to the screen:

    void doMessage(pStrMessage, pIntImportance)
    {
        switch (pIntImportance)
        {
            case 1:
            {
                print ("Error: " + pStrMessage);
                break;
            }
            case 2:
            {
                print ("Warn: " + pStrMessage);
                break;
            }
            case 3:
            {
                print ("Info: " + pStrMessage);
                break;
            }
            default:
            {
                print ("Debug: " + pStrMessage);
                break;
            }
        }
    }

    I'm bored now, but I hope you find that useful. To learn how to convert between data types, i.e. how to turn your decimal into an integer, read this page about casting. I'm not a c programmer, but it seems odd to me you defined "int main()", why should main return an integer? How about "void main()"?

    The reason you declare main as returning an int is that all programs return status codes to the operating system - especially in Unix/Linux. By convention, returning zero means everything went ok.

    I generally recommend avoiding any calls to scanf() directly. Instead, read the string in and perform sscanf() on the string. scanf() skips whitespace in ways that can be confusing.

    -Hak Ron

×
×
  • Create New...