
-----------------------------------
appling
Tue Feb 24, 2004 3:13 pm

how to make this function??
-----------------------------------
how could i make this program into a function
var x, y : int
var prime : array 1 .. 7 of int := init (2, 3, 5, 7, 11, 13, 15)
var a : array 1 .. 7 of int
get x
for i : 1 .. 7
    y := 0
    loop
          if x mod prime (i) = 0 then
                x := x div prime (i)
                y := y + 1
          else
                exit
          end if
    end loop
    a (i) := y
end for


for k : 1 .. 7
    put a (k)
end for


i tried a lot, who can help me??? :?:  :?:

-----------------------------------
shorthair
Tue Feb 24, 2004 3:20 pm


-----------------------------------
Function Hello
*
*
*
*
end Hello



Hello

-----------------------------------
appling
Tue Feb 24, 2004 3:29 pm


-----------------------------------
i dont know how to call the function, so can u write the code and let me have a look?

-----------------------------------
recneps
Tue Feb 24, 2004 3:49 pm


-----------------------------------
Functions are simple.

procedure ProcedureNameHere

%Code here...
var x, y : int 
var prime : array 1 .. 7 of int := init (2, 3, 5, 7, 11, 13, 15) 
var a : array 1 .. 7 of int 
get x 
for i : 1 .. 7 
    y := 0 
    loop 
          if x mod prime (i) = 0 then 
                x := x div prime (i) 
                y := y + 1 
          else 
                exit 
          end if 
    end loop 
    a (i) := y 
end for 


for k : 1 .. 7 
    put a (k) 
end for 
% end code.

end ProcedureNameHere

% then to call, just type the proc name.
ProcedureNameHere

Simple Simple.

-----------------------------------
appling
Tue Feb 24, 2004 4:02 pm


-----------------------------------
sorry, the teacher told me just use the function not the procedure.so...

thank u all the same

-----------------------------------
naoki
Tue Feb 24, 2004 4:55 pm


-----------------------------------
The above poster is wrong, that's actually a procedure, not a function

A function goes

function whateverthename (place parameters here) : whatever returning value (can be boolean, integer, string, real)

put code here
should end in
result whatever returning value (i.e. result true, result num ... )

end whateverthename




calling it is different than a procedure. You can't just run it normally. For example, if it returned a boolean answer, you would go


if whatever the name (parameters) = true then


-----------------------------------
appling
Tue Feb 24, 2004 5:13 pm


-----------------------------------
i tried this 
function KKK (var x : int) : array 1 .. 7 of int
    var prime : array 1 .. 7 of int := init (2, 3, 5, 7, 11, 13, 15)
    var a : array 1 .. 7 of int
    var y : int
    for i : 1 .. 7
        y := 0
        loop
            if x mod prime (i) = 0 then
                x := x div prime (i)
                y := y + 1
            else
                exit
            end if
        end loop
        a (i) := y
    end for
end KKK


var X : int
get X
put KKK (X)
but turing said "illegal put item" ???
i dont know where i am wrong

-----------------------------------
naoki
Tue Feb 24, 2004 5:45 pm


-----------------------------------
i dunno, I don't really feel like running it but I'm guessing that Turing won't let you have a whole ARRAY as your returning value. I mean, think about it: Turing has NO idea how you want to output or result an array. Do you want to add/catenate the values? Do you want them output on the same line or seperate lines? Ambiguous cases aren't Turing's specialty

also, if you look in your function you never declared "result" and then whatever you wanted. You just put exit. That means that it's supposed to return something, but you're not telling it what.

Change the resulting value, and make sure you have one in your program. If you want to return an array value, that could work, but not the entire array

-----------------------------------
shorthair
Tue Feb 24, 2004 5:49 pm


-----------------------------------
you need to result somthing ,

-----------------------------------
appling
Tue Feb 24, 2004 5:50 pm


-----------------------------------
what i need to do if i want the whole array(array a) as my result?
and how to call it?

-----------------------------------
Thuged_Out_G
Tue Feb 24, 2004 10:38 pm


-----------------------------------
your program doesnt output anything, it just does some variable changes if some conditions are true, so it would make more sense to make the program a procedure with parameters instead of a function.

-----------------------------------
recneps
Wed Feb 25, 2004 6:12 pm


-----------------------------------
oops! haha, sorry there, i kinda started on functions and then got interrupted, and kinda changed to procedure :\

-----------------------------------
appling
Wed Feb 25, 2004 9:28 pm


-----------------------------------
after a long time trying, i work it out at last  :D  :D  :D 

function get_fators (x : int) : array 1 .. 7 of int
    var prime : array 1 .. 7 of int := init (2, 3, 5, 7, 11, 13, 17)
    var y, z : int
    z := x
    var a : array 1 .. 7 of int
    for i : 1 .. 7
        y := 0
        loop
            if z mod prime (i) = 0 then
                z := z div prime (i)
                y := y + 1
            else
                exit
            end if
        end loop
        a (i) := y
    end for
    result a
end get_fators


var X : int
get X

for i : 1 .. 7
    put get_fators (X) (i)
end for

is this right/?

thanks here to all the people :!:  :!:
