Solved

const keyword in functions

Posted on 2006-11-18
3
324 Views
Last Modified: 2010-04-01
Hi Experts,

when functions are declared with const parameters, I think I read it means it will  not be changed within that function.

functionOne( const string& a);    
functionTwo( const Test& t);
funcionThree(const bool c);

Are these always good idea when I am not going to change that parameter inside the function?

Also, functionOne(const string a)  is it going to be the same as functionOne(const string& a)
Other than that I am passing reference rather than value.

0
Comment
Question by:ambuli
3 Comments
 
LVL 9

Assisted Solution

by:DrAske
DrAske earned 100 total points
Comment Utility
>>Are these always good idea when I am not going to change that parameter inside the function?
yes :)

>>Also, functionOne(const string a)  is it going to be the same as functionOne(const string& a)
regarding that *string* a will not change inside the function, *YES* it is.
but the first argument is passed by-value, and the other one is passed by-reference
(using  *&* depends on the size of the arguement)

NOTE!!
Many C++ programmers prefer that
1) Modifiable arguments be passed to functions by using pointers.
2) SMALL NONmodifiable arguments be passed by value.
3) HUGE (big) NONmodifiable arguments be passed by using REFERECES TO CONSTANTS.

regards,Ahmad;
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 150 total points
Comment Utility
functionOne( const string& a);    
functionTwo( const Test& t);

These two functions get reference parameter. const definition ensures that parameters cannot be changed. Using const here is OK.

funcionThree(const bool c);
functionOne(const string a);

In this case using const doesn't make sence. Parameters are passed by value, this means, function operates with copy of these parameters. For caller, it doesn't matter whether such parameter is const or not - caller's parameter is not changed in any case.
0
 

Author Comment

by:ambuli
Comment Utility
Thank you.  Now, everything makes more sense :-)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now