
To have a declared "return" of a functions you need to place a caret (^) symbol in front of the variable whose value will be returned . It's important that the line of code having the return to be the last because when a ^ is encountered, the method ends execution and returns the value of the variable following the ^ return symbol.
TIP: Sometimes you can use the caret in the last statement. Something like:
^x := y + 29. "return the value of x"
In Smalltalk, comments are enclosed in double quotations. We sugest that you use many comments inside your programs when doing it. This will be the best documentation. If you let to do the documentation after the tests you never you do it...
If you use a variable like a parameter in the definition of the name of a method you don't need to declare it inside the method:
aMethod: aParameter
^aParameter * 45
Smalltalk has resources to programming comparisons. It's a little different from Java or C. Look the IF THEN equivalent:
| x y |
x := 1.
y := 2.
(x > y)
ifTrue: [
"do something".
]
ifFalse: [
"do something".
].
You can write many lines of code between the square brackets.
For comparisons we use:
> "greater than"
< "less than"
= "equal to in value"
~= "not equal in value"
>= "greater than or equal to"
<= "less than or equal to"
The "==" comparison, checks to see if the two objects are the same object, rather than equal in value.
And the signals for AND and OR are:
(a > 0) & (b < 0) "Returns true if a is positive
and b is negative,Otherwise false."
(a > 0) | (b < 0) "Returns true if either a is
positive or b is negative."
You can use the "return symbol" inside a comparison:
(m < n)
ifTrue: [^m]
ifFalse:[^n]
This code returns the value of n if n is greater than m , otherwise it returns the value of m .
We said that to declare an string you need to put it inside single quotes. You can declare a character using the symbol "$" to preced it:
someChar := $K.
A number can not begin with decimal point or end with decimal point:
someValue := .57 "ERROR!!!!!!!"
The arithmetic operation signals in Smalltalk are:
+ "addition" - "subtraction" * "multiplication" / "division" // "division with truncation" \\ "remainder from division"Some examples of arithmetic operations are follows:
5 + 4. "returns 9" 9 // 4. "returns 2" 9 \\ 4. "returns 1" | x y | x := 5. y := 7. x * y "returns 35"
In Smalltalk, nil means "nothing". All variables initially are point to nil .
The usual LOOP FOR is declared in Smalltalk like this:
1 to: last do: [ :i | i+1. "do something" ]
This can be a little confuse for a Java or C programer. Lets go to see an example, comparing:
The LOOP FOR that you know would be:
for(i=1; i<=4; i++){
x = i*5;
}
In Smalltalk you will have:
1 to:4 do: [
: i | i+1.
x := i*5.
]
And lets go to see an example of the LOOP WHILE in Smalltalk:
x := 5.
y := 0.
[y <= x] whileTrue: [
"do something".
y := y + 1
].
We will have the 6 roundtrips.
I accept that, for old programers - like me, from the "Cobol times" - can be not very easy to use, in the beginning...
PREVIOUS LESSON NEXT LESSON T. CONTENTS HOMEPAGE
