Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion passing a function as an argument
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Tobias Burnus  
View profile  
 More options Feb 7 2012, 1:30 pm
From: Tobias Burnus <bur...@net-b.de>
Date: Tue, 7 Feb 2012 10:30:49 -0800 (PST)
Local: Tues, Feb 7 2012 1:30 pm
Subject: Re: passing a function as an argument
On Feb 2, 12:36 am, Ken <ken.al...@sbcglobal.net> wrote:

> I've seen sample code where a function name is passed as a argument to
> a function or subroutine and then declared as an external function.
> What is actually being passed?  An address?

Yes, one passes the address of the procedure. By contrast, with
procedure pointers, you pass the address of the pointer variable,
which contains the address of the procedure. Example usage:

module m
  subroutine p()
     print *, 'Hello'
  end subroutine p
  subroutine foo (f, g)
      procedure() :: f
      procedure(), pointer :: g
      call f()
      call g()
   end subroutine foo
end module m

program test
  use m
  procedure(), pointer :: proc_ptr
  proc_ptr => p
  call foo (p, proc_ptr)
end program test

Here, "p" is the passed procedure, "proc_ptr" is a pointer variable,
which contains the address of "p".

Note that Equivalent to using
  procedure() :: f
the following:
  external f

A better choise is to use:
  procedure(p) :: f
  procedure(p), pointer :: g
Then the interface is known to the compiler. The first line is
equivalent to
  interface
    subroutine f()
    end subroutine f
  end interface

The other line is equivalent to the following, but I think not all
compilers support it:
  interface
    subroutine g()
    end subroutine g
  end interface
  pointer :: g

The procedure statement - and procedure pointers - are Fortran 2003
features. By now, most compilers have implemented it. I know that
gfortran supports it (full support since 4.5). Also g95 supports it
since years, though it does not seem to support the last version.

Tobias


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.