fortran中数组作为函数参数传递

 

001、

root@ubuntu26:/home/test01/fortran# cat test01.f90         ## 测试程序
program test01_fortran
    implicit none

    integer,allocatable :: ay1(:)
    integer :: i,a
    real :: result
    write(*, "(A)", advance="no") "input a: "; read *, a
    allocate(ay1(a))

    do i = 1, a
        write(*, "(A, I0, A)", advance="no") "input ay1(", i, "):"; read *, ay1(i)
    end do
    result = array_mean(ay1)                                  !调用数组参数
    print *, "result = ", result
    deallocate(ay1)

contains
    real function array_mean(x)                                 ! 定义函数
        implicit none

        integer,intent(in) :: x(:)                               !声明数组变量
        array_mean = real(sum(x))/size(x)

    end function array_mean
end program test01_fortran
root@ubuntu26:/home/test01/fortran# gfortran test01.f90 -Wall -O2 -o kkk    ## 编译
root@ubuntu26:/home/test01/fortran# ls
kkk  test01.f90
root@ubuntu26:/home/test01/fortran# ./kkk
input a: 3
input ay1(1):10
input ay1(2):50
input ay1(3):30
 result =    30.0000000

 

image

 。

 

posted @ 2026-11-09 09:30  小鲨鱼2018  阅读(3)  评论(0)    收藏  举报