Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
how to make this function??
Author Message
appling

Posted: Tue Feb 24, 2004 3:13 pm   Post subject: how to make this function??

how could i make this program into a function
 code: 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

Posted: Tue Feb 24, 2004 3:20 pm   Post subject: (No subject)

Function Hello
*
*
*
*
end Hello

Hello
appling

Posted: Tue Feb 24, 2004 3:29 pm   Post subject: (No subject)

i dont know how to call the function, so can u write the code and let me have a look?
recneps

Posted: Tue Feb 24, 2004 3:49 pm   Post subject: (No subject)

Functions are simple.

 code: 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

Posted: Tue Feb 24, 2004 4:02 pm   Post subject: (No subject)

sorry, the teacher told me just use the function not the procedure.so...

thank u all the same
naoki

Posted: Tue Feb 24, 2004 4:55 pm   Post subject: (No subject)

The above poster is wrong, that's actually a procedure, not a function

A function goes
 code: 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

 code: if whatever the name (parameters) = true then
appling

Posted: Tue Feb 24, 2004 5:13 pm   Post subject: (No subject)

i tried this
 code: 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

Posted: Tue Feb 24, 2004 5:45 pm   Post subject: (No subject)

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

Posted: Tue Feb 24, 2004 5:49 pm   Post subject: (No subject)

you need to result somthing ,
appling

Posted: Tue Feb 24, 2004 5:50 pm   Post subject: (No subject)

what i need to do if i want the whole array(array a) as my result?
and how to call it?
Thuged_Out_G

Posted: Tue Feb 24, 2004 10:38 pm   Post subject: (No subject)

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

Posted: Wed Feb 25, 2004 6:12 pm   Post subject: (No subject)

oops! haha, sorry there, i kinda started on functions and then got interrupted, and kinda changed to procedure :\
appling

Posted: Wed Feb 25, 2004 9:28 pm   Post subject: (No subject)

after a long time trying, i work it out at last
 code: 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
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 13 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: