473,657 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting at optional arguments

So these two functions are different:

void foo(void);

and

void foo();

because the first one allows no arguments, but the second does. My
question is: In the implementation of the second function, how does
one get at those "optional" arguments?

Thanks!
Jan 15 '07 #1
6 2702
Rob Hoelz <ho***@wisc.edu writes:
So these two functions are different:

void foo(void);

and

void foo();

because the first one allows no arguments, but the second does.
This is a misconception. The first declaration declares that
foo() has no parameters. The second declaration does not declare
foo()'s parameters and therefore says nothing about the type or
number of arguments that must be supplied. You can usually
invoke the function whether a prototype is specified or not[*],
but it is safer if you specify a prototype.
[*] Functions with a variable number of arguments are one exception,
functions that have parameters of type that is changed by the
default promotions is another, and I'm not sure that this is an
exhaustive list.
My question is: In the implementation of the second function,
how does one get at those "optional" arguments?
The arguments are not optional. They must be supplied, in the
same way that arguments must be supplied to any other function.

When the function is implemented, the parameters must be
specified, either in prototype form or the now-obsolete K&R form.
--
"For those who want to translate C to Pascal, it may be that a lobotomy
serves your needs better." --M. Ambuhl

"Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt
Jan 15 '07 #2
Rob Hoelz wrote:
So these two functions are different:

void foo(void);

and

void foo();

because the first one allows no arguments, but the second does. My
question is: In the implementation of the second function, how does
one get at those "optional" arguments?
Neither of these functions have optional arguments.

A function with optional arguments must be declared with a prototype,
with at least one fixed argument followed by three dots ('...')
representing the optional arguments.

eg.
void foo(int a, ...);
or
void foo(int a, int b, ...);

You then get at these optional arguments using the va_* macros.

#include <stdarg.h>
#include <stdio.h>

/* Assume a is the number of optional args supplied, and
that each optional arg is an int. Print each argument. */

void foo(int a, ...)
{
int i;
va_list v;

va_start(v, a);

for(i = 0; i < a; i++)
{
printf("%d\n", va_arg(v, int));
}

va_end(v);
}

You can then use the function like this:

foo(0); /* doesn't print */
foo(1, 42); /* prints 42\n */
foo(2, 42, 69); /* prints 42\n69\n */

--
Simon.
Jan 15 '07 #3
Rob Hoelz wrote:
So these two functions are different:

void foo(void);

and

void foo();

because the first one allows no arguments, but the second does. My
question is: In the implementation of the second function, how does
one get at those "optional" arguments?
I assume you mean something along these lines:

#include <stdlib.h>
#include <stdio.h>

void f();

int main(void)
{
f(1,2);
return 0;
}

void f(int i)
{
/* how can we get at both 1 and 2? */
}
The answer is, you can't reliably, portably, or legally. The Standard
(well, n869) says the following:

J.2 Undefined behavior

[#1] The behavior is undefined in the following
circumstances:

[big snip]

-- For a function call without a function prototype, the
number of arguments does not agree with the number of
parameters (6.5.2.2).
So there's no legal way to even call the function with "optional"
parameters, let alone access them.

Depending on your implementation, there might be a way (as long as
demons are flying out of your nose anyway) to get at that extra
argument, assuming that your program didn't crash or use your credit
card to buy nude koala bear photos from the web. But any method you did
find would likely be non-portable.

The upshot is that it just isn't useful. If you need variable
parameters, there are ways to do it. That isn't it.


Brian
Jan 16 '07 #4
Default User <de***********@ yahoo.comwrote:
The answer is, you can't reliably, portably, or legally. The Standard
(well, n869) says the following:
J.2 Undefined behavior
[#1] The behavior is undefined in the following
circumstances:
-- For a function call without a function prototype, the
number of arguments does not agree with the number of
parameters (6.5.2.2).
I found myself making a reference to 6.5.2.2 a few days ago in a
similar context, and I noticed that the Annex is "informativ e". Is
there also some normative text in 6.5.2.2 that explicitly states that
the behavior in this particular situation is undefined? Or is it
undefined by omission?

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Jan 16 '07 #5
Christopher Benson-Manica wrote:
Default User <de***********@ yahoo.comwrote:
The answer is, you can't reliably, portably, or legally. The Standard
(well, n869) says the following:
J.2 Undefined behavior
[#1] The behavior is undefined in the following
circumstances:
-- For a function call without a function prototype, the
number of arguments does not agree with the number of
parameters (6.5.2.2).

I found myself making a reference to 6.5.2.2 a few days ago in a
similar context, and I noticed that the Annex is "informativ e". Is
there also some normative text in 6.5.2.2 that explicitly states that
the behavior in this particular situation is undefined? Or is it
undefined by omission?
"[...] If the number of arguments does not equal the number of
parameters, the behavior is undefined. [...]" (6.5.2.2p6)

Jan 16 '07 #6
Christopher Benson-Manica wrote:
Default User <de***********@ yahoo.comwrote:
The answer is, you can't reliably, portably, or legally. The
Standard (well, n869) says the following:
J.2 Undefined behavior
[#1] The behavior is undefined in the following
circumstances:
-- For a function call without a function prototype, the
number of arguments does not agree with the number of
parameters (6.5.2.2).

I found myself making a reference to 6.5.2.2 a few days ago in a
similar context, and I noticed that the Annex is "informativ e". Is
there also some normative text in 6.5.2.2 that explicitly states that
the behavior in this particular situation is undefined? Or is it
undefined by omission?

Hmmm, good point. Perhaps that paragraph of the Standard is meant to
state explicitly what one can glean from elsewhere.


Brian

Jan 16 '07 #7

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

Similar topics

9
4569
by: Rob Long | last post by:
Hey, I've just noticed a somewhat annoying feature with PHP5 type hinting. You cannot hint types on optional arguments like this: class MyClass { function someFunc(Type1 $arg1, Type2 $arg2 = new Type2()) { }
7
36581
by: JT | last post by:
how can i declare a function that will accept an optional parameter? something like: function newFunc(strValue1, strValue2) --where strValue2 is optional. thanks much.
2
17732
by: Exyle | last post by:
I have a function that has two arguents, however depending on the value of the first argument, the second one may or may not be needed. If I only input the first argument when calling the function, PHP returns a warning. many built in php functions have optional arguments, is there a way to make arguments in user defined functions optional as well? Thanks.
13
2511
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. I've done this before with Modules, and saw that <gasp> they behave just like sealed classes with only static members. Anyway, it looks like Optional Parameters are nothing but a way to tell the compiler to write some overloads for you. So, in...
16
4057
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
3
4784
by: Winshent | last post by:
I want to pass optional arguments to build up varios strings, is this possible? look at arrTBL(1,3) in the code below: =========================================== Public Function arrTBL(Optional ByVal DataSetName As String = "") As String(,) Dim DSN As String = DataSetName
12
2288
by: Nick Hounsome | last post by:
Can anyone tell me what the rational is for not supporting optional arguments. It is obviously a trivial thing to implement and, since C++ has them, I would not expect them to be omitted without a good reason.
5
14879
by: Jason | last post by:
Hi there, I'm new to C .. have a very basic question .. hope someone has the patience to help! I want to declare a function that has optional parameters and I want to initialize the non supplied parameters .. how do I do this? eg:
14
3262
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and which not, rather than relying on massively overlaoded methods which hopefully provide the best...
0
8420
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
agi2029
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.