Velocity Reviews > Return type

# Return type

danieljmathew@gmail.com
Guest
Posts: n/a

 08-25-2005
Problem:

I want to write a function for retrieving a value from a linked list,
given the position as an int.
Function prototype: int retrieve(int pos, List *l);
There are 2 abnormal conditions - the position given is greater than
the size of the list, or the list is empty. I thought of returning 0 in
case these errors occur.
But there arises the problem - even if there's no error, zero can be
returned (if that's the value of the node a the position pos).

One solution i conjured is:
Change the function to :
int retrieve(int *pos, List *l);
Now pos is passed by reference. If there's an error the function
returns 0. Otherwise it stores the value of the node at position *pos
to the int *pos, and returns 1.
So the calling function first checks whether the return value is 1, and
if so, it prints the value pointed by first argument pos (or whatever
name it has in the calling function):
int pos=14;
List ls;
if(retrieve(&pos,&ls)) printf("%d",pos);

Is there any other, more elegant solution?
Dany

Steffen
Guest
Posts: n/a

 08-25-2005
Hi

> I want to write a function for retrieving a value from a linked list,
> given the position as an int.
> Function prototype: int retrieve(int pos, List *l);
> There are 2 abnormal conditions - the position given is greater than
> the size of the list, or the list is empty. I thought of returning 0 in
> case these errors occur.
> But there arises the problem - even if there's no error, zero can be
> returned (if that's the value of the node a the position pos).
>
> One solution i conjured is:
> Change the function to :
> int retrieve(int *pos, List *l);
> Now pos is passed by reference. If there's an error the function
> returns 0. Otherwise it stores the value of the node at position *pos
> to the int *pos, and returns 1.

I think what you did in principle is the only way to deal with an error,
however i would strongly suggest >not< to use the pos-argument to return
the result, but rater add another argument, either
- a pointer to a boolean that indicates whether an error occured or not,

and simply return the result the usual way
int retrieve(int pos, List *l, int *ok)
- a pointer to where the result is to be placed and the return value
indicating an error
int retrieve(int pos, List *l, int *result)

Steffen

CBFalconer
Guest
Posts: n/a

 08-25-2005
"(E-Mail Removed)" wrote:
>
> Problem:
>
> I want to write a function for retrieving a value from a linked list,
> given the position as an int.
> Function prototype: int retrieve(int pos, List *l);
> There are 2 abnormal conditions - the position given is greater than
> the size of the list, or the list is empty. I thought of returning 0 in
> case these errors occur.
> But there arises the problem - even if there's no error, zero can be
> returned (if that's the value of the node a the position pos).

Simply return a pointer to the list item. You then retrieve the
value by dereferencing the pointer. If the position does not exist
return null.

list *p;

p = retrieve(int pos, list *l)
if (p) value = p->datavalue;
else /* no such entry */;

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

akarl
Guest
Posts: n/a

 08-25-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Problem:
>
> I want to write a function for retrieving a value from a linked list,
> given the position as an int.
> Function prototype: int retrieve(int pos, List *l);
> There are 2 abnormal conditions - the position given is greater than
> the size of the list, or the list is empty. I thought of returning 0 in
> case these errors occur.
> But there arises the problem - even if there's no error, zero can be
> returned (if that's the value of the node a the position pos).
>
> One solution i conjured is:
> Change the function to :
> int retrieve(int *pos, List *l);
> Now pos is passed by reference. If there's an error the function
> returns 0. Otherwise it stores the value of the node at position *pos
> to the int *pos, and returns 1.
> So the calling function first checks whether the return value is 1, and
> if so, it prints the value pointed by first argument pos (or whatever
> name it has in the calling function):
> int pos=14;
> List ls;
> if(retrieve(&pos,&ls)) printf("%d",pos);
>
> Is there any other, more elegant solution?

I would do something like

/* Retrieves element at pos in list.
Precondition: (pos >= 0) && (pos < count(list)) */

int retrieve(int pos, List *list)
{
assert((pos >= 0) && (pos < list->count));
...
}

where count(list) returns list->count.

August

Default User
Guest
Posts: n/a

 08-25-2005
(E-Mail Removed) wrote:

> Problem:
>
> I want to write a function for retrieving a value from a linked list,
> given the position as an int.
> Function prototype: int retrieve(int pos, List *l);
> There are 2 abnormal conditions - the position given is greater than
> the size of the list, or the list is empty. I thought of returning 0
> in case these errors occur.
> But there arises the problem - even if there's no error, zero can be
> returned (if that's the value of the node a the position pos).

What about -1?

Brian

danieljmathew@gmail.com
Guest
Posts: n/a

 08-25-2005
Thanx everybody.

I think i found a solution fitting my requirements in CBF's suggestion.
I'll return with more newbie doubts if any.

Dany.

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post andrew cooke Python 1 04-03-2011 01:02 AM Diego Martins C++ 5 09-05-2006 08:38 PM gk245 C Programming 2 05-06-2006 01:03 AM Greenhorn C Programming 15 03-06-2005 08:19 PM DaKoadMunky C++ 8 05-14-2004 09:46 PM

Advertisments