472,342 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 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 1399
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...
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....
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...
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...
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...
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...
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...
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...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.