Questions tagged [parameter-passing]

parameter-passing is the process of assigning values to the parameters of a function

Association of actual and formal parameters upon function call. See Parameter Passing and its methods. The most widely used parameter passing methods are:

  • call-by-value
  • call-by-reference
  • call-by-value-result
  • call-by-name
  • call-by-need
  • macro expansion.
7270 questions
7058
votes
89 answers

Is Java "pass-by-reference" or "pass-by-value"?

I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, this blog) that claim that it isn't (the blog post says that Java uses pass-by-value). I don't think I understand the distinction they're…
user4315
  • 4,725
  • 5
  • 20
  • 9
2876
votes
29 answers

How do I pass a variable by reference?

The Python documentation seems unclear about whether parameters are passed by reference or value, and the following code produces the unchanged value 'Original' class PassByReference: def __init__(self): self.variable = 'Original' …
David Sykes
  • 43,314
  • 17
  • 65
  • 77
2675
votes
23 answers

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

In the following method definitions, what does the * and ** do for param2? def foo(param1, *param2): def bar(param1, **param2):
Todd
  • 28,050
  • 4
  • 20
  • 13
827
votes
10 answers

Check number of arguments passed to a Bash script

I would like my Bash script to print an error message if the required argument count is not met. I tried the following code: #!/bin/bash echo Script name: $0 echo $# arguments if [$# -ne 1]; then echo "illegal number of parameters" fi For…
Naftaly
  • 11,192
  • 6
  • 28
  • 43
729
votes
7 answers

How to pass all arguments passed to my bash script to a function of mine?

Let's say I have defined a function abc() that will handle the logic related to analyzing the arguments passed to my script. How can I pass all arguments my bash script has received to it? The number of params is variable, so I can't just hardcode…
devoured elysium
  • 90,453
  • 117
  • 313
  • 521
660
votes
5 answers

What does the star and doublestar operator mean in a function call?

What does the * operator mean in Python, such as in code like zip(*x) or f(**k)? How is it handled internally in the interpreter? Does it affect performance at all? Is it fast or slow? When is it useful and when is it not? Should it be used in a…
553
votes
8 answers

How to write a bash script that takes optional input arguments?

I want my script to be able to take an optional input, e.g. currently my script is #!/bin/bash somecommand foo but I would like it to say: #!/bin/bash somecommand [ if $1 exists, $1, else, foo ]
Abe
  • 10,626
  • 12
  • 42
  • 70
517
votes
17 answers

What does map(&:name) mean in Ruby?

I found this code in a RailsCast: def tag_names @tag_names || tags.map(&:name).join(' ') end What does the (&:name) in map(&:name) mean?
collimarco
  • 29,854
  • 31
  • 89
  • 120
496
votes
7 answers

How can I pass an argument to a PowerShell script?

There's a PowerShell script named itunesForward.ps1 that makes iTunes fast forward 30 seconds: $iTunes = New-Object -ComObject iTunes.Application if ($iTunes.playerstate -eq 1) { $iTunes.PlayerPosition = $iTunes.PlayerPosition + 30 } It is…
Boris Pavlović
  • 58,387
  • 26
  • 115
  • 142
477
votes
15 answers

How do I pass multiple parameters into a function in PowerShell?

If I have a function which accepts more than one string parameter, the first parameter seems to get all the data assigned to it, and remaining parameters are passed in as empty. A quick test script: Function Test([string]$arg1, [string]$arg2) { …
Nasir
  • 9,529
  • 7
  • 28
  • 38
463
votes
5 answers

Expanding tuples into arguments

Is there a way to expand a Python tuple into a function - as actual parameters? For example, here expand() does the magic: some_tuple = (1, "foo", "bar") def myfun(number, str1, str2): return (number * 2, str1 + str2, str2 +…
AkiRoss
  • 9,951
  • 6
  • 51
  • 81
352
votes
11 answers

Why use the params keyword?

I know this is a basic question, but I couldn't find an answer. Why use it? if you write a function or a method that's using it, when you remove it the code will still work perfectly, 100% as without it. E.g: With params: static public int…
MasterMastic
  • 19,099
  • 11
  • 59
  • 86
324
votes
13 answers

JavaScript: Passing parameters to a callback function

I'm trying to pass some parameter to a function used as callback, how can I do that? function tryMe (param1, param2) { alert (param1 + " and " + param2); } function callbackTester (callback, param1, param2) { callback (param1,…
vitto
  • 17,510
  • 29
  • 87
  • 128
306
votes
6 answers

Bare asterisk in function arguments?

What does a bare asterisk in the arguments of a function do? When I looked at the pickle module, I see this: pickle.dump(obj, file, protocol=None, *, fix_imports=True) I know about a single and double asterisks preceding arguments (for variable…
Eric
  • 3,754
  • 2
  • 19
  • 31
300
votes
9 answers

jQuery's .click - pass parameters to user function

I am trying to call a function with parameters using jQuery's .click, but I can't get it to work. This is how I want it to work: $('.leadtoscore').click(add_event('shot')); which calls function add_event(event) { blah blah blah } It works if I…
Paul Hoffer
  • 11,841
  • 6
  • 25
  • 37
1
2 3
99 100