Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is it OK to declare same function many times with different parameters in VC++?

e.g:
first function will be :
Function(datatype as long int, length long int, Void * data)

second function will be :
Function(datatype as long int, length long int, Id int, Void * data)

How about declaring same function with same parameters many times but different data types of parameters ?
e.g:
first function will be :
Function(datatype as long int, length long int, Void * data)

second function will be :
Function(datatype as long int, length long int, Const Struct * data)

I am not getting syntax errors, but i want to clarify is this correct way of using?
Posted
Comments
[no name] 31-Mar-14 18:56pm    
Of course you are not getting syntax errors. It's called "function overloading" and like in all things, nothing wrong with it in moderation. http://en.wikipedia.org/wiki/Function_overloading
Member 20 31-Mar-14 19:05pm    
Thanks for the response, but for Question 2 "How about declaring same function with same parameters many times but different data types of parameters ?"
depending on case to case basis it should call 1st or 2nd function. So if i declare both functions in same place and while calling if i say 3rd parameter as "Const Struct * data then will it call 2nd function?
[no name] 31-Mar-14 19:10pm    
That is still function overloading. The compiler is smart enough (99.99999999999999% of the time) to figure which function to call based of the parameters that you are using to call it. That is the whole point.
Member 20 31-Mar-14 19:12pm    
Thanks a lot!!!
Sergey Alexandrovich Kryukov 31-Mar-14 19:52pm    
Not getting syntax errors? Not true.
—SA

1 solution

1) return type is missing; 2) C++ is a case-sensitive language, so "Const" and "Struct" won't compile, 3) … no, that's enough. It's pretty obvious that you are trying to program in C++ without reading even about the basics. No, there is no such thing as miracle.

"Same function with different parameters" is not the same function. This is a set of totally different function which happen to have the same name (which has its convenience), and which one to call is decided by a compiler by the signatures recognized from the call expression, if possible. There is are no problems with declaring, defining and calling such functions.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900