Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Anologous to subroutine in C++

  1. Oct 29, 2014 #1
    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???
     
  2. jcsd
  3. Oct 29, 2014 #2

    jtbell

    User Avatar

    Staff: Mentor

    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 (Text):

    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 (Text):

    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 (Text):

    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!
     
  4. Oct 29, 2014 #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...
     
  5. Oct 29, 2014 #4

    jtbell

    User Avatar

    Staff: Mentor

    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().
     
  6. Oct 29, 2014 #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???
     
  7. Oct 29, 2014 #6

    jtbell

    User Avatar

    Staff: Mentor

    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.



    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.
     
  8. Oct 29, 2014 #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
     
  9. Oct 29, 2014 #8
    ok i think i get it now!! Thank you for your time although it was a silly question!!
     
  10. Oct 29, 2014 #9

    jtbell

    User Avatar

    Staff: Mentor

    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
     
  11. Oct 29, 2014 #10

    Mark44

    Staff: Mentor

    sketos, please put [ code ] and [ /code ] tags around your code (without the extra spaces).
    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 (Text):

    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>.
    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.
    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: Oct 29, 2014
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Anologous to subroutine in C++
Loading...