On Oct 14, 2016, at 4:24 PM, Peter Mew <peterm3...@gmail.com> wrote:
> 
> Please tell me to go away if this request is inappropriate
> Im trying to convert a C function into 4D
> I know very very little C, so after 2 days I'm looking for some help
> The C function looks like
[snip]
> int
> cddb_sum(int n)
> 
> {
> 
> int     ret;
> /* For backward compatibility this algorithm must not change */
> ret = 0;
> 
> while (n > 0) {
>        ret = ret + (n % 10);
> 
>        n = n / 10;
> return (ret);
> 
> }
> 
> }

[snip]

> and the subroutine cddb_sum Like This
> 
> c_Longint($position;$addDigits)
> $Position:=$1
> $addDigits:=0;
> While ($Position>0)
> $addDigits:=$addDigits+($Position%10)
> $Position:=int($Position/10)
> End while
> $0:=$adddigits
> 
> I think Im misunderstanding the functioning of cddb_sum

The C version of cddb_sum is written pretty stupidly. The return inside the 
while loop essentially turns the “while” into an “if”, because it is guaranteed 
to run no more than once.

I love early return, but this is absolutely the wrong way to use it.

Jim

**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**********************************************************************

Reply via email to