0

Possible Duplicate:
returning multiple values from a function

There is this exercise I have, and it says that I have to make a function that will read with appropriate inducements the height and number of hits a ball hits the ground.

How can a function return two values? Doesn't it only return one? What will it return?

float insert(int h,int n)
{
   printf ("Give a value for height and number of hits");
   scanf ("%d %d",&h,&n);
   return
}
Community
  • 1
  • 1

3 Answers3

1

As an aside, the function you give returns nothing which is an error.

A function can have only a single return value. If you want to return multiple values you can:

  1. Return a struct containing the values.
  2. Pass the return values as parameters using pointers.

An example of the option 2:

void divmod(int a, int b, int *div, int *mod)
{
    *div = a/b;
    *mod = a%b;
}

Call the function like this:

int div;
int mod;
divmod(666, 42, &div, &mod);

I intentionally chose a different example because I couldn't work out what you want to do with your float return value.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

You can either return a struct that encapsulates the two values, or use two pointer parameters in which the function will store the values.

typedef struct
{
    double height;
    int hits;
} BallParameters;


BallParameters insert()
{
    BallParameters ret;
    printf ("Give a value for height and number of hits");
    scanf ("%f %d",&ret.height,&ret.hits);
    return ret;
}

/* ~~~ or ~~~ */

void insert(double *height, int *hits)
{
    printf ("Give a value for height and number of hits");
    scanf ("%f %d",height,hits);
}
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
0

For a homework assignment, they'll probably be happy with you passing in the parameters to fill in.

void insert(int* h, int* n)
{
   ...
   scanf("%d %d", h, n);
}

// called like:
// int height, number;
// insert(&height, &number);

But you can always get tricky and return a struct

typedef struct
{
  int h;
  int n;
} S;

S insert()
{
  S s;
  ...
  scanf ("%d %d" , &s.h, &s.n);
  return s;
}
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
miked
  • 3,458
  • 1
  • 22
  • 25
  • 3
    Careful, this is C, not C++, so references (used in the first example) are not available; you should replace them with pointers. Also, in C when using a `struct` type you must always qualify it with the `struct` keyword (unless you use the common `typedef` idiom). – Matteo Italia Jun 13 '11 at 16:31
  • Correct you are. I wrote it the C-way, and then figured I should change it because I spend most of my time in the C++ side of things and usually get yelled at when I write plain old C code. I got mixed up what tag I was filtering under this time :) – miked Jun 13 '11 at 16:42
  • I edited it to show how the "C way"(s) - hope you don't mind :) – Karl Knechtel Jun 13 '11 at 17:03
  • What are the dots (...) suppose to mean? – Nikos Angelis Jun 13 '11 at 17:10
  • The ... is just stuff I didn't want to type (like the printf prompt). – miked Jun 13 '11 at 17:26
  • So I just call the void function and it's ready? – Nikos Angelis Jun 13 '11 at 17:33