473,624 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const non-pointer function parameters

Shoud we declare non-pointer function parameters with const keywords?

int main(void){
int f(const int i);
int i;

f(i);
return 0;
}

Mar 26 '07 #1
7 2220
lovecreatesbea. ..@gmail.com said:
Shoud we declare non-pointer function parameters with const keywords?

int main(void){
int f(const int i);
int i;

f(i);
You have asked the wrong question. Here is a more important and urgent
question for you: "why is it a very bad idea indeed to pass
indeterminate values to functions?"

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 26 '07 #2
lovecreatesbea. ..@gmail.com wrote:
Shoud we declare non-pointer function parameters with const keywords?
Why would you want to? What effect do you think it would have?

--
Ian Collins.
Mar 26 '07 #3
lovecreatesbea. ..@gmail.com wrote:
>
Shoud we declare non-pointer function parameters with const keywords?
int f(const int i);
It isn't usually done that way.
A const parameter might seem to make sense
if you're viewing the definition of the the function but,
it isn't usually written that way because
the calling function is not affected by
what happens to the value of i inside of f.

--
pete
Mar 26 '07 #4
"lovecreatesbea ...@gmail.com" <lo************ ***@gmail.comha scritto nel
messaggio news:11******** **************@ n59g2000hsh.goo glegroups.com.. .
Shoud we declare non-pointer function parameters with const keywords?

int main(void){
int f(const int i);
int i;
Supposing you do something else here...
f(i);
return 0;
}
When you call f(i), the value of i is copied into a local variable in f()'s
stack, which in this case is called i too.
Let's use different names for them to show the distinction:

int main(void) {
int f(const int k);
int i;
/* do something */
f(i); /*let's suppose i equals 5 here*/
return 0;
}

int f(const int k) {
/* do something */
}

As soon as f(i) is called, the local variable k in function f() is
initialized to 5. The variable k is discarded as soon as f() returns.
Anyway, the local variable i in function main() is unaffected by f() (which
is not even aware that i exists). The const in the function prototype says
that f() should never change the value of k during its execution. If *this*
is what you want, it's ok to write int f(const int k) (or int f(const int
i), or int f(const int whatever); anyway the name of the parameter won't
conflict with that of any local variables in main(), since f() can't even
see them).
Mar 26 '07 #5
On 26 Mar 2007 02:26:28 -0700, "lovecreatesbea ...@gmail.com"
<lo************ ***@gmail.comwr ote:
Shoud we declare non-pointer function parameters with const keywords?

int main(void){
int f(const int i);
int i;

f(i);
return 0;
}
First, it is usually a bad idea to declare functions 'locally' (within
a function). It is legal, but rare, so most other (e.g. maintenance)
programmers won't be used to it and will easily misread these
declarations or miss them when looking something up. And it is never
necessary except in the unusual and rare case that you are writing a
'private' (internal linkage) wrapper or proxy for an external routine
(or even datum).

Second, it doesn't matter to the compiler. Putting 'top level' const
on a parameter _in the definition_ does make it unwritable (legally)
within the body, and may or may not be a good idea depending on your
design and coding rules for function bodies. Putting it in the
(separate) declaration has no effect on calls and it is guaranteed to
work either way; e.g.:
/* foo.c */
int foo ( const int zorg ) { ... do stuff ... return n; }
/* bar1.c */
int foo ( const int zorg ) ; /* perhaps from header */
... foo (3) ...
/* bar2.c */
int foo ( /*unqual*/ int zorg ) ; /* ditto */
... foo (42) ...
both declarations are compatible with the definition and are
guaranteed to work the same, and indeed are compatible with each other
and can both appear (or be #include'd) in the same source and scope.
(Although I have seen a compiler, IIRC AIX xlc, get this wrong.)

Given that you do use top-level const in the definition:

The (style) disadvantage of putting it in the separate declaration(s)
used by the caller(s) is that it is unnecessary and may be confusing
to those not familiar with it, or at least add a little clutter.

The (style) advantage of putting it there is that you can make the
declaration (often in a .h file) an exact textual copy of the
beginning of the definition, either by just copy-and-paste or more
automatic and systematic means.

Apr 15 '07 #6
On Mar 26, 10:26 am, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
Shoud we declare non-pointer function parameters with const keywords?
Usually, I would say no. But it can be helpful. eg:

int
really_convolut ed_function_I_h ave_to_maintain ( int x )
{
/*
* Lots of stuff that covers hundreds, maybe thousands
* of lines, that I don't have time to look at:
*/

/* Finally, a reference to x. */

/* more code */
}

Facing that function, I might glance through the code and
conclude that x is not being modified by the code above the
first reference that I see to x. I can search for instances
of x in the code, but I keep seeing instances of the name
x in comments or in #defined conditionals, so I'm not
really certain what's going on. As a confirmation,
I might modify the formal parameter list and add a const
qualifier so that the compiler will either confirm or
refute my hypothesis.

In other words, the only use I see for it is to
provide information that should be more readily
available by writing clean code in the first place.

--
Bill Pursell

Apr 15 '07 #7
"lovecreatesbea ...@gmail.com" <lovecreatesbea ...@gmail.comwr ote:
Shoud we declare non-pointer function parameters with const
keywords?

int f(const int i);
Ford: Yes, if you like.
Barman: Will that help?
Ford: No.

--
Peter

Apr 16 '07 #8

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

Similar topics

8
6826
by: Nick Savoiu | last post by:
In the code below how can I get x to use the const T& operator? Thanks, Nick #include <stdio.h> template <typename T> class C { public:
17
3265
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
1
1188
by: Dave | last post by:
In std::stack<>, why are both const and non-const versions of top() necessary? Wouldn't only the const version suffice? This could be called on either a const or non-const stack object and, in either case, this method does indeed not modify the stack it is a part of...
4
1787
by: Dave | last post by:
Hello NG, In a thread I had started a long time ago, the conclusion had been reached that there is no good way to select between calling const vs non-const methods of the same name. (Of course, the object in question is non-const so that both may be legally called.) However, it appears I have indeed found a way to do it. I'm sure hoping nobody can poke any holes in my little scheme, but I want to throw it out there to see if it stands...
14
2858
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
2
2583
by: Mark Stijnman | last post by:
I would like to be able to have an object accessible as a vector using the operator, but able to track modifications to its data, so that it can update other internal data as needed. I figured that setting a flag in the non-const operator would work, but it doesn't. Take the code below: #include <iostream> using std::cout; using std::cerr;
10
2055
by: S.Tobias | last post by:
1. If I have a struct type which contains a const member: struct mystruct { const int id; int mutable; }; does a definition struct mystruct ms; define an object `ms' which is *partly* const? Ie. the object `ms' is not const as a whole, but modifying ms.id yields UB in all contexts?
17
3458
by: George2 | last post by:
Hello everyone, This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to correct me. Thanks. 1. A const reference can be binded to a rvalue, for example, a temporary object. And the "life" of the temporary object is guaranteed to be extended and we can safely operate through the const-reference. 2. A non-const reference can...
1
1310
by: nsdevelop12 | last post by:
Is "const Object* pA = new Object(1)" the same as "const Object A(1)" in terms of creating const objects that should not be modified through cast-to-non-const pointers. See the following example: const int* createInt() { return new int(1); }
19
2691
by: fungus | last post by:
I define this class: class foo { std::vector<int>data; public: int operator(int n) { return data; }
0
8172
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
8620
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
8335
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,...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
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...
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
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.