What is a formal parameter, in Java?

2019-02-16 17:16发布

I'm currently working on java legacy code and I encounter with a class that represent a formal parameter but I don't know why is that. I read about C++ Formal Parameters, but it confused me because in C++ it is the same as the argument (I'm in doubt about this affirmation) and in my legacy code it is a class, that has only a private int member that store a number (with their set & get methods) but honestly, I didn't find the why of that declaration.

3条回答
SAY GOODBYE
2楼-- · 2019-02-16 17:43

Not disagreeing with Elliot Frisch at all, but I can say it more simply:

The variable w is a "formal parameter" in the following function definition:

void foobar(Widget w) {
    ...
}

The value returned by nextWidget(...) is the "actual parameter" when you write the following function call:

foobar(nextWidget(...));
查看更多
再贱就再见
3楼-- · 2019-02-16 17:57

In Java and in C++ the formal parameter is specified in the signature of the method:

public void callIt(String a)

callIt has a single formal parameter that is a String. At run-time we talk about actual parameters (or arguments), the :

callIt("Hello, World");

"Hello, World" String is an actual parameter, String a is a formal parameter.

From the Wikipedia entry for parameter:

The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function definition,

and:

argument (sometimes called actual parameter) refers to the actual input passed.

查看更多
做自己的国王
4楼-- · 2019-02-16 18:09

Well, coming to Java or any language these definitions always hold true:

  • caller — the function is invoking the function call.

  • callee — the function that is invoked by the caller.

An argument term technically in programming refers to the data that is passed by caller to the callee.

And a parameter term technically refers to the type of data being passed more specifically to an identifier which identifies the type of data. So a parameter more or less refers to the identifier that identifies a particular type. Coming further, a formal parameter is the identifier used in the callee's method signature.

And an actual parameter is the identifier used by the caller when invoking the call.

Since you know that we can even pass the arguments (i.e. data) in the call to the callee directly, actual parameters are not compulsory and hence directly data can be passed whereas formal parameters are always compulsory.

查看更多
登录 后发表回答