Sub Program in C++

Hi, recently i doing some work about converting Fortran to C++
Fortran have a function call SUBROUTINE, which can return multiple values include array in the function.
But I can't find any thing that can passing this 2 data type in C++

here the simple code

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
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdio.h>

using namespace std;

// Declare function TRY and accessing the array
void TRY ()
{


	int D[6]={1,2,3,4,5,6};
	int A;
	A=1;
}

int main ()
{
	
	
	
	int A;
	int D[6];

        TRY(); //Calling function TRY
		
	cout<<A<<"\n";
	cout<<D[4];
	
	std::system ("PAUSE");


}


from the code, i need accessing and storing array in other function, i just need the function TRY help me do something then the main function just call the answers from function TRY when need it.
who can help me? thanks...
In your above code, you have declared an integer array and an integer in your function as well as main. But these variables have a scope of only the block they are defined in.. so it won't work the way you want it to... If you want your function to access a variable from the main, pass the address of the variable to the function...
Thanks for your reply, I am new to C++, can you explain it with the code? Thank you very much.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream.h>
#include <conio.h>
#include <stdio.h>

void TRY (int *p, int*q)        //p and q are pointers. They accept address of passed arguments
{
	*p=1;                         //Assigns 1 to the value at address pointed by p
	q[4]=5;                     //Assigns value 5 to the fourth element in the array pointed by q.
}

void main ()
{
	int A;
	int D[6];

	TRY(&A, D);               //Passes the address of variable A and array D to the function..
                                 //The address of an array is the name of the array itself, so no & sign...
	cout<<A<<"\n";
	cout<<D[4];
	getch();

}
@Caprico: you should update your compiler. The headers should be iostream and cstdio (without .h), and main must return int.
Yeah, I know.. I'm using a damn old version of TurboC++... VisualC++ is a lil complicatd to undrstnd.. Anyways, I've shifted to VC++ 10.0, just fr quick compiling i use TC...
There are many simpler, modern compiler-IDE-bundle options; some of them are juts as fast and easy as TC++, and they have the added advantage of being up to date.

just found the solution, thanks guys....

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
#include<iostream>
using namespace std;

int subfunction(int &A,int(&D)[6])
{
	D[0]=1;
	D[1]=2;
	D[2]=3;
	D[3]=4;
	D[4]=5;
	D[5]=6;
	return A;
}

int main()
{
	int D[6];
	int A;
	subfunction(A,D);

	cout<<D[0]<<D[1]<<D[2]<<D[3]<<D[4]<<D[5];
	system("PAUSE");

	return 0;
}
Why does your subfunction return A ? You don't use the returned value, and you don't even need one, since you're passing by reference (not that it makes any sense to - your subfunction doesn't use it).

Also, this: int(&D)[6] is just ugly and far more effort than needed, and inflexible. How about this instead:

int subfunction(int &A,int* D)
Last edited on
ya, i try to return D, but it seems got error, so i jz simply assign a variable and return it... maybe i not sure the code of returning array....


actually my project is much more complex compare to this 1, it return single variable and array in the same sub function.

the sub function is to calculate and assign the answers in to array, and return it, and call it from main function when need it.

i jz simplify it so it is easier to read....

@Caprico , thanks for your effort...
Last edited on
If you want to return a pointer, it's simple:

1
2
3
4
5
6
7
8
9
10
int* subfunction(int* D)
{
	D[0]=1;
	D[1]=2;
	D[2]=3;
	D[3]=4;
	D[4]=5;
	D[5]=6;
	return D;
}


Of course, given that the pointer doesn't actually change throughout the function, there's no reason to return it. The calling code has access to the exact same pointer and can just carry on using it, so even better would be:

1
2
3
4
5
6
7
8
9
void subfunction(int* D)
{
	D[0]=1;
	D[1]=2;
	D[2]=3;
	D[3]=4;
	D[4]=5;
	D[5]=6;
}
Last edited on
Why is it better to pass a pointer instead of a reference to the function?

Passing a reference you make sure the array has exactly 6 elements.
@Moschops , it is a nice and simple reference, i will remember it... thanks...
@Peter87 , as for my project now the number array element is fix, so is ok for me, if it is various size, then maybe need pointer as you mention....
That is exactly the problem. Your function is too specific, it wouldn't be reused.
Instead you could do
1
2
3
4
void initiliaze(int *array, size_t size){
  for( size_t K=0; K<size; ++K)
    array[K] = K+1;
}
(which is 'equivalent' to generate_n)
That way the function will work for whatever array you need.
closed account (1vRz3TCk)
Isn't it a case of "swings and roundabouts"?

If the function you are designing needs to take an array of three ints and will only ever take an array of three ints it would be safer to make it take a reference to an array of three ints?

I would say that it is unwise to give blanket advice based on weather the code is ugly or to specific, but that's just me.
I would say that it is unwise to give blanket advice based on weather the code is ugly or to specific

That's quite a blanket statement :)
Topic archived. No new replies allowed.