Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 25th, 2003, 12:53 PM
Registered User
 
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default parameter initilization in functions

I've read that it is important to initialize a variable upon definition, that way the variable will not be assigned memroy space that might contain junk values. So, for example, in a function like 'int text(int x, int y)', are x and y automatically initialized to zero? Do I have to initialize them, or the compiler takes care of it?

Reply With Quote
  #2 (permalink)  
Old July 26th, 2003, 01:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Hi Nigor

Firstly the signature of the function is such that
the caller of the function will HAVE to provide
values for both of the paramerters, or the function call
will give error at compilation. still, to be on the safer side
give default values to the parameters like this
int text(int x, int y = 0)

this declaration lets the caller call the the function
without supplying the value for the second parameter yet
the parameter will get initialized with zero.
but it all depends whether you want to let anybody
call your function that way.

Write back if there is anything else you wanna ask.


Ankur Verma
.Net and C++ Specialist
Wiley Tech Support
Reply With Quote
  #3 (permalink)  
Old July 29th, 2003, 04:50 PM
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi nigor,

Quote:
quote:I've read that it is important to initialize a variable upon definition, that way the variable will not be assigned memroy space that might contain junk values.
That is good practice.

Quote:
quote:So, for example, in a function like 'int text(int x, int y)', are x and y automatically initialized to zero...
No they are not. When you declare a function like:

int text(int x, int y)

you are not defining x and y, so you're first statement doesn't apply. In addition, I don't recommend Ankur_Verma's recommendation to provide default values--providing default values and intializing your variables are two different subjects.

x and y are the parameters of your function, and they don't need initializing. You should think of them as place holders for the data you are going to send the function. For instance your function might be defined like this:
Code:
int text(int x, int y)
{
   int result = 0;
   result = x + y;
   return result;
}
You would call that function like this:

int a = 10;
int b = 20;

text(a, b);

Then the value for a will be substituted for x, and the value of b will be substituted for y in the function.
Reply With Quote
  #4 (permalink)  
Old July 29th, 2003, 11:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Hi 7stud

I appreciate your reviewing both the query and the
answer, I did call It 'initializing' in my answer,
thanks for pointing that out.

Providing default values is a good programming practice
itself, when you have 0s or NULLs or ""s assigned to the
parameters when no specific value has been provided for
them, it is always easier to use them in conditions etc.
as in that case, the user defining varibales, not
initializing them and passing them to the function call
just to match the signature of the call and in the
process provide junk values to the function- is one less
thing you have to worry about.
Again it all depends how you want your function to be
called.

Welcome to the group, its discerning people like you
who make ideas like this forum a success.


Ankur Verma
.Net and C++ Specialist
Wiley Tech Support
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
C functions...??? ethantinder C# 2 April 25th, 2008 02:25 AM
Parameter object malfunction - out parameter dash dev C# 2005 6 December 4th, 2007 12:58 PM
Functions LTello BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 3 September 15th, 2006 11:15 AM
Functions Meg Intro Programming 1 January 23rd, 2006 01:03 PM
Help with Functions megabytes C# 1 August 7th, 2003 12:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.