Subroutine in subroutine argument problem

In summary, the OP is having a problem with the argument of a subroutine which he did not write himself. He is supposed to write his own subroutine named funcs(a,b,c). Then this subroutine that he got has funcs as one of it's arguments subr(a,b,c,funcs,..) and it says in the description of the subroutine that he must provide a subroutine funcs to make subr(a,b,c,funcs,..) work. However, when he does not declare funcs, he gets an error that funcs is not explicitly declared, and when he does declare funcs, he gets an
  • #1
snipertje
11
0
I'm having a problem with the argument of a subroutine which I didn't write myself, but should be working as intended.

I'm supposed to write my own subroutine named funcs(a,b,c). Then this subroutine that I got has funcs as one of it's arguments subr(a,b,c,funcs,..) and it says in the description of the subroutine that I must provide a subroutine funcs to make subr(a,b,c,funcs,..) work.

The problem is when I don't declare funcs (Real/integer...) I get an error that funcs is not explicitly declared.. When I do declare funcs, i get an error that i 'attempt to call a non-subroutine'.

Is there a way to pass my subroutine to the other subroutine?
 
Technology news on Phys.org
  • #2
I did my best to read your mind to find out what programming language you are using, but the distance is too large. Sorry, I can't help.
 
  • #3
Based on the OP's other posts, I believe he is coding in Fortran. It apparently is possible to pass subroutine names as actual arguments to subroutines, according to this link: http://www.fortran.com/F77_std/rjcnf0001-sh-15.html.
See 15.5.2.2 Actual Arguments for an External Function, and 15.6.2.3 Actual Arguments for a Subroutine on the linked page.

snipertje,
Be sure you understand the distinction between a FUNCTION subprogram and a SUBROUTINE subprogram. You are calling 'funcs' a subroutine, but from its name, I suspect that is actually a function. There are differences between these two types of subprogram, both in the way that they are called, and whether they return a value.
 
  • #4
Assuming this really is Fortran...

snipertje said:
The problem is when I don't declare funcs (Real/integer...) I get an error that funcs is not explicitly declared.. When I do declare funcs, i get an error that i 'attempt to call a non-subroutine'.

In addition to declaring the function name (in the main program) as real/integer/whatever, I think you must also declare it as "external" so that the compiler doesn't assume that it's a variable, e.g.

REAL FUNCS
EXTERNAL FUNCS

I remember having to do this when I used the "function name as subroutine argument" technique a long time ago. As I recall, the reason you don't have to do this when you call FUNCS in the normal way in your program, is that the compiler can determine from the syntax that it's a function. That is, if you have something like FUNCS(A,B,C) and you haven't declared FUNCS as an array, then it must be a function call. However, if you have CALL SUBR(A,B,C,FUNCS), the compiler assumes FUNCS is a variable unless you tell it otherwise via the EXTERNAL declaration.

(It beats me how I remember all this stuff. It's been about fifteen years since I did any Fortran programming!)
 
Last edited:
  • #5
jtbell said:
Assuming this really is Fortran...



In addition to declaring the function name (in the main program) as real/integer/whatever, I think you must also declare it as "external" so that the compiler doesn't assume that it's a variable, e.g.

REAL FUNCS
EXTERNAL FUNCS

I remember having to do this when I used the "function name as subroutine argument" technique a long time ago. As I recall, the reason you don't have to do this when you call FUNCS in the normal way in your program, is that the compiler can determine from the syntax that it's a function. That is, if you have something like FUNCS(A,B,C) and you haven't declared FUNCS as an array, then it must be a function call. However, if you have CALL SUBR(A,B,C,FUNCS), the compiler assumes FUNCS is a variable unless you tell it otherwise via the EXTERNAL declaration.

(It beats me how I remember all this stuff. It's been about fifteen years since I did any Fortran programming!)
Wow, this is really helpful (if it works anyway ;)). It's indeed confusing that I have to name a subroutine funcs, but I'm positive it has to be a subroutine. I will try this out.
 
  • #6
If it really is a SUBROUTINE subprogram and not a FUNCTION subprogram, then you shouldn't have to declare a type (REAL, INTEGER, etc.), because SUBROUTINEs don't have a type, but you still need to declare it EXTERNAL. Declaring it EXTERNAL will probably remove the "not explicity declared" error message, by itself.
 
  • #7
Ok, cheers
 

FAQ: Subroutine in subroutine argument problem

What is a subroutine in subroutine argument problem?

A subroutine in subroutine argument problem refers to a common issue in computer programming where a subroutine (a set of instructions that perform a specific task) is called within another subroutine as an argument. This can lead to unexpected behavior or errors if not properly handled.

What causes a subroutine in subroutine argument problem?

A subroutine in subroutine argument problem is typically caused by the incorrect use of arguments in a subroutine call. This can include passing the wrong number of arguments, passing arguments in the wrong order, or passing arguments with incorrect data types.

How can a subroutine in subroutine argument problem be fixed?

To fix a subroutine in subroutine argument problem, the subroutine call must be carefully reviewed to ensure that the correct number and type of arguments are being passed in the correct order. Additionally, the subroutine itself may need to be modified to handle unexpected arguments or provide more specific instructions for proper usage.

What are some common errors associated with a subroutine in subroutine argument problem?

Some common errors that may occur due to a subroutine in subroutine argument problem include "missing argument" errors, "argument type mismatch" errors, or "too many arguments" errors. These errors can often be identified by carefully reviewing the subroutine call and the expected arguments.

How can a subroutine in subroutine argument problem be prevented?

To prevent a subroutine in subroutine argument problem, it is important for programmers to carefully plan and test their code before implementation. This includes thoroughly reviewing subroutine calls and ensuring that all arguments are properly defined and used. Additionally, using proper coding practices and commenting can help prevent these types of issues from occurring.

Similar threads

Replies
9
Views
1K
Replies
59
Views
10K
Replies
5
Views
12K
Replies
14
Views
4K
Replies
3
Views
2K
Back
Top