Is it possible to set default arguments for functions like you are able to in PHP?
Like so: -
function(type = 'default', default = 'argument')
-
// ...
-
?
Cheers guys
7 1913 gits 5,390
Expert Mod 4TB
hi ...
not like that ... but you may use: - function my_func(arg0) {
-
// if arg0 is not passed to my_func
-
if (typeof arg0 == 'undefined') {
-
arg0 = 'default_val0';
-
}
-
-
// now use arg0 and further code
-
}
kind regards
hi ...
not like that ... but you may use: - function my_func(arg0) {
-
// if arg0 is not passed to my_func
-
if (typeof arg0 == 'undefined') {
-
arg0 = 'default_val0';
-
}
-
-
// now use arg0 and further code
-
}
kind regards
Yeh, thought that might be the case.
Was just looking for a prettier alternative ^_^
This should hopefully be supported in the next major version of JavaScript/ECMAScript or something similar. In the meantime, you could use something like this.
- function shout(message){
-
alert( message || "hello world" )
-
}
That's effective in most cases,but if you pass null, false, 0, empty string, etc. it will return "hello world" which is probably not what you want.
That's effective in most cases,but if you pass null, false, 0, empty string, etc. it will return "hello world" which is probably not what you want.
thats a good point. any falsy -evaluating variable would trigger the default.
if you want the defaults only upon undefineds, try something like: -
function shout(message){
-
alert( message != undefined ? message : "hello world" )
-
}
-
- function (message){
-
message= (message !== undefined)? message : "default" ;
-
}
!== not equal and same type
Sign in to post your reply or Sign up for a free account.
Similar topics
by: J.R. |
last post by:
Hi folks,
The python can only support passing value in function call (right?), I'm
wondering how to effectively pass a large parameter, such as a large list or
dictionary?
It could achieved...
|
by: Edward Diener |
last post by:
In the tutorial on functions there are sections on default arguments and
keyword arguments, yet I don't see the syntactic difference between them.
For default arguments the tutorial shows:
def...
|
by: Nick Coghlan |
last post by:
Time for another random syntax idea. . .
So, I was tinkering in the interactive interpreter, and came up with the
following one-size-fits-most default argument hack:
Py> x = 1
Py> def...
|
by: Agent Mulder |
last post by:
Hi group,
I want to know why this doesn't work
void f(int a=0,int b=1,int c=2){}
int main()
{
f(); //OK
f(1); //OK
f(1,2); //OK
f(1,2,3); //OK
|
by: earl |
last post by:
class temp
{
public:
temp();
foo(char, char, char*);
private:
char matrix;
};
temp::foo(char p, char o, char m = matrix )
|
by: The Directive |
last post by:
This code will not compiled:
In main function:
Dot temp = new Dot( *(new Point( 5, 5 )) );
In Dot class:
//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5...
|
by: aling |
last post by:
What's the rule of default argument of function in C++? I found that
the default argument of function could not necessary be a constant
value. Is it true? Previously I thought that the default...
|
by: sods |
last post by:
Hi,
I write a test code about template used for strategy.
it's very similar to sample code in TC++PL 13.4.1.
#include <iostream>
#include <string>
using std::basic_string;
|
by: claudiu |
last post by:
Hi,
I'll go straight to the first question. Why does the code below
compile?
void f(int i = 0);
int main()
{
(&f)();
|
by: bukzor |
last post by:
I've found some bizzare behavior when using mutable values (lists,
dicts, etc) as the default argument of a function. I want to get the
community's feedback on this. It's easiest to explain with...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |