Question about a function call with multiple arguments

In summary, Python allows you to pass multiple arguments to a function, whereas Java does not. Java also has a feature called "varargs" which allows for a variable number of arguments. Python also has the "tuple" data structure which allows different datatypes to be combined together.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,111
2,691
Recently in college, we have started learning python. I found that the print statement can accept multiple variables. For example, if I have variables x1, x2 and x3, I can write print(x1, x2, x3) in python. Something similar exists in Matlab as well.

I have learned Java for nearly four years, and I know that something like this is not possible there. If I have to print three variables, I have to write System.out.println("" + x1 + x2 + x3);, that is, concatenate the variables. I can put multiple arguments only if the function definition allows me to do that. I cannot just put the variables as arguments; rather, I have to concatenate them as a string.

How does python and/or Matlab work in this regard? I put in multiple variables as arguments, and that is accepted! Overloading is certainly not a solution here.

In addition, if I want to define a separator in python print statement, I can mention that using sep = ' ' as an argument in the function call. But how does python recognise this as the separator only using the keyword "sep"?
 
Technology news on Phys.org
  • #2
See the examples here.
 
  • Informative
Likes Wrichik Basu
  • #3
Also Java hasn't been left behind there is a feature called varargs:

https://www.geeksforgeeks.org/variable-arguments-varargs-in-java/
that allows for a variable number of arguments.

The way these schemes work is either the arguments are all of the same type and the compiler creates an array of the arguments under the covers. Or the arguments are of different types and the compiler has a reflection feature ie passes datatype along with the values allowing them to be read back using the correct datatype.

Python in particular has the tuple structure that allows different datatypes to be combined together. This is especially useful when you want to return multiple values ie you return one tuple with the values packed in the returned tuple.
 
  • Informative
  • Like
Likes QuantumQuest, Klystron and Wrichik Basu
  • #4
jedishrfu said:
Also Java hasn't been left behind there is a feature called varargs:

https://www.geeksforgeeks.org/variable-arguments-varargs-in-java/
that allows for a variable number of arguments.
Didn't know about that. Thanks.

Our Professor says that the fact that Python doesn't have any syntax to end a structure (unlike Matlab or java, which uses the end keyword and braces respectively), makes it a much more advanced language. Somehow I cannot support this. I know I will not use java for solving a physics problem. But given an option, I will perhaps not use Python as well for doing an extensive program. I would rather prefer Matlab (or Octave). Till now, I knew that the alignment of code is for the purpose of readability of the code. I am seeing for the first time a language that depends completely on alignment.
 
  • Like
Likes Klystron
  • #5
jedishrfu said:
Also Java hasn't been left behind there is a feature called varargs:
It's surprising to me that this wasn't a part of Java until JDK 5. The varargs concept has been part of C/C++ for many years.
 
  • #6
Wrichik Basu said:
How does python and/or Matlab work in this regard?

The Python print function does "under the hood" the string concatenation that Java makes you do explicitly. It's a convenience feature. (Note that Python also accepts arguments to print that are not strings; it calls the __str__ method of those objects under the hood to get their string representation, whereas Java also makes you explicitly call whatever function gives you the string representation of non-string objects before printing.)
 
Last edited:
  • #7
Wrichik Basu said:
Somehow I cannot support this.

Having indentation be significant, with no block end delimiters, is something that many people take time to get used to with Python. Having programmed in Python for many years, I find it very useful not to have to type delimiters and keep track of block closures; I can just indent my code the way I would do it anyway, for readability, and Python takes care of the rest.
 
  • Like
Likes Wrichik Basu
  • #8
Wrichik Basu said:
Didn't know about that. Thanks.

Our Professor says that the fact that Python doesn't have any syntax to end a structure (unlike Matlab or java, which uses the end keyword and braces respectively), makes it a much more advanced language.
IMHO, this is a very superficial reason to call a language more advanced. In fact, Python has some very annoying throwbacks to old problems. For instance, one of the treacherous things that made old makefiles error-prone was the way a continued statement had to end in a '\', but only if there was no space after that. Those were so hard to spot. Of all the things for Python to adopt, that is IMHO the most annoying.
 
  • Like
Likes Wrichik Basu
  • #9
I get hit with the tabs vs spaces issue in python as i switch editors based on where the code is needed.

In general its best to have soft tabs where the editor always inserts spaces but you have to remember to check if its on.
 
  • #10
Wrichik Basu said:
Didn't know about that. Thanks.

Our Professor says that the fact that Python doesn't have any syntax to end a structure (unlike Matlab or java, which uses the end keyword and braces respectively), makes it a much more advanced language. Somehow I cannot support this. I know I will not use java for solving a physics problem. But given an option, I will perhaps not use Python as well for doing an extensive program. I would rather prefer Matlab (or Octave). Till now, I knew that the alignment of code is for the purpose of readability of the code. I am seeing for the first time a language that depends completely on alignment.

I would reconsider that argument as python has oo features and can be divided into modules.

The only gripe we have with python is its inability to package everything needed into one file short making a multimegabyte docker image. Of course small scripts with external modules like numpy pandas or whatever can be a single script. Its the application level code that runs into this issue.
 
  • #11
jedishrfu said:
I get hit with the tabs vs spaces issue in python as i switch editors based on where the code is needed.

In general its best to have soft tabs where the editor always inserts spaces but you have to remember to check if its on.
Spaces and tabs are really creating a problem. Till now, I have mostly used an IDE for writing codes. When I used notepad/gedit, I used to indent with tab. Now in python, this is becoming a major problem. When you are writing a long program, it becomes really irritating to count and put spaces. One space less, and your program falls through.
 
  • #12
jedishrfu said:
I would reconsider that argument as python has oo features and can be divided into modules.

The only gripe we have with python is its inability to package everything needed into one file short making a multimegabyte docker image. Of course small scripts with external modules like numpy pandas or whatever can be a single script. Its the application level code that runs into this issue.
Yes, it has, but I believe Python is not quite built for the same purpose as java or c++. While learning from online tutorials, I found it is customary to put a reference to the currently called object in a function definition, through the keyword self. I don't remember doing this in java (except maybe in a few cases). I believe Python might be good for scientific computing, but not suitable for application development.
 
  • #13
FactChecker said:
For instance, one of the treacherous things that made old makefiles error-prone was the way a continued statement had to end in a '\', but only if there was no space after that.
Does this problem still exist in Python 3.7, or has it died away with 2.7?

An additional problem of python is compatibility issues. Currently, I am coding in 3.7, but who knows, they might stop supporting codes of 3.x once 4.x is released.
 
  • #14
Wrichik Basu said:
When you are writing a long program, it becomes really irritating to count and put spaces.

Every editor I'm aware of allows you to let the "Tab" key be the equivalent of some standard number of spaces (4 is the usual number for Python code).
 
  • #15
Wrichik Basu said:
I believe Python might be good for scientific computing, but not suitable for application development.

If this were true, it would be a huge surprise to the many people (myself included) who develop applications in Python.

Since you admit you are unfamiliar with Python as a language, you should be more cautious in your claims.
 
  • #16
Wrichik Basu said:
While learning from online tutorials, I found it is customary to put a reference to the currently called object in a function definition, through the keyword self.

It's not "customary", it's required in Python. If a function is a method on a class, it has to have as one of its arguments the instance of that class that is calling the method. Other languages like C++ do this by an implicit parameter ("this" in C++) that is automatically defined inside methods. Python does it by explicitly requiring you to define the "self" argument when you define the function. That way an individual function doesn't even have to "know" whether it's a method on a class or not; it just operates on the arguments that are passed to it.
 
  • #17
Wrichik Basu said:
Does this problem still exist in Python 3.7, or has it died away with 2.7?

AFAIK it's still there. However, use of continuations of lines like this is extremely rare in Python (whereas it was common with makefiles). And there's an easy way to avoid it in Python (whereas there was no way to avoid it in makefiles): just enclose any expression that will occupy multiple lines in parentheses, since that makes the expression look like one line to the interpreter. This is commonly done, for example, with long import statements.
 
  • #18
Wrichik Basu said:
An additional problem of python is compatibility issues. Currently, I am coding in 3.7, but who knows, they might stop supporting codes of 3.x once 4.x is released.

Considering that Python 2.7 is still supported almost 10 years after Python 3 was initially released, I think this concern is extremely farfetched.
 
  • #19
PeterDonis said:
If this were true, it would be a huge surprise to the many people (myself included) who develop applications in Python.

Since you admit you are unfamiliar with Python as a language, you should be more cautious in your claims.
Well, I do not claim this. I admit that I am a beginner. Our Professor told us in the class that python is not quite used for application development.
 
  • #20
PeterDonis said:
Considering that Python 2.7 is still supported almost 10 years after Python 3 was initially released, I think this concern is extremely farfetched.
Correct me if I am wrong, but I think there are some compatibility issues if you try to execute a python 2.7 program using 3.7 compiler (at least our Prof. told so; I am not sure exactly where these issues occur).
 
  • #21
Wrichik Basu said:
I do not claim this.

Do not claim what? You posted this:

Wrichik Basu said:
I believe Python might be good for scientific computing, but not suitable for application development.

That's a claim. You said "I believe". That's stating it as your opinion, not someone else's.

Wrichik Basu said:
Our Professor told us in the class that python is not quite used for application development.

And if that's what you meant, that's what you should have said. My response would be much the same.
 
  • #22
Wrichik Basu said:
Correct me if I am wrong, but I think there are some compatibility issues if you try to execute a python 2.7 program using 3.7 compiler

Yes, which is why you use a Python 2.7 interpreter to run Python 2.7 code. Python 2.7 is available on any system where Python 3.7 is available (in fact, Python 2.7 is probably available on more systems than Python 3.7 since Python 3.7 is a very recent release). So this is not a problem.
 
  • #23
PeterDonis said:
Yes, which is why you use a Python 2.7 interpreter to run Python 2.7 code. Python 2.7 is available on any system where Python 3.7 is available (in fact, Python 2.7 is probably available on more systems than Python 3.7 since Python 3.7 is a very recent release). So this is not a problem.
That is not true. In a large organization, where a system administration department takes care of all computers, it can be a constant struggle to get the right ones on your computer and on other computers that the code needs to be developed on. Where I worked, it took an act of God to get the right things on the computers.
 
  • #24
FactChecker said:
In a large organization, where a system administration department takes care of all computers, it can be a constant struggle to get the right ones on your computer and on other computers that the code needs to be developed on.

Yes, but it's just as much of a struggle to get Python 3.7 on them all as it is to get Python 2.7 on them all. So this is not an advantage of Python 3.7 over 2.7.
 
  • #25
FactChecker said:
In a large organization, where a system administration department takes care of all computers, it can be a constant struggle to get the right ones on your computer and on other computers that the code needs to be developed on.

Also, this problem is hardly particular to Python; all languages have new versions come out and have backwards compatibility issues with old versions. Python supporting 2.7 for almost 10 years now since Python 3 was released is actually a pretty long support time frame for an old version, compared to other languages.
 
  • #26
Wrichik Basu said:
Yes, it has, but I believe Python is not quite built for the same purpose as java or c++. While learning from online tutorials, I found it is customary to put a reference to the currently called object in a function definition, through the keyword self. I don't remember doing this in java (except maybe in a few cases). I believe Python might be good for scientific computing, but not suitable for application development.
Java has self embedded as the this pointer which is optionally used when a naming conflict occurs between function args and instance attributrs
 
  • #27
jedishrfu said:
Java has self embedded as the this pointer which is optionally used when a naming conflict occurs between function args and instance attributrs

For python oo is kind of tacked on to the language hence you need to use self in methods as a placeholder argument but not when you call the method.
 

1. How do I pass multiple arguments in a function call?

In order to pass multiple arguments in a function call, you can simply separate each argument with a comma within the parentheses of the function call. For example: functionName(argument1, argument2, argument3).

2. What is the maximum number of arguments that can be passed in a function call?

The maximum number of arguments that can be passed in a function call depends on the programming language and the specific function being used. Some languages may have a limit on the number of arguments, while others may not have any limitations.

3. Can I pass arguments in any order in a function call?

Yes, in most programming languages, the order of the arguments in a function call does not matter as long as they are separated by a comma. However, some functions may have specific requirements for the order of arguments, so it is important to check the documentation for the specific function you are using.

4. What happens if I pass more or less arguments in a function call than the function expects?

If you pass more arguments than the function expects, the extra arguments will be ignored. If you pass less arguments than the function expects, you may receive an error depending on the programming language and the function being used. It is important to pass the correct number of arguments to avoid errors.

5. Can I use variables as arguments in a function call?

Yes, you can use variables as arguments in a function call. The variable's value will be passed to the function as the argument. It is important to make sure the variable has a value before using it as an argument in a function call.

Similar threads

  • Programming and Computer Science
Replies
3
Views
766
  • Programming and Computer Science
Replies
3
Views
268
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
2
Replies
43
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Classical Physics
Replies
4
Views
867
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
951
  • Set Theory, Logic, Probability, Statistics
2
Replies
54
Views
3K
  • Programming and Computer Science
Replies
25
Views
3K
Back
Top