Thursday, March 4, 2010

Methods or User Defined function- Sleek and beautiful

Methods in many programming languages are actually the Functions or Subroutines, are often referred to as 'methods'. Different programming languages provide different idea how a User Defined Function should invoked. Some programming language instead of UDF it is called as Subroutine. These are both refer to as Methods or User defined functions (UDF).

Function - a function is a sequence of commands or programming code that returns a value. A function that is available through a simple reference and specification of arguments in a given higher-level programming language. Also known as built-in procedure; intrinsic procedure; standard function.

Subroutine - a SUB is a sequence of commands or programming code, but it does NOT return a value. Subroutine used in Visual Basic, Visual Foxpro.

In Object Oriented Programming languages - A method is an object's abilities. Methods or functions are the modules from which java program is built. Method can be declared with -

access-specifier, modifier, type, method-name (parameter list), where access specifer is optional. Methods also known as functions. access-specifier – private/ public/protected.

Note that Interface method visibility is public by default. You do not need to specify the access modifier it will default to public. For clarity it is considered a good practice to put the

public keyword. The same way all member variables defined in the Interface by default will become static final once inherited in a class.

Access modifiers
You surely would have noticed by now, the words public, protected and private at the beginning of class's method declarations used, these keywords are called the access modifiers.

protected keyword is a modifier that can be used in the declaration of methods or variables. A protected variable or method is only visible within its class, within its subclasses, or within the class package.

private keyword is a modifier that can be used in the declaration of methods or variables. Using the private modifier in the declaration for either of these types hides the methods and variables so they cannot be directly referenced outside of the class they're declared in. One exception to this rule is that private methods or variables declared within a class can also be used by inner classes.

public keyword is a modifier that can be used in the declaration of classes and interfaces. The public keyword can also be used as a modifier in the declaration of methods and variables. Classes or interfaces declared as public are visible everywhere. Methods and variables declared as public are visible everywhere their corresponding classes are visible.

(Note that the protected, public, private keyword cannot be used in the declaration of local variables.)

type – Type is compulsory for a method declaration. Type declaration indicates that what types of values going to be returned.

void (no return) or int / float / double / Boolean / char / String –(if a function return a value it not compulsory to accept a parameter form caller method).

parameter list are variables or objects that receive the value of argument.

Function Call by value – When a simple type passed to a method, as a copy of the argument is being made and passed to the function is known as call by value, any changes made to the parameter inside the function do not affect the original. A functions parameter list also known as signature.

void test()
{
int a=10,b=20;
int s=sum(a, b);
}

Function call by reference – When an object, address of a variable or a pointer is passed as an argument to a method it is called call by reference. A reference to an argument is passed as parameter, the value of the argument is not passed. This method is called call by reference in java.

class testing
{
int a, b;
void test()
{
a=10,b=20;
testing t=new testing();
int s=sum(t);
}
}

Pure function – Pure function or accessor methods return information to the caller about a state of an object without changing the state.

Impure function – Pure function or mutator methods return information to the caller about a state of an object by changing the state.

actual parameter—the actual value that is passed into the method by a caller. Actual arguments (those supplied by the caller) are evaluated. Caller – sum(a, b)

formal parameter—the identifier used in a method to stand for the value that is passed into the method by a caller. Receiver – int sum(int x, int y)

Message passing - "The process by which an object sends data to another object or asks the other object to invoke a method. Also known to some programming languages as interfacing

static - The static keyword is used as a modifier for methods and variables. When the static keyword appears in a method or variable declaration, it means that there will be only one copy of the method or variable that each class object may reference, regardless of the number of instances of the containing class that are created.

Examples
Basic

1 DEF fnf (t) = 1.5 * t ^ 2 + 3.2 * t ^ 2 + 2.3 * x + 4.1 :'Function
10 CLS
20 x = 1
30 WHILE x < 2.5 40 y = fnf(x) : ' Function calling 50 PRINT y 60 x = x + .5 70 WEND 80 END

C / C++
//main program
void main()
{
int c;
c=sum(a,b);
printf(“%d”,c);
}

//sum functions
int sum(int a,int b)
{
return a+b;
}
Java //main program
public class
{
public static void main(String args[])
{
int c;
c=sum(a,b);
System.out.println(c);
}

//sum functions
int sum(int a,int b)
{
return a+b;
}

Hope you have a helluva of time attending this function, next week come back with Built-in Functions.

!!!These functions are really very heavily built, that’s why very helpful too, till then Sizobonana!!!

No comments: