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 907913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:37:49+00:00 2026-05-15T16:37:49+00:00

I’m trying to write a program with a function which returns a matrix with

  • 0

I’m trying to write a program with a function which returns a matrix with a random number on the diagonal, 1s on the sub-diagonals and 0s everywhere else. So, I wrote this function:

    real function am1d
    do i=1,L
        do j=1,L
            if (i.eq.j) then
                am1d(i,j)=rand()*w-w/2.
            elseif ((i-j.eq.1) .or. (j-i.eq.1)) then
                am1d(i,j)=1
            else am1d(i,j)=0
        enddo
    enddo
end function am1d

And tried to call it from here (in the same source file, just above the function)

    program make3d
    integer, parameter :: L = 20
    real, parameter    :: w = 0.5
    real :: x


    !x=rand(1234)   ! seed random manually
    x=rand(itime)   ! seed random from current local time
    print *,am1d()(:)


end program make3d

But trying to compile this throws the error:

   $ f95 make3d.f
make3d.f:18.21:

       print *,am1d()(:)                                                
                     1
Error: Syntax error in PRINT statement at (1)
make3d.f:7.72:

      program make3d                                                    
                                                                        1
make3d.f:24.72:

      real function am1d                                                
                                                                        2
Error: Two main PROGRAMs at (1) and (2)

What does that mean? I didn’t think a function could ever be a program? I’ve had small logical functions underneath the endprogram statement before, without any trouble.

  • 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-15T16:37:50+00:00Added an answer on May 15, 2026 at 4:37 pm

    If you want function am1d to return an array, you need to declare it as such. There were several other syntax errors. The compiler got confused …. Did you forget the “contains” statement … without that or a module, the compiler wouldn’t “know” what to do with another procedure and might try to interpret it as second main program.

    Try:

    program make3d
        integer, parameter :: L = 20
        real, parameter    :: w = 0.5
        real :: x
    
        x=rand(1234)   ! seed random manually
        !x=rand(itime)   ! seed random from current local time
        write (*, *) am1d()
    
    stop
    
    contains
    
    function am1d ()
    real, dimension (L,L) :: am1d
        integer i, j
        do i=1,L
            do j=1,L
                if (i.eq.j) then
                    am1d(i,j)=rand()*w-w/2.
                elseif ((i-j.eq.1) .or. (j-i.eq.1)) then
                    am1d(i,j)=1
                else 
                   am1d(i,j)=0
                end if
            enddo
        enddo
    end function am1d
    
    end program make3d
    

    or place the routine in a module, which I think provides a clearer and cleaner interface (makes variable communication explicit) — then you will need to “use” the module in the main program and to pass w and L as arguments:

    module a_mod
    
    contains
    
    function am1d (w,L)
    real, intent (in) :: w
    integer, intent (in) :: L
    real, dimension (L,L) :: am1d
    
    .....
    end function am1d
    
    
    end module a_mod
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.