Monday, May 21, 2007

Functions

Define Functions?
Functions in C# are a means of providing blocks of code that can be executed at any point in an application.
· Functions have an advantage of making your code more readable.
· You can use this function from any point in your code, and use the same lines of code in each case. The function can be thought of as containing reusable code.
· Functions can also be used to create multipurpose code, allowing them to perform the same operation on varying data.
· You can supply a function with information to work in the form of parameters, and obtain results from functions in the form of return values. The parameters and return value of a function collectively define the signature of a function.
Example: you can supply an array to search as a parameter and obtain the maximum value in the array as a return value. This means that you can use the same function to work with a different array each time.

Function definition consists of the following:
· Two keywords, static and void
o Note: the void keyword is to indicate the function does not return a value
Example:
Static void Write()
{
……………
}
· Function name followed by parentheses, Ex: Write()
· A block of code to execute enclosed in curly braces
· Function names are usually written in PascalCasing.

When the Function returns a value, you have to modify the function in two ways:
· Specify the type of the return value in the function declaration instead of using the void keyword.
· Use the return keyword to the end of the function execution and transfer the return value to the calling code.
Example:
Static ()
{
……………
return ;
}

Example:
Static double GetVak()
{
return 3.2;
}
Note: The only limitation here is that must be a value that is either of type or can be implicitly converted to that type.

Note: The return values are usually the result of some processing carried out by the function. When the return statement is reached, program execution returns to the calling code immediately. No lines of code after this statement will be reached

Note: Placing return in a for loop, an if block, or any other structure causes the structure to terminate immediately and the function to terminate.

Note: The return statement is processed before reaching the closing } of the function.

When a Function is to accept the parameters, you must specify the following:
· A list of the parameters accepted by a function in its definition, along with the types of those parameters
· A matching list of parameters in each function call.
Example:
Static ( , …)
{
……………
return
}

Note: you can have any number of parameters, each with a type and a name. The parameters are separated using commas. Each of these parameters is accessible from code within the function as a variable.
Example:
Static double Product (double param1 double param2)
{
return param1 * param2
}

Parameter Matching:
When you call a function, you must match the parameters as specified in the function definition exactly. This means matching the parameter types, the number of parameters, and the order of the parameters.
Example:
Static coid MyFunction(string myString, double myDouble)
{
}
This function can’t be called as follows:
MyFunction (2.6, “Hello”);
You also can’t use as follows:
MyFunction (“Hello”);
Note: Here you are attempting to pass a double value as the first parameter and a string value as the second parameter, which is not the order in which the parameters are defined in the function definition.

Parameters Arrays:
C# allows you to specify one (and only one) special parameter for a function. This parameter is known as a parameter array. Parameter arrays allow you to call functions using a variable amount of parameters and are defined using the params keyword.
Parameter arrays can be a useful way to simplify your code, because you don’t have to pass arrays from your calling code. Instead, you pass several parameters of the same type that are placed in an array that you can use from within your function.
Example:
Static ( , ………., params [ ] )
{
…..
return ;
}
You can call the function using code like:
(, ………, , , ……)
Here , ,….. are values of type , which are used to initialize the array. The number of parameters that you specify here is almost limitless; the only restriction is that they are all of type . You can even specify no parameters at all.

No comments: