Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:29:36+00:00 2026-05-25T01:29:36+00:00

In a subroutine or function an input variable can be defined with intent(in) and

  • 0

In a subroutine or function an input variable can be defined with intent(in) and the compiler assures that within the subroutine the variable can not be altered. As soon as the variable is passed (by reference) to another subroutine this subroutine is able to alter the variable without compiler warning.

This was tested with gfortran with the code:

program Test
    integer i
    i = 21 ! half the truth
    call test(i)
    write (*,*) "21 expected, but is 42: ", i
end program

subroutine test(i)
    integer, intent(in) :: i
    call doSomethingNasty(i)
end subroutine

subroutine doSomethingNasty(i)
    integer :: i
    i = 42 ! set the full truth ;-)
end subroutine

My questions are:

  1. Is this the normal behaviour for all compilers?
  2. Is there a way to force the compilers to assure that the variable is really constant and that alterations would be presented as compiler errors? I mean something like the const keyword in C/C++ which is also checked against the called functions which also need to assure that the constant is treated accordingly and that no reference is escaping.
  3. I found the possibility to pass the variable to the subroutine by “value” via passing it trough an expression like test((i)). For numeric variables, this is understandable and ok, but this seems to work with gfortran for arrays, derived types and pointers, too. Does this work with other compilers, too? Is it a safe way to protect my local variables?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:29:37+00:00Added an answer on May 25, 2026 at 1:29 am

    With sufficient compiler options gfortran generates a warning for your example, that an implicit interface is used.

    If you make the interface explicit by placing the subroutines into a module, and use intents for all arguments, gfortran will catch the problem:

    module mysubs
    
    contains
    
    subroutine test(i)
        integer, intent(in) :: i
        call doSomethingNasty(i)
    end subroutine
    
    subroutine doSomethingNasty(i)
        integer, intent (inout) :: i
        i = 42 ! set the full truth ;-)
    end subroutine
    
    
    end module mysubs
    
    
    program Test_intent_in
    
    use mysubs
    
        integer i
        i = 21 ! half the truth
        call test(i)
        write (*,*) "21 expected, but is 42: ", i
    
    end program Test_intent_in
    

    gfortran gives error message:

    call doSomethingNasty(i)
                              1
    Error: Procedure argument at (1) is INTENT(IN) while interface specifies INTENT(INOUT)
    

    When pass the argument “(i)” you are passing an expression rather than a variable. The expression is not definable and thus should not be used as an actual argument for an “out” or “inout” dummy argument.

    Another approach for argument “safety”: you can also use the “value” attribute in the declaration of a dummy argument to essentially make a local copy of the argument and guarantee that the actual argument won’t be altered.

    Edit:
    As kemiisto pointed out, “contains” also makes the interface known. I don’t like “contains” because the variable scoping … all variables of the parent program are visible. Try this test code out:

    PROGRAM contains_tst
    
      INTEGER :: i, m
    
      i = 21
      m = 22
      CALL test(m)
    
      CONTAINS
    
        SUBROUTINE test(j)
          INTEGER, INTENT(IN) :: j
          write (*, *) i, j
        END SUBROUTINE test
    
    END PROGRAM contains_tst
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a subroutine that takes a filehandle as an argument. How do I
I often have a subroutine in Perl that fills an array with some information.
I can create function pointers in Fortran 90, with code like real, external ::
I have the following FORTRAN SUBROUTINE SETPATHS(INPUT) !DEC$ ATTRIBUTES DLLEXPORT::SetPaths CHARACTER*20 INPUT CHARACTER*20 DIRECTORY
In a simple aspx page I can have a custom JavaScript function postback as
mysub gets a subroutine reference as its first argument. Can I simply call mysub(sub{some
Right now I can name the subroutine printargs as follows to get the dump.
I’m trying to figure out how to iterate over an array of subroutine refs.
I'm sure most of you know that a nested loop has O(n^2) complexity if
How can I check if a Perl module is part of the core -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.