Anologous to subroutine in C++

In summary, the new user struggles to find a similar subroutine in C++ to the Fortran example. They try using pointers and array notation, but ultimately are not satisfied with the result.
  • #1
sketos
56
0
Hello,

I am relevant new user to C++ while i am quit comfortable using fortran!

One of my favourite thing to use in fortran was the subroutines but i struggling to find something similar in C++. Let me give you an example:

In Fortan:

subroutine test(Tabular1,Tabular2)
...
...
Tabular2(i)=expresion<Talular1(i)>
...
...
end​

while in c++ it's a bit tricky to do the same easy task. What i tried to do is to use pointers (for example i input an array in func and get another array as a return after i have done some calculations using the input array).

...
...
int func(int *pt1,int *pt2)
{
for(int i=0 ; i<5 ; i++)
{
*(pt2+i)=*(pt1+i)+1;

}
return *pt2;
}

int main()
{

int vec[5]={1,1,1,1,1};
int vac[5]; func(&vec[0],&vac[0]);

for(int i=0 ; i<5 ; i++)
{
cout<<vac<<endl;

} return 0;
}

Is there another way to do this more straitforward or i have to get used to pointers?
 
Technology news on Phys.org
  • #2
First, why did you make your function return an int, when all you want to do is call it without receiving a return-value? The equivalent of a Fortran 'subroutine' is to declare a function as 'void' and not return anything at all.

Code:
void func(int *pt1, int *pt2)
{
  for(int i=0; i<5; i++)
  {
    *(pt2+i) = *(pt1+i) + 1;
  }
}

// and in your main()

func (&vec[0], &vac[0]);

Second, you don't have to pass explicitly "the address of the first element of the array" when you call the function. Simply giving the name of the array has that effect.

Code:
func (vec, vac);

Third, you can use array-style notation inside the function, both to declare the array parameters, and to access elements of the array:

Code:
void func(int pt1[], int pt2[])
{
  for(int i=0; i<5; i++)
  {
    pt2[i] = pt1[i] + 1;
  }
}

You can specify the size of the arrays as 5 in the function, but that's not necessary, because the size is already determined in the main() function. Just make sure you don't try to go beyond the fifth element in either one!
 
  • Like
Likes sketos
  • #3
Thank you very much! But if let's say i want to use the pt2[ ] (in your case) later on my main program?? the void function does not give me this ability...
 
  • #4
sketos said:
But if let's say i want to use the pt2[ ] (in your case) later on my main program??
Why not just use vac[], instead? They're simply two different names for the same thing. One name is used in main(), the other name is used in func().
 
  • #5
OOOOooo i though that this was not possible...

int func(int vec[],int vac[])
{
for(int i=0 ; i<5 ; i++)
{
vac=vec+1;

}

}

int main()
{

int vec[5]={1,1,1,1,1};
int vac[5];

func(vec,vac);

for(int i=0 ; i<5 ; i++)
{
cout<<vac<<endl;

}
return 0;
}


I had the impression that if you declare an int function you should have a return but the above code seems to work! Thank you very much jtbell. So what is the need for the pointers anyway?
 
  • #6
sketos said:
I had the impression that if you declare an int function you should have a return but the above code seems to work!

You've declared that the function is going to return an int, but don't actually try to return anything, so it returns garbage. But when you call the function, you don't use the returned value (you're not doing something like pt=func(vec,vac)), so the garbage doesn't matter. I'd make the function 'void' anyway, so other programmers reading your code don't wonder whether there's something wrong here.

So what is the need for the pointers anyway?


In code like this there's no need to use pointers explicitly. It's basically a matter of style. Personally, I prefer to avoid using pointers unless they're really necessary, because it's easy to make mistakes when using them. C++ provides ways to avoid using pointers in many situations where you would have to use them in C.
 
  • #7
A lot of software of lib codebase are still using pointers, especially those written years ago. Updating all of them is hard and maintaining them is nightmare. Learning so is good for you in later jobs. Pointers provide coders direct access to memory so it increases a little the program performance.
With pointers you can work with both references either to variable's address or value while references are only aliases to variables's values and values are simply valued objects.
Ex:
int x=1; // x is an integer of value 1
int *p=&x; //&p is the address of x and *p is the value of x
const int &r=x; //r is an alias of x
 
  • #8
ok i think i get it now! Thank you for your time although it was a silly question!
 
  • #9
No question about it, if you're going to do a lot of programming, you'll have to learn about pointers eventually. The only question is when. With C++, we preferred to put them off until students really needed them, and we could motivate them properly. In our case, this didn't happen until after the class I taught, and I was happy to leave it to the other prof to teach them about pointers. :D
 
  • #10
sketos, please put [ code ] and [ /code ] tags around your code (without the extra spaces).
sketos said:
OOOOooo i though that this was not possible...

int func(int vec[],int vac[])
{
for(int i=0 ; i<5 ; i++)
{
vac=vec+1;

}
).​
I doubt that the code above is what you really want. I didn't check, but if the compiler considers vec and vac to be array constants, it will generate error warnings.
Code:
int func(int vec[],int vac[])
{
  for(int j=0 ; j<5 ; j++)
    {
      vac[j] = vec[j]+1;
    }
}
1. The two array variables in the body of the loop need indexes. I have added them.
2. This function should return a value, meaning there should be a statement such as return <something>.
sketos said:
int main()
{

int vec[5]={1,1,1,1,1};
int vac[5];

func(vec,vac);

for(int i=0 ; i<5 ; i++)
{
cout<<vac<<endl;

}
return 0;
}
In the code above, func() is declared to return an int, but you are calling it as if it were a void function, a function that does not return a value. If you declare a function that returns some type, you should call it so that the return value is used in some way, such as by being on the right side of an assignment statement.
sketos said:
I had the impression that if you declare an int function you should have a return but the above code seems to work! Thank you very much jtbell. So what is the need for the pointers anyway?
Modern compilers will generate warnings if you have a function that is supposed to return a value but it doesn't actually have a return statement. They will also generate warnings if you call a function that's supposed to return something, but you call it in such a way that you discard the returned value.
 
Last edited:

What is an analogous to subroutine in C++?

An analogous to subroutine in C++ is a function. Functions in C++ are similar to subroutines in other programming languages, as they are blocks of code that can be called and executed multiple times throughout a program.

How do you create a subroutine in C++?

To create a subroutine in C++, you use the keyword "void" followed by the name of the subroutine and a set of parentheses. Within the parentheses, you can specify any parameters that the subroutine will take in. After the parentheses, you use curly brackets to define the code that will be executed when the subroutine is called.

What is the purpose of using subroutines in C++?

Subroutines in C++ serve several purposes. They allow for code reusability, as a subroutine can be called and executed multiple times throughout a program. They also help with organization and readability, as functions can be named and labeled to indicate their purpose within the program.

Can a subroutine in C++ return a value?

Yes, a subroutine in C++ can return a value. To do this, you use a data type other than "void" in the function declaration. Within the subroutine, you use the "return" keyword followed by the value that you want to return. The calling code can then use this returned value for further processing.

How do you call a subroutine in C++?

To call a subroutine in C++, you use the name of the subroutine followed by a set of parentheses. If the subroutine takes in parameters, you include their values within the parentheses. The code within the subroutine will then be executed, and any returned values can be used by the calling code.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
3
Views
727
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
5
Views
883
Back
Top