Click here to Skip to main content
15,888,221 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
#include <stdio.h>



void suma(int a, int b, int *sum, int  *average)
{

    *sum = a + b;

    *average = *sum / 2;
}

int main()
{

    int l = 3, j = 5, sum;
    int  average;

    suma(l, j, &sum, &average);
    printf("%d\n", sum);
    printf("%d\n", average);
return 0;
}


What I have tried:

I can't undestand the logic behind it \
The question was to get sum and average of 2 number by using fucntion and passing reference
Posted
Updated 30-May-22 2:08am
Comments
Greg Utas 29-May-22 19:33pm    
That's not passing references, but pointers. References would be int& sum and int& average.

Greg is right, but also wrong: C does not have any concept of references, that was introduced with C++ - so you are basically using the wrong terms which can get confusing. What your code is passing are pointers.

The thing you need to remember is that in C all function parameters are passed by value - which means that a copy of the variable content is passed, not the actual variable. So anything you do to that value inside the function will not change anything in the outside world:
C
int foo (int x)
   {
   printf("Begin foo x = %u\n", x);
   x = x * 2;
   printf("End foo   x = %u\n", x);
   return x;
   }
int main()
   {
   int x = 666;
   printf("Before call x = %u\n", x);
   int y = foo(x);
   printf("After call x  = %u\n", x);
   printf("After call y  = %u\n", y);
   }
Will give you this:
Before call x = 666
Begin foo x = 666
End foo   x = 1332
After call x  = 666
After call y  = 1332
The changes are made to a copy of the value in the variable and don't affect the outside world.

Think about it: why do that do that? Because it would cause real problems if they didn't! If the actual variable was passed through, (i.e. the parameter was passed by reference) you would get this:
int main()
   {
   int x = 666;
   printf("Before call x = %u\n", x);
   int y = foo(x);
   printf("After call x  = %u\n", x);
   printf("After call y  = %u\n", y);
   }
Will give you this:
Before call x = 666
Begin foo x = 666
End foo   x = 1332
After call x  = 1332
After call y  = 1332
Which is not a problem at all. But ... what if I do this:
C
int y = foo(666);

Now foo has to change the value of a constant - and that would make code really difficult to understand:
C
foo(666)
printf("After call 666 = %u\n", 666);
Would have to give you
After call 666 = 1332


So if you can't affect the outside world when you pass a variable into a function, what do you do when you need to return two or more values?
Simple: you pass the address of a variable as a pointer to the variable! The function gets a copy of the pointer, which is fine - but the function can now change the value of the variable by using the pointer, which is the address of the variable.
int foo (int x, int* py)
   {
   printf("Begin foo x = %u, y = %u\n", x, *py);
   x = x * 2;
   *py = *py + 1;
   printf("End foo   x = %u, y = %u\n", x, *py);
   return x;
   }
int main()
   {
   int x = 666, y = 100;
   foo(x, &y);
   printf("After call x = %u, y = %u\n", x, y);
   }
Will give you:
Begin foo x = 666, y = 100
End foo   x = 1332, y = 101
After call x = 666, y = 101
Now go back to your code and look at what is happening!
 
Share this answer
 
Comments
CPallini 30-May-22 1:55am    
5.
merano99 30-May-22 2:17am    
5.
if you want to work with references than the code could look like this
C++
void suma(int a, int b, int &sum, int  &average)
{
    sum = a + b;
    average = *sum / 2;
}
But this isnt good code. Better is to use vector than you code write functions like
C++
long sum(vector& input);
long average(vector& input);
where the vector can have a lot of elements - and not only two.
 
Share this answer
 
Comments
Richard MacCutchan 30-May-22 8:20am    
You cannot use references in C.
merano99 30-May-22 18:24pm    
Are you shure? average = *sum / 2;

I would suggest to use the vector in a class, and not as parameter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900