FORTRAN Simplification of Function

I have the following code that counts the number
of consecuitive logicals from the first one.

Any way I can simplify this function?

 Function count_present              & 
   (                                 &
     p1, p2, p3, p4, p5, p6, p7, p8  &
   )                                 &
     Result (n)

   !!$ In.
   Logical, Intent (in) :: p1
   Logical, Intent (in), Optional :: p2, p3, p4, p5, p6, p7, p8 

   !!$ Out.
   Integer :: n

   n = 0

   If (Present (p8)) Then

     If      (p8) Then; n = 8
     Else If (p7) Then; n = 7
     Else If (p6) Then; n = 6
     Else If (p5) Then; n = 5
     Else If (p4) Then; n = 4
     Else If (p3) Then; n = 3
     Else If (p2) Then; n = 2
     Else If (p1) Then; n = 1
     End If

   Else If (Present (p7)) Then

     If      (p7) Then; n = 7
     Else If (p6) Then; n = 6
     Else If (p5) Then; n = 5
     Else If (p4) Then; n = 4
     Else If (p3) Then; n = 3
     Else If (p2) Then; n = 2
     Else If (p1) Then; n = 1
     End If

   Else If (Present (p6)) Then

     If      (p6) Then; n = 6
     Else If (p5) Then; n = 5
     Else If (p4) Then; n = 4
     Else If (p3) Then; n = 3
     Else If (p2) Then; n = 2
     Else If (p1) Then; n = 1
     End If

   Else If (Present (p5)) Then

     If      (p5) Then; n = 5
     Else If (p4) Then; n = 4
     Else If (p3) Then; n = 3
     Else If (p2) Then; n = 2
     Else If (p1) Then; n = 1
     End If

   Else If (Present (p4)) Then

     If      (p4) Then; n = 4
     Else If (p3) Then; n = 3
     Else If (p2) Then; n = 2
     Else If (p1) Then; n = 1
     End If

   Else If (Present (p3)) Then

     If      (p3) Then; n = 3
     Else If (p2) Then; n = 2
     Else If (p1) Then; n = 1
     End If

   Else If (Present (p2)) Then

     If      (p2) Then; n = 2
     Else If (p1) Then; n = 1
     End If

   Else

     If (p1) n = 1

   End If

 End Function count_present

Did you consider recursion?

No I didn't. Not sure how to approach it, since
you mentioned it. What did you have in mind?

My FORTRAN is not just a bit rusty, but has entirely corroded away.
Still, from staring at above function, I'd infer that the result is dependent on a) the respective parameter being present and b) the parameter being true.
Not sure what happens if you try to evaluate a non-present parameter or pass it on to another function. Couldn't you call a sub-function eight times to find out which parameter fulfills the condition and then pass back the return value?

If you don't mind to set n many times (which is a very small overhead)
and if I have understood your code, you can try

if (present (p1) .and. (p1)) n = 1
if (present (p2) .and. (p2)) n = 2
...
if (present (p8) .and. (p8)) n = 8

---------- Post updated at 03:05 PM ---------- Previous update was at 02:55 PM ----------

Alternatively try the following that is without any overhead:

 If (Present (p8) .and. (p8)) Then
   n = 8
 Else If (Present (p7) .and. (p7)) Then
   n = 7
 ...
 Else If (Present (p2) .and. (p2)) Then
   n = 2
 Else If (p1) Then
   n = 1
 End If