S Maxwell
S Maxwell

Reputation: 21

returning two variables that have multiple values

I am trying to use a function that will calculate values for h and then input these values of h into an equation that will calculate n. This is what my code currently looks like...

int findN(double xI, double xF) {


double h = 0.1;
int n;

do {
    printf_s("%8.5f \n", h);
    n = ((xF - xI) / h);
    h = h / 10;


    printf_s("%6d \n", n);
} while (h >= 0.00001);


return n;
}

I know that this function will only return n currently, but as i am new to this i am unsure as to how to also return all the values of h as well as all the values of n... If someone could assist me and show me how to return all the values for n & h, it would be much appreciated.

Thanks.

Upvotes: 2

Views: 66

Answers (5)

dbush
dbush

Reputation: 224102

The simplest way to handle this is change the function to accept pointers to variables that will accept the values of n and h. Then the function will dereference those pointers to update the relevant variables in the calling function.

void findN(double xI, double xF, int *ret_n, double *ret_h)
{
    ...
    *ret_n = n;
    *ret_h = h;
}

Then you can call your function like this:

int n;
double h;
findN(1.2, 3.4, &n, &h);

This method is fine for a relatively small number of parameters. If the number of parameters gets to be too large, you can instead create a struct containing all of the values to be returned either pass in the address of the struct or just return the struct outright.

Upvotes: 0

Moses John
Moses John

Reputation: 1

Read pointers,you will be able to return as many values you want to return,when calling function through main add &h in actual parameters,it means findN(xI,xF,&h) and in declaring the function findN add double *h in formal parameters,that is int findN(double xI,double xF,double *h)...."meaning of * is -value at address of....meaning of & is address of.This will make changes in h globally in this program as the vale is changing in its address.You can return even more values like this using more variables.This is called returning values indirectly.Vote for my answer if its applicable.

Upvotes: 0

oklas
oklas

Reputation: 8220

Typical approach to return multpile values is using arrays and pass its pointer to function:

int f(double *h) {
  h[0] = 1.1;
  h[1] = 2.2;
}

int main()
{
  // create pointer
  double *h;

  // initialize it with memory block
  h = malloc(2*sizeof(double));

  // call the function
  f(h);

  // show output
  printf_s("%8.5f \n", h[0]);
  printf_s("%8.5f \n", h[1]);

  // release memory block
  free(h);

  return 0;
}

Also same array may be created without memory allocation. It is more simple but arrays exists only until execution is not leave away from function scope where it declared.

int main()
{
  // create array
  double h[2];

  // call the function
  f(h);

  // show output
  printf_s("%8.5f \n", h[0]);
  printf_s("%8.5f \n", h[1]);

  return 0;
}

And if you can know count of element only during function call you can allocate array in function and return array by pointer and release array at caller.

double* f() {
  // create pointer
  double *h;

  // some size calculations
  int size = 1+1;

  // initialize it with memory block
  h = malloc(size*sizeof(double));

  // fill the array
  h[0] = 1.1;
  h[1] = 2.2;

  // return array by pointer
  return h;
}

int main()
{
  // create pointer
  double *h;

  // call the function
  h = f();

  // show output
  printf_s("%8.5f \n", h[0]);
  printf_s("%8.5f \n", h[1]);

  // release memory block
  free(h);

  return 0;
}

Upvotes: 1

Archmede
Archmede

Reputation: 1842

Read into pointers if you want to learn more. But essentially by sending h as a pointer it will return it's value to main.

#include <stdio.h>

int findN(double xI, double xF, double h[]) {
    int i = 0;
    int n;

    h[i] = 0.1;
    do {
        i++;
        printf_s("%8.5f \n", *h);
        n = ((xF - xI) / (*h));
        h[i] = h[i-1] / 10;


        printf_s("%6d \n", n);
    } while (h[i] >= 0.00001);


    return n;
}

int main()
{
    double h[100];
    double xI = 1.0, xF = 1.0;
    int n;

    n = findN(xI, xF, h);

    return 0;
}

Upvotes: 0

chux
chux

Reputation: 153582

There are many ways to solve this. Another is to return a struct.

Below, findN() returns one object. It just happens that the object contains two members. This approach is suitable when with small objects. With large objects,other approaches should be considered.

typedef struct {
  int n;
  double h;
} nh;

nh findN(double xI, double xF) {
  nh retval;
  retval.h = 0.1;

  do {
    printf_s("%8.5f\n", retval.h);
    retval.n = ((xF - xI) / retval.h);
    retval.h = retval.h / 10;
    printf_s("%6d\n", retval.n);
  } while (retval.h >= 0.00001);

  return retval;
}

// usage exanple
nh y;
y = findN(1.23, 4.56);
printf_s("h:%8.5f, n:%6d\n", y.h, y.n);

Upvotes: 0

Related Questions