954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

procedures and functions

is there any way that one can place code in C# in a procedure or a function? I have code logic which is going to be executed many times over and instead of recoding it twenty times over I want to put them in functions and procedures so that I can just invoke them all the time. The help file only refers to procedures and functions in server 200 or later. is there not someting in c# itself?

quintoncoert
Posting Whiz in Training
266 posts since May 2007
Reputation Points: 160
Solved Threads: 3
 

90% of what you do in C# (or any OO language for that matter) is in functions.

I am not sure exactly what you are asking though - I know you are new to C#. You ask if you can put some repetative code in a function and call it over and over instead of coding it everytime. But coding it every time is writing a function every time and calling that code is calling a function - so it is the same principal in c#.

If you could be a little bit clearer or give some small code of what you are trying to do I will give you a better answer to get you on your way. :)

f1 fan
Posting Whiz in Training
279 posts since Jan 2006
Reputation Points: 26
Solved Threads: 11
 

consider the following c++ code. It contains a function called square wich can be called hundreds of times without the programmer having to code it one hundred times. main calls it only once but main could have called it as often as it wished.

int square( int )

int main()
{
int number;
cin >> number;

cout << square( number ) << endl;

return 0;
}

int square ( int n )
{
return n * n;
}

i would like to know the C# counterpart of that. But i would be calling the square function from a command button on a form. In Visual Basic you can add a new module which can be either a function( i.e. return values ) or a procedure ( not return values ). Then when you click a button in VB you say ...Call procedure... in the event procedure of the button or if you want the output from a function you just assign the function to variable also somewhere in code. How do I do the equivalent in C#?

Thank you very much for your help.

quintoncoert
Posting Whiz in Training
266 posts since May 2007
Reputation Points: 160
Solved Threads: 3
 

You code it exactly like you have written it :)

If you need to call it from a button, then in your button click handler you merely call the function and update the value wherever you need to (i.e. a textfield or whatever).

C# doesn't have "procedures" - only functions. Functions that do not return a value are declared as returning "void", like so

public void someFunction(){
}
Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You