return parameters

return parameters..i dont know if the translation is ok, because the book am using is in italian..we say( parametri di uscità..like exit parameters, i hope is the return parameters we talking about here...)
ok so lets say is the return parameter we talking about...when i have to return a single parameter..thats easy..(.return parameter ;) and we're done...but what if I have to return as parameter the coordinates of lets say a cell of a matrix?..those are two figures..how is that done? lets say i have to return too parameters on exiting( can that be said in english?) ok i want to return two parameters..how is that done? I hope i explained myself enough...
thank you :)

and one last questiong...if in my code i have like int num = 4000; declared...some where in the code i can still do..num = 5...and the initial value of num declared as integer 4000 will be replaced by 5? that is a correct syntax right? thank you
Last edited on
I think the book says about output parameters. They are parameters that are declared either as reference to objects or pointers to objects.

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

void f( int *p )
{
   *p = 20;
}

void f( int &x )
{
   x = 30;
}

int main()
{
   int x = 10;

   std::cout << "x = " << x << std::endl; 

   f( &x );

   std::cout << "x = " << x << std::endl; 

   f( x );

   std::cout << "x = " << x << std::endl; 
} 


if you want to return two or more objects you can use a structure that contains data members that correspond to desired objects.

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

struct Point
{
   int x;
   int y;
}

Point make_point()
{
   Point p = { 10, 20 };
   return p;
}

int main()
{
   int u;
   int v;

   Point p = make_point();

   u = p.x;
   v = p.y;

   std::cout << "u = " << u << ", v = " << v << std::endl;
}
closed account (Dy7SLyTq)
to the first: i believe initializer_list is the solution. to the second: yes

edit:
and we call them return values fyi
Last edited on
Topic archived. No new replies allowed.