EzDevInfo.com

arguments interview questions

Top arguments frequently asked interview questions

JavaScript variable number of arguments to function

Is there a way to allow "unlimited" vars for a function in JavaScript?

Example:

load(var1, var2, var3, var4, var5, etc...)
load(var1)

Source: (StackOverflow)

How do I find the number of arguments passed to a Bash script?

How do I find the number of arguments passed to a Bash script?

This is what I have currently:

#!/bin/bash
i=0
for var in "$@"
do
  i=i+1
done

Are there other (better) ways of doing this?


Source: (StackOverflow)

Advertisements

Reference a method parameter in javadoc

Is there a way to reference one of the method parameters from the method documentation body? Something like:

/**
 * When {@paramref a} is null, we rely on b for the discombobulation.
 *
 * @param a this is one of the parameters
 * @param b another param
 */
void foo(String a, int b)
{...}

Source: (StackOverflow)

Arguments or parameters? [duplicate]

This question already has an answer here:

I often find myself confused with how the terms 'arguments' and 'parameters' are used. They seem to be used interchangeably in the programming world.

What's the correct convention for their use?


Source: (StackOverflow)

How do I expand a tuple into variadic template function's arguments?

Consider the case of a templated function with variadic template arguments:

template<typename Tret, typename... T> Tret func(const T&... t);

Now, I have a tuple t of values. How do I call func() using the tuple values as arguments? I've read about the bind() function object, with call() function, and also the apply() function in different some now-obsolete documents. The GNU GCC 4.4 implementation seems to have a call() function in the bind() class, but there is very little documentation on the subject.

Some people suggest hand-written recursive hacks, but the true value of variadic template arguments is to be able to use them in cases like above.

Does anyone have a solution to is, or hint on where to read about it?


Source: (StackOverflow)

Looking for a Command Line Argument Parser for .NET [closed]

I'm looking for a command line argument parser, such as "Command line parser" from http://www.sellsbrothers.com/tools/Genghis/ .

Features I'm looking for:

  • Auto-generation of usage
  • Should able to check required and optional parameters
  • Parameters should support IEnumerable with separator support
  • Should support flag parameters
  • Would be nice to support combining parameters such as "/fx" == "/f /x"
  • Would be nice to not force for a space after a parameter such as "/ftest.txt" == "/f test.txt"

P.S : "Command line parser" is quite good, I really like the design of it but there is no documentation, no new updates and I couldn't figure out to do certain stuff such as how to check for required parameters.


Source: (StackOverflow)

How to Parse Command Line Arguments in C++? [duplicate]

Possible Duplicate:
What parameter parser libraries are there for C++?

What is the best way of parsing command-line arguments in C++ if the program is specified to be run like this:

prog [-abc] [input [output]]

Is there a library in STL to do this?


Related:


Source: (StackOverflow)

How best to determine if an argument is not sent to the JavaScript function

I have now seen 2 methods for determining if an argument has been passed to a JavaScript function. I'm wondering if one method is better than the other or if one is just bad to use?

 function Test(argument1, argument2) {
      if (Test.arguments.length == 1) argument2 = 'blah';

      alert(argument2);
 }

 Test('test');

Or

 function Test(argument1, argument2) {
      argument2 = argument2 || 'blah';

      alert(argument2);
 }

 Test('test');

As far as I can tell, they both result in the same thing, but I've only used the first one before in production.

Another Option as mentioned by Tom:

function Test(argument1, argument2) {
    if(argument2 === null) {
        argument2 = 'blah';
    }

    alert(argument2);
}

As per Juan's comment, it would be better to change Tom's suggestion to:

function Test(argument1, argument2) {
    if(argument2 === undefined) {
        argument2 = 'blah';
    }

    alert(argument2);
}

Source: (StackOverflow)

What is the difference between _tmain() and main() in C++?

If I run my C++ application with the following main() method everything is OK:

int main(int argc, char *argv[]) 
{
   cout << "There are " << argc << " arguments:" << endl;

   // Loop through each argument and print its number and value
   for (int i=0; i<argc; i++)
      cout << i << " " << argv[i] << endl;

   return 0;
}

I get what I expect and my arguments are printed out.

However, if I use _tmain:

int _tmain(int argc, char *argv[]) 
{
   cout << "There are " << argc << " arguments:" << endl;

   // Loop through each argument and print its number and value
   for (int i=0; i<argc; i++)
      cout << i << " " << argv[i] << endl;

   return 0;
}

It just displays the first character of each argument.

What is the difference causing this?


Source: (StackOverflow)

Which exception should I raise on bad/illegal argument combinations in Python?

I was wondering about the best practices for indicating invalid argument combinations in Python. I've come across a few situations where you have a function like so:

def import_to_orm(name, save=False, recurse=False):
    """
    :param name: Name of some external entity to import.
    :param save: Save the ORM object before returning.
    :param recurse: Attempt to import associated objects as well. Because you
        need the original object to have a key to relate to, save must be
        `True` for recurse to be `True`.
    :raise BadValueError: If `recurse and not save`.
    :return: The ORM object.
    """
    pass

The only annoyance with this is that every package has its own, usually slightly differing BadValueError. I know that in Java there exists java.lang.IllegalArgumentException -- is it well understood that everybody will be creating their own BadValueErrors in Python or is there another, preferred method?


Source: (StackOverflow)

How do I pass a unique_ptr argument to a constructor or a function?

I'm new to move semantics in C++11 and I don't know very well how to handle unique_ptr parameters in constructors or functions. Consider this class referencing itself:

#include <memory>

class Base
{
  public:

    typedef unique_ptr<Base> UPtr;

    Base(){}
    Base(Base::UPtr n):next(std::move(n)){}

    virtual ~Base(){}

    void setNext(Base::UPtr n)
    {
      next = std::move(n);
    }

  protected :

    Base::UPtr next;

};

Is this how I should write functions taking unique_ptr arguments?

And do I need to use std::move in the calling code?

Base::UPtr b1;
Base::UPtr b2(new Base());

b1->setNext(b2); //should I write b1->setNext(std::move(b2)); instead?

Source: (StackOverflow)

jQuery pass more parameters into callback

Is there a way to pass more data into a callback function in jQuery?

I have two functions and I want the callback to the $.post, for example, to pass in both the resulting data of the AJAX call, as well as a few custom arguments

function clicked() {
    var myDiv = $("#my-div");
    // ERROR: Says data not defined
    $.post("someurl.php",someData,doSomething(data, myDiv),"json"); 
    // ERROR: Would pass in myDiv as curData (wrong)
    $.post("someurl.php",someData,doSomething(data, myDiv),"json"); 
}

function doSomething(curData, curDiv) {

}

I want to be able to pass in my own parameters to a callback, as well as the result returned from the AJAX call.

Thanks!


Source: (StackOverflow)

C# Passing Function as Argument

I've written a function in C# that does a numerical differentiation. It looks like this:

public double Diff(double x)
{
    double h = 0.0000001;

    return (Function(x + h) - Function(x)) / h;
}

I would like to be able to pass in any function, as in:

public double Diff(double x, function f)
{
    double h = 0.0000001;

    return (f(x + h) - f(x)) / h;
}

I think this is possible with delegates (maybe?) but I'm not sure how to use them.

Any help would be greatly appreciated.

Thanks, Ash


Source: (StackOverflow)

Default Arguments in Matlab

Is it possible to have default arguments in Matlab? For instance, here:

function wave(a,b,n,k,T,f,flag,fTrue=inline('0'))

I would like to have the true solution be an optional argument to the wave function. If it is possible, can anyone demonstrate the proper way to do this? Currently, I am trying what I posted above and I get:

??? Error: File: wave.m Line: 1 Column: 37
The expression to the left of the equals sign is not a valid target for an assignment.

Thanks!


Source: (StackOverflow)

What's the difference between an argument and a parameter?

When verbally talking about methods, I'm never sure whether to use the word argument or parameter or something else. Either way the other people know what I mean, but what's correct, and what's the history of the terms?

I'm a C# programmer, but I also wonder whether people use different terms in different languages.

For the record I'm self-taught without a background in Computer Science. (Please don't tell me to read Code Complete because I'm asking this for the benefit of other people who don't already have a copy of Steve McConnell's marvellous book.)

Summary

The general consensus seems to be that it's OK to use these terms interchangeably in a team environment. Except perhaps when you're defining the precise terminology; then you can also use "formal argument/parameter" and "actual argument/parameter" to disambiguate.


Source: (StackOverflow)