[comp.lang.pascal] What's wrong with this:

tpehrson@javelin.sim.es.com (Tim Clinkenpeel) (04/20/91)

hi, it's me again.  please DO NOT respond with email, as my last question 
received over 30 responses.

why doesn't this work:

function upper(I:string):string;
 var z:integer; 
 begin
  for z:=1 to length(I) do
  if (ord(I[z])>96) and (ord(I[z])<123) then I[z]:=upcase(I[z]);
 end;

i have a feeling i'm overlooking the obvious (to people with a decent text).
in the index under function pages are referenced like this one (entire page
quoted):
 "In this chapter we will be discussing functions"
however, the discussion of functions is superficial at best.  NOTE: i have
deemed my text fit only for fire fodder at this point.

and, as always, thank you for your response.
-- 
one two! one two! & through & through /=============================/ hear your
the vorpal blade went snicker-snack! / tpehrson@javelin.sim.es.com / death boon 
he left it dead, and with his head  /=====aka: tim clinkenpeel====/ the wail of
he went galumphing back [DISCLAIMER: send SASE to: disclaimer, 84151-0521] DOOM 

ebergman@isis.cs.du.edu (Eric Bergman-Terrell) (04/20/91)

two things about your code:

1.	it looks like you're trying to make a permanent change to the
	parameter - if so, it needs to be passed by reference (i.e. it
	needs to be a VAR parameter).

2.	you've declared a function, but haven't returned a value (by
	assigning a value to the function's name.

Really you have two options here:

1.	Write a procedure that changes the value of its parameter

2.	Write a function that returns the changed string


Terrell

ebergman@isis.cs.du.edu (Eric Bergman-Terrell) (04/20/91)

And another thing:  I'm guessing that character 96 & 123 (or whatever they
were) are 'a' and 'z', respectively.

You can use the characters themselves in your program:

if (string[i] >= 'a') and (string[i] <= 'z')
	then ...


even better, you can use a set of the characters between 'a' and 'z':

if (string[i] in ['a'..'z'])
	then ...


Hang in there - you've made a good start!


Terrell

amb43790@uxa.cso.uiuc.edu (Anthony M Brummett) (04/20/91)

tpehrson@javelin.sim.es.com (Tim Clinkenpeel) writes:

>hi, it's me again.  please DO NOT respond with email, as my last question 
>received over 30 responses.

>why doesn't this work:

>function upper(I:string):string;
> var z:integer; 
> begin
>  for z:=1 to length(I) do
>  if (ord(I[z])>96) and (ord(I[z])<123) then I[z]:=upcase(I[z]);
> end;

>i have a feeling i'm overlooking the obvious (to people with a decent text).
>in the index under function pages are referenced like this one (entire page
>quoted):
> "In this chapter we will be discussing functions"
>however, the discussion of functions is superficial at best.  NOTE: i have
>deemed my text fit only for fire fodder at this point.

>and, as always, thank you for your response.
>-- 
>one two! one two! & through & through /=============================/ hear your
>the vorpal blade went snicker-snack! / tpehrson@javelin.sim.es.com / death boon 
>he left it dead, and with his head  /=====aka: tim clinkenpeel====/ the wail of
>he went galumphing back [DISCLAIMER: send SASE to: disclaimer, 84151-0521] DOOM 
Probably the best thing to do is to delete the if clause and make it:
  for z:=1 to length(I) do
    I[z]:=upcase(I[z]);
because upcase returns an uppercase letter if I[z] is upper or lower case.

userW6BP@um.cc.umich.edu (04/20/91)

In article <1991Apr20.011454.25016@javelin.sim.es.com>, 
tpehrson@javelin.sim.es.com writes:
>why doesn't this work:
>
>function upper(I:string):string;
> var z:integer; 
> begin
>  for z:=1 to length(I) do
>  if (ord(I[z])>96) and (ord(I[z])<123) then I[z]:=upcase(I[z]);
> end;
>
>i have a feeling i'm overlooking the obvious ...
 
Yes, you are. 
 
You need to set the function to the value of the altered string:
 
FUNCTION upper(I:string):string;
 
VAR
  z : INTEGER;
 
BEGIN 
  FOR z := 1 TO LENGTH(I) DO
      IF (ORD(I[z]) > 96) AND (ORD[I[z]) < 123)
         THEN I[z] := UPCASE(I[z]);
 
  upper := I;     (* This is what you were missing *)
END;
 
 
--Allan

dave@tygra.UUCP (David Conrad) (04/22/91)

In article <1991Apr20.011454.25016@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
>hi, it's me again.  please DO NOT respond with email, as my last question 
>received over 30 responses.
>

Why should this newsgroup be swamped instead of your mailbox?
Offer to summarize to the net instead of asking people to post answers.

>why doesn't this work:
>
[example deleted]

See the Summary line.
--
David Conrad, tygra!dave
dave%tygra@sharkey.cc.umich.edu
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave%tygra@sharkey.cc.umich.edu

cse4a004@eve.wright.edu (JOHN BLACKWELL) (05/03/91)

In article <1991Apr20.011454.25016@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
>
>why doesn't this work:
>
>function upper(I:string):string;
> var z:integer; 
> begin
>  for z:=1 to length(I) do
>  if (ord(I[z])>96) and (ord(I[z])<123) then I[z]:=upcase(I[z]);
> end;
>
Try a new header:
 procedure upper(var I:string);

That should let you return changes to I.

		-John Blackwell

protonen@daimi.aau.dk (Lars J|dal) (05/06/91)

cse4a004@eve.wright.edu (JOHN BLACKWELL) writes:

>In article <1991Apr20.011454.25016@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
>>
>>why doesn't this work:
>>
>>function upper(I:string):string;
>> var z:integer; 
>> begin
>>  for z:=1 to length(I) do
>>  if (ord(I[z])>96) and (ord(I[z])<123) then I[z]:=upcase(I[z]);
>> end;
>>
>Try a new header:
> procedure upper(var I:string);

>That should let you return changes to I.

>		-John Blackwell

Correct, but if the guy really wants a function (so the argument doesn't
have to be a variable) he could just as well keep the header as it is.
Then he has forgotten to assign the function the return value, so the 
following line should be added at the end of the function:

upper:=I;

+--------------------------------------------------------------------------+
|      Lars J|dal       |       (put your favourite quotation here)        |
| protonen@daimi.aau.dk |                                                  |
|--------------------------------------------------------------------------|
| Computer Science Department  -  Aarhus University  -  Aarhus  -  Denmark |
+--------------------------------------------------------------------------+