What Is the Equivalent of Subroutine in C++?

  • Context: C/C++ 
  • Thread starter Thread starter sketos
  • Start date Start date
  • Tags Tags
    C++ Subroutine
Click For Summary

Discussion Overview

The discussion revolves around finding an equivalent to Fortran subroutines in C++, focusing on function definitions, usage of pointers, and array manipulation. Participants explore different ways to implement functionality similar to subroutines in C++ and clarify the implications of using pointers versus array parameters.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in finding a C++ equivalent to Fortran subroutines, sharing an example using pointers.
  • Another participant suggests that a function can be declared as 'void' to avoid returning a value, which aligns with the behavior of Fortran subroutines.
  • It is proposed that passing array names directly to functions simplifies the syntax, eliminating the need for explicit address-of operators.
  • A participant questions how to access modified array values in the main program if the function is void, leading to a discussion about variable naming and scope.
  • Concerns are raised about the necessity of pointers, with some participants advocating for their use for performance reasons, while others prefer avoiding them for simplicity.
  • One participant notes that modern compilers may issue warnings for functions declared to return a value but not actually returning anything, emphasizing the importance of proper function declarations.

Areas of Agreement / Disagreement

Participants generally agree on the basic functionality of C++ functions and their relation to Fortran subroutines, but there are differing opinions on the necessity and usage of pointers versus array parameters. The discussion remains unresolved regarding the best practices for function declarations and pointer usage.

Contextual Notes

Some participants highlight potential issues with array handling and function return types, indicating that there may be misunderstandings about how C++ treats arrays and pointers. There are also mentions of compiler behavior that could affect how code is interpreted.

Who May Find This Useful

This discussion may be useful for programmers transitioning from Fortran to C++, particularly those interested in understanding function definitions, array manipulation, and the role of pointers in C++ programming.

sketos
Messages
55
Reaction score
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++)
{
count<<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
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   Reactions: sketos
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...
 
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().
 
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++)
{
count<<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?
 
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.
 
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
 
ok i think i get it now! Thank you for your time although it was a silly question!
 
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:

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K