Re: Updating parameters in a procedure / function

new topic     » goto parent     » topic index » view thread      » older message » newer message

don cole wrote:
> 
> ZNorQ wrote:
> > 
> > don cole wrote:
> > > 
> > > ZNorQ wrote:
> > > > 
> > > > I want to create a function that adds an element to a sequence, and
> > > > returns
> > > > the index in the sequence. I want it to be flexible, so that it can
> > > > update any sequence - but how do I do that?
> > > > 
> > > > For example;
> > > > 
> > > > }}}
<eucode>
> > > > atom     idx             idx = 0
> > > > sequence myCars          myCars = {}
> > > > sequence myPhones        myPhones = {}
> > > > sequence myGirlFriends   myGirlFriends = {}
> > > > 
> > > > function addElement(sequence Element, sequence mySequence)
> > > >   mySequence = append(mySequence, Element)
> > > >   return length(mySequence)
> > > > end function
> > > > 
> > > > idx = addElement("Audi", myCars)
> > > > idx = addElement("99 99 99 99", myPhones)
> > > > idx = addElement("Trine", myGirlFriends)
> > > > </eucode>
{{{

> > > > 
> > > > Regards,
> > > > Kenneth / ZNorQ
> > > 
> > > Hello ZNorQ,
> > > 
> > > It seems to me you would want:
> > > 
> > > }}}
<eucode>
> > >  atom     idx             idx = 0
> > >  sequence myCars          myCars = {}
> > >  sequence myPhones        myPhones = {}
> > >  equence myGirlFriends   myGirlFriends = {}
> > > 
> > > function addElement(sequence Element, sequence mySequence)
> > >   mySequence = append(mySequence, Element)
> > >   return mySequence
> > > end function
> > > 
> > >  myCars=addElement("Audi",myCars)
> > >  idx=length(myCars)
> > > 
> > >  myPhones = addElement("99 99 99 99", myPhones)
> > >  idx=length(myPhones)
> > > 
> > >  myGirlFriends = addElement("Trine", myGirlFriends)
> > >  idx=length(myGirlFriends)
> > > 
> > > ----------------also you could-------------------
> > > function addElement(sequence Element, sequence mySequence)
> > >   mySequence = append(mySequence, Element)
> > >   return {mySequence }& {length(mySequence})
> > > end function
> > > 
> > >   junk=addElement("Audi",myCars)
> > >   myCars=junk[1]
> > >   idx=junk[2]
> > > 
> > >   junk=addElement("99 99 99 ",myPhones)
> > >   myPhones=junk[1]
> > >   idx=junk[2]
> > > 
> > >   junk=addElement("Trine", GirlFriends)
> > >   myGirlFriends=junk[1]
> > >   idx=junk[2]
> > > 
> > > > </eucode>
{{{

> > > 
> > > 
> > > Don Cole
> > 
> > Hey Don,
> > 
> > Yeah, I thought of both those examples, but I was kinda hoping I could do
> > it in one go only, like in my example... :) I remember back in the pascal
> > days, you could use a "var" statement (I think) in the procedure/function
> > declaration, and that would tell it that it should manipulate the global
> > variable, instead of using a copy of the global. I guess we call them
> > pointers.. :)
> > 
> > Regards,
> > Kenneth
> 
> 
> Hello ZNorQ ,
> 
> As for pointers how about:
> 
> I'll just do one database in my example.
> }}}
<eucode>
> function addElement(sequence Element, sequence mySequence)
>   integer pointer_one,pointer_two
>     pointer_one=length(mySequence)+1
>       mySequence = append(mySequence, Element)
>     pointer_two=length(mySequence)
>  return {pointer_one,pointer_two)
> end function
> 
> anns_idx=addElement(myGirlFriends,"Ann")
> 
> </eucode>
{{{

>  Just a thought.
> Don Cole

Hey Don,

Not sure if we're talking about the same type of pointers. I was talking
about a pointer to the memory address of the global sequence so that the
function / procdure updates the global instead of creating a local copy
that it manipulates.

I.e.

function addItem(sequence myItem, sequence *mySequence)
  .. some code that adds info to mySequence ..
  return length(mySequence)
end function


This way I could pass ANY random global sequences that I create, and pass
that into my function, the function would not create a local copy of the
sequence, but instead manipulate the global one. I guess this could save
some memory also, and that would be an idea for very large and complex
programs.

Please forgive me if I misunderstood your suggestion.. :)

PS! My intention is not to create yet another topic about Euphoria and
pointers, so please lets leave it at what suggestions I've had for now..
I think I can utilize those I've already got.. :)

Kenneth

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu