Thread: subroutine in C

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    24

    subroutine in C

    Hi pals, I tried to implement my code from FORTRAN to C, but I found a problem, how can I call a subroutine in C, I saw that I could use the command VOID but Im not sure, could you explain me this please
    thx

  2. #2
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    It has been long since I have written fortran, and I never was really advanced in it...
    But I guess subroutines were close to functions in C (or close to using 'goto' statement - which is not good way in C - it makes larger codes a mess).

    But I doubt no one will give you detailed description about functions in C. I suggest you'll google some really basic C tutorial, and experiment with creating functions. You should also pay attention to "pass by reference" Vs. "pass by value" issues in C.

    You can then come back, and ask a bit more specific questions if needed.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A fortran subroutine is, in C, a function with no return value, which is a "void" function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    So I can use VOID in C as I use subroutine in Fortran right

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, but C is case-sensitive, so "VOID" is not the same as "void".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    well some problem trying to implement my code from fortran to C with void somebody could help me
    Code:
    	void contour (float f[][],float x[],float y[],int jmax, int imax)
    	{
    	for(j=1;j<=jmax;j++)
    	{
    		f[1][j]=ui*x[1];
    		f[imax][j]=ui*x[imax];
    	}
    	for(i=1;i<=imax;i++)
    	{
    		f[i][jmax]=ui*x[i];
    		if((i>=il) && (i<=it))
    		f[i][1]=f[i][2]-0.1*(y[2]-y[1])*(1.0-2*x[i]);
    		else
    		f[i][1]=f[i][2];
    	}// end contour ()

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so in C, you have to SPECIFY the size of all but the right-most array size - or use a single-dimension array.

    You also must start array indices at 0, not 1. So array[imax] will not work, probably.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    even in void as in the main I need to specify the size of the array?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vikingcarioca View Post
    even in void as in the main I need to specify the size of the array?
    Yes.

    The way C works is that every array [no matter how many dimensions] is implemented as a simple one-dimension array. The compiler will then calculate the respective postion by multiplying the first index times it's size + second index.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vikingcarioca View Post
    even in void as in the main I need to specify the size of the array?
    'void' in C is used a couple of ways:

    1 - To indicate that the function will not return a value.
    Code:
    void fun1( int x )
    {
       printf( "x + 1 is %d\n", x + 1 );
    }
    2 - To indicate that a function takes no arguments (is passed nothing).
    Code:
    int fun2( void )
    {
        printf( "This function takes no parameters, but returns something!\n" );
        return 0;
    }
    3 - To declare a "universal" datatype.
    Code:
    void *vptr;
    int x;
    char c;
    vptr = &x;
    ...
    vptr = &c;
    You can assign any type to a void pointer, and it will keep tabs on it for you. But to get back what you're pointing at, you need to reference that type implicitly.

    You can also use it as a cast to disregard return values, but you usually don't need to do that.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by quzah View Post
    2 - To indicate that a function takes no arguments (is passed nothing).
    Code:
    int fun2( void )
    {
        printf( "This function takes no parameters, but returns something!\n" );
        return 0;
    }
    Only in prototypes, though.
    The compiler couldn't care less about the actual definition.
    Last edited by Elysia; 04-03-2009 at 03:55 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Elysia View Post
    Only in prototypes, though.
    The compiler could care less about the actual definition.
    The phrase is "couldn't care less". Could care less means that it does care, and it could care less than it currently does, but doesn't for some reason...

    For the record, I don't care as much what the compiler thinks, as much as I care what the language defines. Anyway, I'm clear on what it is you're trying to say. If you're saying that when you call a function which takes no parameters, you don't have to specify void:
    Code:
    void fun( void )
    {
       ...
    }
    
    ...
    
    fun( );
    Then I agree. When you call a function which takes no arguments, you do not have to provide arguments. That's sort of the whole point.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by quzah View Post
    For the record, I don't care as much what the compiler thinks, as much as I care what the language defines. Anyway, I'm clear on what it is you're trying to say. If you're saying that when you call a function which takes no parameters, you don't have to specify void:
    Code:
    void fun( void )
    {
       ...
    }
    
    ...
    
    fun( );
    Then I agree. When you call a function which takes no arguments, you do not have to provide arguments. That's sort of the whole point.


    Quzah.
    The point is / was that this...
    Code:
    void fun(void);
    void fun()
    {
        // blah
    }
    The "void" must be in the prototype to tell the compiler that function takes no arguments. Sure, if you only provide a definition, then you must use void in the parameter list.
    This trickery allows this sort of stuff to compile:
    Code:
    void fun(); // Unspecified number of args
    void fun(int arg1, int arg2)
    {
        // blah
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Void specifies that there are no arguments. Empty specifies that there are an unspecified number of arguments. There's a difference. And since I wasn't talking about having an unspecified number of arguments, it's not relevant to the discussion of void really. Again, void is specifically saying that there will be no arguments.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I merely want to make it clear that this works:
    Code:
    void foo(void);
    void foo()
    {
    
    }
    
    int main()
    {
    	foo();
    }
    This works:
    Code:
    void foo(void);
    void foo(void)
    {
    
    }
    
    int main()
    {
    	foo();
    }
    And this works:
    Code:
    void foo(void)
    {
    
    }
    
    int main()
    {
    	foo();
    }
    In all of them, foo takes no arguments and the compiler will complain if you try.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling Java Subroutine
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 10-14-2008, 11:52 AM
  2. AIX passwdpolicy() subroutine
    By syndex in forum C Programming
    Replies: 1
    Last Post: 10-06-2008, 06:04 PM
  3. Assembler Language Subroutine 2
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 03-30-2008, 04:48 PM
  4. problem with subroutine
    By pankleks in forum C Programming
    Replies: 2
    Last Post: 11-24-2005, 02:57 AM
  5. fgets in a function subroutine
    By wbeasl in forum C Programming
    Replies: 1
    Last Post: 12-03-2003, 03:51 AM