What Is the Point of Having Two Different Names For the Same Parameter

What is the point of having two different names for the same parameter?

inArray is external name which the caller of the function should use when passing parameters. arr is the internal name which the function implementer uses in the implementation to refer to the parameter. You don't have to supply external name.It makes it more readable. It is more like to make swift function names and parameters readable as Objective-C functions are.

Correct way to pass multiple values for same parameter name in GET request

Indeed, there is no defined standard. To support that information, have a look at wikipedia, in the Query String chapter. There is the following comment:

While there is no definitive standard, most web frameworks allow
multiple values to be associated with a single field.[3][4]

Furthermore, when you take a look at the RFC 3986, in section 3.4 Query, there is no definition for parameters with multiple values.

Most applications use the first option you have shown: http://server/action?id=a&id=b. To support that information, take a look at this Stackoverflow link, and this MSDN link regarding ASP.NET applications, which use the same standard for parameters with multiple values.

However, since you are developing the APIs, I suggest you to do what is the easiest for you, since the caller of the API will not have much trouble creating the query string.

Swift function, two names for one parameter

From Apple's documentation:

Sometimes it’s useful to name each parameter when you call a function, to indicate the purpose of each argument you pass to the function.

If you want users of your function to provide parameter names when they call your function, define an external parameter name for each parameter, in addition to the local parameter name. You write an external parameter name before the local parameter name it supports, separated by a space:

func someFunction(externalParameterName localParameterName: Int) {
// function body goes here, and can use localParameterName
// to refer to the argument value for that parameter
}

Shorthand External Parameter Names

If you want to provide an external parameter name for a function parameter, and the local parameter name is already an appropriate name to use, you do not need to write the same name twice for that parameter. Instead, write the name once, and prefix the name with a hash symbol (#). This tells Swift to use that name as both the local parameter name and the external parameter name.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

Why do the parameters in a called function have to have other names and not the same as the one calling it?

This is the result of what is known as "scope" in Computer Science.

In JavaScript scopes are defined by their enclosed "Execution Contexts" ECMA, which was designed similarly to a stack of cards.

Practically speaking, this means there are areas where names reference memory locations. Your coding example highlights a scenario where two different memory locations are referenced using the same name because they are in different areas.

In JavaScript, one way to create one of these areas for different name references is to use a function. Inside of the function, new names will take precedence over outside names.

So, when you have a situation such as:

for ( var x = 1; x <= 10; ++x ){
square(x);
}
function square(x){
return x * x;
}

The confusing part here is mostly that the same name for the variable was used in both places.

The x variable inside of the function is actually in its own name reference area. Inside of that area, x now refers only to the parameter value of x, and not to any other value.

From a more technical angle, there is an execution context currently executing the code inside of the for loop. It contains a memory environment which has an entry for x. When square(x) is called, it uses that value to pass to a new execution context for the function square. Inside of the execution context for square, there is also a memory environment. This memory environment contains an entry for the function's parameter, also named x. Note that the memory location is different since there are two different execution contexts, and two different memory environments.

So really, there doesn't need to be different names from a technical perspective. However, from a readability perspective it is hard to read and that makes the program harder to build later on.

As far as why this is done, it is because of memory management and execution efficiency. There is a mechanism called "garbage collection" which runs when it can (when there is free processor time) that will remove unused memory. This memory is determined to be unused when it goes out of scope (which is why the execution contexts are important). In addition, during lookup for variable values, having a small area to start with and then expanding out makes the lookup much faster. These are the two main reasons why scoping is used behind the scenes.

Can the names of parameters in function definition be same as the name of arguments passed to it in the function call in C++?

Yes, the names of the variables you pass in a function call can be the same as the names of the parameters in the function definition. The scope of the function parameters begins and ends in the function block, so the compiler can keep the two (or more) variables defined at different scopes separate, even when they have the same name.

You can call your function by passing variables with the same name (add(num1, num2)), different names (add(x, y)), or no names at all (add(3, 4)).

See the Function parameter scope section in the C++ Scope reference.

Is it good practice to use the same Variable names for both method call and method signature parameters?

I realize that it's a contrived example to demonstrate what you're asking, but your example does contain a naming problem which I'll point out:

int a; // <---- right here
int b; // <---- and here

int value = getValue(a,b); // <--- and a little here

private int getValue(int a, int b)
{
int value = a+b;
return value;
}

The problem isn't in whether or not the variable names match or don't match what they're called in the method. The problem is that the variable names aren't called anything meaningful. This is considerably more of an issue than what you're asking.

Let's re-factor your method to make the example slightly less contrived...

int a;
int b;

int value = GetSum(a,b);

private int GetSum(int firstValue, int secondValue)
{
return firstValue + secondValue;
}

The method is a bit cleaner now and more intuitively reflects its purpose. Now we re-ask the question... Should a and b be renamed to match the ones in the method?

Most likely not. The names in the method have been changed to indicate their context. The method is getting a sum of two values, the first one and the second one. So what is the context of a and b? Are they also known only as the first one and the second one? Or do they convey some other meaning that's not readily available? Something like:

int milesToFirstDestination;
int milesToSecondDestination;

or:

int heightOfPersonInInches;
int heightOfStepstoolInInches;

or any other example of two values which would need to be summed for some purpose. If we added that context to the variable names then we most certainly wouldn't want to add it to the method. The method should be as general-purpose as possible, performing a single task without any concern outside of that task.

In short, it's neither good nor bad practice, because it's not something to even consider. There may be times where, by coincidence alone, the names are the same. (This can often happen with private helper methods, for example.) But they're not the same as a result of a standard or practice to be followed, but rather as a result of coincidentally having the same meaning.

already defines a member called with the same parameter types c#

Any of Davide suggestions will work. Another options is to do just have one method that takes the ID and the Parameter Name like so:

public static List<int> Lista(int id,string paramName)
{
List<int> list = new List<int>();
using (FbConnection con = new FbConnection(M.Baza.connectionKomercijalno2018))
{
con.Open();
using (FbCommand cmd = new FbCommand("SELECT BRDOK FROM DOKUMENT WHERE MAGACINID = @MID ORDER BY DATUM ASC", con))
{
cmd.Parameters.AddWithValue(paramName, id);

FbDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
list.Add(Convert.ToInt32(dr[0]));
}
}
}
return list;
}

Since everything in both methods are the same and just which parameter name changes.

Can we define two functions with same name but different parameters?

I am confused when we need to define another function we can give it a different name.

The is the one of the most basic feature of C++: function overloading. In C you can't have two functions with the same name, at all. In C++, it's entirely possible as long as the function function signature is different, ie two functions having the same name but different set of parameters.

https://en.wikipedia.org/wiki/Function_overloading

Sending HTTP request with multiple parameters having same name

Although POST may be having multiple values for the same key, I'd be cautious using it, since some servers can't even properly handle that, which is probably why this isn't supported ... if you convert "duplicate" parameters to a list, the whole thing might start to choke, if a parameter comes in only once, and suddendly you wind up having a string or something ... but i guess you know what you're doing ...

I am sorry to say so, but what you want to do, is not possible in pure AS2 ... the only 2 classes available for HTTP are LoadVars and XML ... technically there's also loadVariables, but it will simply copy properties from the passed object into the request, which doesn't change your problem, since properties are unique ...

if you want to stick to AS2, you need an intermediary tier:

  1. a server to forward your calls. if you have access to the server, then you create a new endpoint for AS2 clients, which will decode the requests and pass them to the normal endpoint.
  2. use javascript. with flash.external::ExternalInterface you can call JavaScript code. You need to define a callback for when the operation is done, as well as a JavaScript function that you can call (there are other ways but this should suffice). Build the request string inside flash, pump it to JavaScript and let JavaScript send it to the server in a POST request and get the response back to flash through the callback.

up to you to decide which one is more work ...

side note: in AS3, you'd use flash.net::URLLoader with dataFormat set to flash.net::URLLoaderDataFormat.TEXT, and then again encode parameters to a string, and send them.



Related Topics



Leave a reply



Submit