Shop OBEX P1 Docs P2 Docs Learn Events
Is this clean? — Parallax Forums

Is this clean?

Jay KickliterJay Kickliter Posts: 446
edited 2010-08-18 02:39 in General Discussion
I'm trying to translate some C code to Spin/PASM, but I'm having a hard time. It may be me, but I'm finding a lot the code less than intuitive. Is it good practice to have funtcions that operate but do not return anything? Here's an example:

void Vector_Scale(float vectorOut[3],float vectorIn[3], float scale2)
{
for(int c=0; c<3; c++)
{
vectorOut[c]=vectorIn[c]*scale2;
}
}

void Vector_Add(float vectorOut[3],float vectorIn1[3], float vectorIn2[3])
{
for(int c=0; c<3; c++)
{
vectorOut[c]=vectorIn1[c]+vectorIn2[c];
}
}

Comments

  • LeonLeon Posts: 7,620
    edited 2010-08-16 04:13
    The functions are modifying an array, so there is nothing to return.

    Get a copy of Kernighan and Ritchie if you need to understand the C language.
  • Andrey DemenevAndrey Demenev Posts: 386
    edited 2010-08-16 04:34
    This is normal. Note that function is defined with void return type - meaning that you are not intending to return anything.

    Agreed with Leon - K&R is a must read for C learning
  • wjsteelewjsteele Posts: 697
    edited 2010-08-16 04:44
    Is it good practice to have funtcions that operate but do not return anything?

    It sure is... methods can be two basic types, ones that return data and ones that don't. Some languages refer to these as Functions and Subroutines (or just as "Sub" like in VisualBasic.) In most C derived languages, the word "void" indicates the null return value.

    It seems you're doing something with 3D graphics objects, correct?

    Bill
  • LeonLeon Posts: 7,620
    edited 2010-08-16 04:50
    In some languages, Pascal comes to mind, there are two types of sub-program - functions and procedures. The former returns a value, the latter doesn't.
  • Jay KickliterJay Kickliter Posts: 446
    edited 2010-08-16 05:53
    I have K&R (couldn't call myself a nerd if I didn't). I guess I picked a bad example, or maybe I've just gotten rusty. Thanks for the replies. At least I know it's me, not you:) That little snipped came from some Arduino code for the SparkFun Razor AHRS. It isn't running fast enough for me (50 Hz), so if I cant speed it up on the Razor I'm just going to use the board as an IMU and process the data on the prop.

    Thanks.
  • ercoerco Posts: 20,261
    edited 2010-08-16 09:10
    50 Hz is exactly the right frequency to send servo pulses (20 milliseconds between pulses). So you still inadvertently came up with something useful! :)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-08-16 09:24
    Also, don't forget that Spin's method parameters are always passed by value. If you want to operate on arrays, you have to pass their addresses using the @ notation. Then, inside the method, use something like long[addr] to access and/or modify the individual elements.

    -Phil
  • mparkmpark Posts: 1,322
    edited 2010-08-16 09:25
    This is normal. Note that function is defined with void return type - meaning that you are not intending to return anything.

    Agreed with Leon - K&R is a must read for C learning

    Curious—does K&R even mention void? My copy doesn't.
  • LeonLeon Posts: 7,620
    edited 2010-08-16 11:21
    It's in my copy. You must have the first edition.
  • TimmooreTimmoore Posts: 1,031
    edited 2010-08-16 13:23
    Early versions of C only returned int. It just happened pointers fitted in an int.
  • HollyMinkowskiHollyMinkowski Posts: 1,398
    edited 2010-08-18 02:39
    There are several good C tutorial pdf files on the net.
    Just do a google search using C tutorial pdf

    @erco
    I like your new avatar!
Sign In or Register to comment.