Flanker
Flanker

Reputation: 133

Fortran 90 function with no arguments?

I would like to know whether I MUST provide arguments for a function in Fortran 90? Can I have a function that takes in no arguments, like in Java, e.g. get(), for example?

Upvotes: 0

Views: 4066

Answers (1)

casey
casey

Reputation: 6915

Yes, this is possible. A function that takes no arguments is simply declared with no arguments, e.g.

integer function get_a_number()
   implicit none
   get_a_number = 42
end function get_a_number

which takes no arguments and simply returns the value 42 in default integer kind.

You can also have optional arguments, e.g.

function hello_string(name)
  implicit none
  character(len=150) :: hello_string
  character(len=*), optional :: name

  if (present(name)) then
     hello_string = "Hello "//trim(name)//"!"
  else
     hello_String = "Hello!"
  end if
end function

This function will return "Hello!" if called with no argument, and "Hello name!" if provided with an argument. This function can take 1 or 0 arguments. Note that this kind of function will require an explicit interface to work properly.

Upvotes: 2

Related Questions