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.
8028 questions
7724
votes
91 answers

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

I always thought Java uses pass-by-reference. However, I read a blog post which claims that Java uses pass-by-value. I don't think I understand the distinction the author is making. What is the explanation?
user4315
  • 4,775
  • 5
  • 23
  • 9
3315
votes
27 answers

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

What do *args and **kwargs mean in these function definitions? def foo(x, y, *args): pass def bar(x, y, **kwargs): pass See What do ** (double star/asterisk) and * (star/asterisk) mean in a function call? for the complementary question…
Todd
  • 34,500
  • 4
  • 23
  • 13
3259
votes
40 answers

How do I pass a variable by reference?

I wrote this class for testing: class PassByReference: def __init__(self): self.variable = 'Original' self.change(self.variable) print(self.variable) def change(self, var): var = 'Changed' When I tried…
David Sykes
  • 48,469
  • 17
  • 71
  • 80
968
votes
11 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…
triple fault
  • 13,410
  • 8
  • 32
  • 45
941
votes
7 answers

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

Let's say I have 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 abc()? The number of arguments is variable, so I can't just hard-code…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
786
votes
4 answers

What do ** (double star/asterisk) and * (star/asterisk) mean in a function call?

In code like zip(*x) or f(**k), what do the * and ** respectively mean? How does Python implement that behaviour, and what are the performance implications? See also: Expanding tuples into arguments. Please use that one to close questions where OP…
743
votes
10 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
  • 12,956
  • 12
  • 51
  • 72
576
votes
8 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ć
  • 63,078
  • 28
  • 122
  • 148
565
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
  • 10,935
  • 8
  • 31
  • 39
552
votes
5 answers

Expanding tuples into arguments

Suppose I have a function like: def myfun(a, b, c): return (a * 2, b + c, c + b) Given a tuple some_tuple = (1, "foo", "bar"), how would I use some_tuple to call myfun? This should output the result (2, "foobar", "barfoo"). I know could define…
AkiRoss
  • 11,745
  • 6
  • 59
  • 86
550
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
  • 34,231
  • 36
  • 108
  • 142
527
votes
8 answers

How to pass a function as a parameter in Java?

In Java, how can one pass a function as an argument to another function?
Jason
  • 5,451
  • 4
  • 19
  • 12
477
votes
5 answers

What does a bare asterisk do in a Python parameter list? (What are "keyword-only" parameters?)

What does a bare asterisk in the parameters 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 parameters (for variable…
Eric
  • 5,686
  • 2
  • 23
  • 36
363
votes
17 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? This is my try: function tryMe(param1, param2) { alert(param1 + " and " + param2); } function callbackTester(callback, param1, param2) { callback(param1,…
vitto
  • 19,094
  • 31
  • 91
  • 130
361
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
  • 20,711
  • 12
  • 68
  • 90
1
2 3
99 100