Advertisement

ANOTHER functions question

Started by July 31, 2002 10:54 AM
9 comments, last by SteveBe 22 years, 3 months ago
Hi guys, I''ll try and explain myself well here but please stick with me. When writing functions and naming the variables for the parameters - its it advisable to name them the same as the actual variables you are passing as parameters or should they be named differently? Hope you understand what I mean. The reason I ask is just because I''m finding it easier to name them both the same. Is this ok?
both ways are perfectly acceptable. use whichever one you like more.

if i understand the question correctly, of course.

---
Come to #directxdev IRC channel on AfterNET
Advertisement
I usually name mine the same so that I can keep track of what the variable actually is alot easier.
main
{
ThisFunction(number);
}

void ThisFunction(int number)
{
}


quote: Original post by flukus
I usually name mine the same so that I can keep track of what the variable actually is alot easier.
main
{
ThisFunction(number);
}

void ThisFunction(int number)
{
}




I think what he meant was something more along these lines.

void thisFunction( int num );
void thisFunction( int num )
{
if (num == 0 )
printf( "OOOO" );
else if (num == 1 )
printf( "Ahh!")
}
main
{
int num;
thisFunction( num )
}
--------

one can not do this:


  void thisFunction( int num );void thisFunction( int a ); <---this is wrong.  The declaration (if you have one) must be the same as the definition.( ie: (int num ){// body}main(){  int iVar;  int num;  int a;    // all these calls are valid.  thisFunction( iVar );  thisFucntion( num );  thisFunction( a );}  
- Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
Like2Byte - sorry for nat saying earlier but I meant what the other guys said.
actually, you can have the function prototype be
void foo(int number);

and then define the function like this

void foo(int hi) {

}

you could even have the prototype be:
void foo(int);
if you wanted

you do have to name the variable in the definition of the function however. Otherwise you could never use it
Feel free to email me at NYYanks432@hotmail.com if you have any questions
Advertisement
quote: Original post by GameDev135
actually, you can have the function prototype be
void foo(int number);

and then define the function like this

void foo(int hi) {

}

you could even have the prototype be:
void foo(int);
if you wanted

you do have to name the variable in the definition of the function however. Otherwise you could never use it


You know what? When you''re right, you''re right and when you''re wrong you''re wrong.

I was wrong.

The compiler checks the data types in the definition and not their actual symbols as cross referenced between it''s declaration and definition.


  #include <stdio.h>void foo( int ); // <-- no variable declared. Just it''s data typeint main( void ){	int a = 1, b = 2, iNum = 3;	foo(a);	foo(b);	foo(iNum);	return 0;}void foo( int bar) // <--- Arrg!  A variable!{	switch( bar )	{		case 1:			printf( "1:%i\n", bar  );			break;		case 2:			printf( "2:%i\n", bar  );			break;		case 3:			printf( "3:%i\n", bar  );			break;		default:			printf("Default");			break;	}}  


I was all messed up!

To clear things up with me - is it the same with c++? (When I create my class with some functions declared for the class, that is.)

  class CTest{private: char var1[80]; char var2[80];  int iVal;public:  CTest();  ~CTest();  char* GetSomething( char* );  char* PutSomething( char* );          // <-- same name as \/, different funcs  char* PutSomething( char* , char*  ); // <-- same name as /\, different funcs}//.. constructors and destructors omitted.CTest::char* PutSomething( char* p ){//...body}CTest:: char* PutSomething( char* p, char* s ){//...body}  


Omitting their respective variable names in the declaration is permissable in c++, correct?

I suppose I could save myself some embarressment and just compile it my self to find out....
- Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
yeah, classes work exactly the same way. The only important thing is that you define the type, which makes sense. No reason that they would make it different with classes, particularly since a goal of C++ is to make any program using C style work with it.
Feel free to email me at NYYanks432@hotmail.com if you have any questions
Roger that!

It''s one of those things that gets overlooked when you''ve started to get into a style of doing things. Thanks for keeping me straight!
- Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
How come almost all examples of functions use foo and bar??

This topic is closed to new replies.

Advertisement