If you pass more arguments than are defined, they will be assigned to an array called arguments. Example please 🙏 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

If you pass more arguments than are defined, they will be assigned to an array called arguments. Example please 🙏

In JavaScript: "If you pass more arguments than are defined, they will be assigned to an array called arguments. They can be used like this: arguments[0], arguments[1], etc." Now, I need an example of arguments[0], arguments[1] etc in such a code below : function sayHello(name, age) { document.write( name + " is " + age + " years old."); } sayHello("John", 20) //Outputs "John is 20 years old."

3rd Jun 2020, 4:13 PM
S M Ashikur Rahman
S M Ashikur Rahman - avatar
3 Answers
+ 9
For python: You can have required arguments and so called *args. In the sample the arguments that are passed from the caller are filled in the sequence they appear, all the rest is hold in varibale "numx". This is a tuple with all other arguments. To use the elements from numx, it can be iterated like a list. def add(num1, num2, *numx): print(f'num1: {num1}, num2: {num2}, others: {numx}') # only for demo print(f'total sum: {sum(list(numx)) + num1 + num2}') add(1,2,3,4,5,6,7)
3rd Jun 2020, 4:52 PM
Lothar
Lothar - avatar
+ 2
Hello Ashiqur Rahman In which programming language? In java it is called varargs. For example fun(int...num) Note the three dots. num can be an array or one or more arguments. fun(5) fun(3, 5) All arguments will threated as an array.
3rd Jun 2020, 4:20 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Thanks all for the helpful replies 💜.. I should have mentioned the specific language. But I forgot 😔..sorry for that. Now I've edited my question/description.. And wrote that. However, I'm interested in learning python as my one of the next course.I think these replies will be helpful then 😂
4th Jun 2020, 4:47 AM
S M Ashikur Rahman
S M Ashikur Rahman - avatar