howto pass values to & from

This is a discussion on howto pass values to & from within the C Programming forums, part of the General Programming Boards category; how do i pass agruments to a function, perform math ops on the values, then return the values ( more ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    11

    howto pass values to & from

    how do i pass agruments to a function,
    perform math ops on the values,
    then return the values ( more than one value )

    thanx in advance
    someone once told me i could make money at this ...

  2. #2
    Mr. Unregistered
    Guest
    I'm going to sleep and don't have much time, but you can't normally return more than 1 value, unless you do it in a weird way. Use pointers for more than 1 value. Don't you have a book on C?

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You pass the memory address of the argument to the function by putting the & sign (reference) in front of the variable name.
    In the function use the * (dereferencing) to access the value of result:

    Code:
    void add(int a, int b, int *result)
    {
      *result = a + b;
    }
    
    int main()
    {
      int a = 10;
      int b = 2;
      int result;
    
      /* pass address of result */
      add(a, b, &result);
      printf("Result = %d\n", result);
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    11

    will this work?

    how bout sumthin like this:

    int buffer[3];

    main(){

    add();
    printf("%i, %i, %i:, buffer[1], buffer[2], buffer[3]);

    }

    int add(void){

    buffer[1] = 1+1;
    buffer[2] = 2+2;
    buffer[3] = 3+3;

    return 0;
    }
    someone once told me i could make money at this ...

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >then return the values ( more than one value )
    Why not just write the function in such a way that it takes the values and returns the one result of the operation on those variables, then you can handle the management of variables in main.
    Code:
    #include <stdio.h>
    #define BUFFER_SIZE 5
    
    static int addTwo ( int one, int two ) { return one + two; }
    
    int main ( void )
    {
      int numberList[BUFFER_SIZE], x;
      /* For adding two numbers together, just call AddTwo. */
      for ( x = 0; x < BUFFER_SIZE; x++ )
        numberList[x] = addTwo ( x + 1, x + 1 );
      /* For more than two, add the result of addTwo to a
      ** number, or call addTwo more than once as long as
      ** the order in which addTwo is called doesn't matter.
      */
      numberList[3] = 10 + addTwo ( 4, 5 );
      numberList[4] = addTwo ( 6, 6 ) + addTwo ( 1, 2 );
      return 0;
    }
    This saves you from having to use global variables and you keep the parameter list for the add function short and easy to read.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    While that solution would work (barring the missing closed quotation on the printf, seems you hit colon instead), it doesn't actually involve any variable passing. Instead you are declaring a global variable that all functions have unrestricted access to.

    To tell if a function is passed something, look at the part between the parenthesis after the function name. Anything in there is passed to the function. For instance:

    Code:
    int foo(int bar)
    has one variable passed to it. It is an integer with the identifier "bar".

    In C, and all the languages I've ever used, variables are normally passed by value. This means when you call a function, the program makes a copy of everything you are passing to the function (an integer, in our example) and give the function that. Because the function is working with copies instead of the original, any changes made inside the function will not carry back to the calling function once the routine is complete.

    The solution to this? Passing by reference. It is a rather complicated topic, so I can't go over it all now, but imagine it as passing the address of the data to a function instead of a copy of the data. This way, the program can go directly to this address and modify the data there, so any changes made are still relevant once the the called function is done.

    There are dozens of posts on the syntax involved, so if you're interested, search the boards for "pointer". If you decide it isn't quite what you need, the other posts have good recommendations.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is a stupid solution to the problem at hand but it does accomplish exacly what you are wanting:

    Code:
    //this function will subtract 5 from x, add 1 to y and divide z by 6
    long *dosomething(long x, long y, long z) {
        long *rtrn_vals  = (long *)malloc(12);
        if(rtrn_vals != NULL) {
            rtrn_vals[0] = x-5;
            rtrn_vals[1] = y+1;
            rtrn_vals[2] = z/6;       
        }
        return rtrn_vals;
    }
    As you can see, each index in the array represents a different variable. I call this way stupid because you are going to have to invest a little more time into error handling, also it makes your code less sensible to a more experienced programmer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 04:04 AM
  2. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 07:30 AM
  3. Pass by reference
    By mcgeady in forum C Programming
    Replies: 11
    Last Post: 02-17-2005, 03:01 AM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 02:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21