fortran - Check values of local variables in subroutines from outside -


how check values in fortran in matlab? example in little program under, why show c=0 in main when c=36 in subroutine testing? how make c=36 in main program?

can call on value c in sort of way? understand in main program variable c either undefined or has value 0, there way save value of c in subroutine can use again in other subroutines, without calculating again?

when program quite large handy check values go.

program main  use test  implicit none  integer :: a,b,c  call testing(a,b)  write(*,*)'test of c in main program',c   end program main    module test  implicit none   contains   subroutine testing(a,b)  integer :: a,b,c  a=2 b=3  c=(a*b)**a  write(*,*)'value of c in subroutine',c  end subroutine testing  end module test 

it important learn scope in programming languages.

every name (identifier) has limited range of validity.

if declare variable inside subroutine

subroutine s   integer :: end subroutine 

than i valid in subroutine.

if declare variable in module

module m   integer :: contains   subroutines , functions end module 

then i valid inside subroutines , functions , in program units use module.

however, not mean should declare variable in module , share it. more or less global variable. reserved cases, necessary, not getting results out of subprograms.

if subroutine computes , want result of computation have 2 possibilities:

1. pass additional argument, defined subroutine

subroutine testing(a, b, c)     integer, intent(in) :: a, b     integer, intent(out) :: c      c = (a*b) ** end subroutine 

you call as

call testing(2, 3, c) print *, "result is:" c 

2. make function

function testing(a, b) result(c)     integer, intent(in) :: a, b     integer :: c      c = (a*b) ** end function 

and can use directly

c = testing(2, 3) print *, "result is:", c 

or just

print *, "result is:", testing(2, 3) 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -