473,396 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Default function arguments

Markus
6,050 Expert 4TB
Is it possible to set default arguments for functions like you are able to in PHP?

Like so:

Expand|Select|Wrap|Line Numbers
  1. function(type = 'default', default = 'argument')
  2. // ...
  3.  
?

Cheers guys
May 21 '08 #1
7 1912
gits
5,390 Expert Mod 4TB
hi ...

not like that ... but you may use:

Expand|Select|Wrap|Line Numbers
  1. function my_func(arg0) {
  2.     // if arg0 is not passed to my_func 
  3.     if (typeof arg0 == 'undefined') {
  4.         arg0 = 'default_val0';
  5.     }
  6.  
  7.     // now use arg0 and further code
  8. }
kind regards
May 21 '08 #2
Markus
6,050 Expert 4TB
hi ...

not like that ... but you may use:

Expand|Select|Wrap|Line Numbers
  1. function my_func(arg0) {
  2.     // if arg0 is not passed to my_func 
  3.     if (typeof arg0 == 'undefined') {
  4.         arg0 = 'default_val0';
  5.     }
  6.  
  7.     // now use arg0 and further code
  8. }
kind regards
Yeh, thought that might be the case.
Was just looking for a prettier alternative ^_^
May 21 '08 #3
acoder
16,027 Expert Mod 8TB
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.
May 21 '08 #4
rnd me
427 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. function shout(message){
  2.      alert( message || "hello world" )
  3. }
May 22 '08 #5
acoder
16,027 Expert Mod 8TB
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.
May 22 '08 #6
rnd me
427 Expert 256MB
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:

Expand|Select|Wrap|Line Numbers
  1.   function shout(message){
  2.     alert( message != undefined ? message : "hello world" )
  3.   }
  4.  
May 22 '08 #7
mrhoo
428 256MB
Expand|Select|Wrap|Line Numbers
  1. function (message){
  2.          message= (message !== undefined)? message : "default" ;
  3. }
!== not equal and same type
May 23 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

46
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...
14
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...
8
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...
8
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
12
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 )
11
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...
4
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...
4
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;
12
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)();
35
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.