473,320 Members | 1,865 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,320 software developers and data experts.

Default values for arguments

Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?

Thx in advans,
Karthik Balaguru

Sep 2 '07 #1
8 1462
karthikbalaguru wrote:
Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?
Not in C, you must be thinking of C++.

--
Ian Collins.
Sep 2 '07 #2
karthikbalaguru wrote:
Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?

Thx in advans,
Karthik Balaguru
The lcc-win32 compiler features this as an extension.

It is not possible in standard C.
Sep 2 '07 #3
karthikbalaguru wrote:
Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?
Yes. Just code in the default value either as a const qualified variable or
with the help of the define preprocessor directive.

Sep 2 '07 #4
On Sep 2, 6:46 am, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?

Unless the function is variadic and has a declaration of the form
T f(T, ...); or the ill-advised and old fashioned form
T f(); you must pass all arguments.

A common idiom is to pass flags in the required arguments
which indicate which of the optional arguments are being
passed (eg open() in unix). For example, if you have a
function that constructs an empty list that takes 2
optional arguments, an initial size and a memory allocation
function, you could do:

struct list * new_list( int flags, ...);

and make calls to it like:

struct list * A;
A = new_list( 0 );
A = new_list( LIST_SIZE, 1024 );
A = new_list( LIST_SIZE | LIST_MALLOC, 1024, malloc );

In the definition of new_list(), you check the flags
and set values appropriately.

Sep 2 '07 #5
santosh wrote:
karthikbalaguru wrote:
>Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?

Yes. Just code in the default value either as a const qualified
variable or with the help of the define preprocessor directive.
Can you give an example of how to do it.
I can't think of a way to specify default values for function argument,
so that you don't have to specify them at the call site.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
Sep 2 '07 #6
On Sun, 02 Sep 2007 13:24:54 +0200, jacob navia wrote:
karthikbalaguru wrote:
>Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?

Thx in advans,
Karthik Balaguru

The lcc-win32 compiler features this as an extension.
So what? This group is about C, the OP could not be using
lcc-win32, or even Windows at all. If the OP was interested to one
particular compiler, (s)he'd have asked on a group about that
compiler. (Actually there are newbies who happen to make questions
about one compiler, but usually they *say* what that compiler is,
and Karthik didn't.)
<flame>IOW: go spamming elsewhere</flame>
It is not possible in standard C.
Right.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Sep 3 '07 #7
Ian Collins <ia******@hotmail.comwrote:
# karthikbalaguru wrote:
# Hi,
# >
# Is it possible to define default values for arguments that are not
# passed when the function call is made ?
# >
# Not in C, you must be thinking of C++.

You can sometimes simulate this with var-args, but remember var-args
doesn't tell you what the actual argument list is. You need some
other protocol to know how long the argument list is and what the
parameter types are.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Wow. A sailboat.
Sep 3 '07 #8
Bart van Ingen Schenau wrote:
santosh wrote:
>karthikbalaguru wrote:
>>Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?
Yes. Just code in the default value either as a const qualified
variable or with the help of the define preprocessor directive.

Can you give an example of how to do it.
I can't think of a way to specify default values for function argument,
so that you don't have to specify them at the call site.

Bart v Ingen Schenau
Somethin' along these lines:
extern int foo(int, int);
#define foo(a) foo((a), 5)
int bar(int a, int b)
{
(foo)(a, b);
return foo(a);
}
Note that the function name is parenthesized. Can only hide one
parameter though.

-- Ark
Sep 4 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

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...
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 )
18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
10
by: Ook | last post by:
I'm having trouble comprehending what exactly "default construction" is. I know how to provide a constructor with initial values, so that if I, for example, in my code do this: MyClass...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
10
by: Alan G Isaac | last post by:
My class MyClass reuses many default parameters with a small number of changes in each instance. For various reasons I decided to put all the parameters in a separate Params class, instances of...
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...
7
by: George Sakkis | last post by:
A situation that often comes up is having to initialize several instance attributes that accept a default value. For a single class, passing the default values in __init__ is fine: class...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.