By 06/07/2015 10 Comments

TypeScript Functions Or Methods

Definition Of Functions Or Methods:

  • In object oriented programming generally Methods/functions are called behavior, sometime they are also called actions.
  • They might be declared inside a class so these are class members, codes, methods or functions.
  • In TypeScript a function can also be declared without a class declaration, if this is a case then it must be declared with a keyword “function”.
  • If a function is declared inside a class then it must be declared without a “function” keyword.
  • Actually a method or function is a block of code, which contains logic and performs a particular action or task (perform only one task).
  • A function operates, access and modifies data or fields (variables).
  • In TypeScript the functions might have strongly typing, “any” keyword, optional parameters, default parameters, rest parameter, function callbacks and function overloaded characteristics.

Define Function Types:

There are 2 types of functions:

  • Return Type Functions
  • Void Type Functions (Return Nothing)

Return Type Functions:

  • Return type functions must use keyword return to returns value or expressions.
  • Return function might be assigned to a variable which variable must has the same data type as return type function has.
  • Return type functions are more useful than void type functions because we can do more activities with return type functions.
  • Return keyword can be used in both types of functions (void and return type), return keyword which is used in void type function does not return value, it only causes the termination of function and at the other hand return keyword which is used in return type functions must returns values or expressions to caller of the function. Remember when return statement executes, the compiler bypasses all the code bellow return statement.
  • Return type function can returns any type of data such as arrays, objects, enumerations, etc.

Syntax:

function funcName(param1 : data-type, param2 : data-type) : func-return-type {
    return (expression);
}

function

  • Keyword “function” which declares a function.

funcName

  • Name of a function, any valid meaningful name.

(param1 : data-type, param2 : data-type)

  • Here “param1” and “param2” are local variables separated by comma (,), these are optional and called parameter-list. Notice “: data-type” indicates the type of data of parameters such as number, string, boolean etc.

func-return-type

  • This indicates the return type of a function such as string, number, boolean or void etc. This function-return-type is optional because TypeScript makes return-type function by declaring keyword “return” implicitly. If we do not use keyword “return” the TypeScript makes the return type of a function “void” implicitly. Both keywords (void, number) can also be explicitly specified.

{

  • Starting scope or body of a function.

return (expression)

  • This can be any statement/expression to be returned such as return a+b.

}

  • Ending/closing scope of a function.

Example – 1:

/* A function declaration which has string as its return type and 1 string type parameter. */
function calculatorType(name: string): string {

// return a value through return keyword.
    return name;
};

/* Multiple functions declaration, each function has number as its return type, 2 number type parameters and different name. Function name and its parameter-list are collectively called function signature. */
function add(v1: number, v2: number): number {
    return (v1 + v2);
};

function substract(v1: number, v2: number): number {
    return (v1 - v2);
};

function multiply(v1: number, v2: number): number {
    return (v1 * v2);
};

function divide(v1: number, v2: number): number {
    return (v1 / v2);
};

/* Above functions are called and print. Each function is called through its name. Notice that, 2 numbers add(10, 2) are passed from each function, these numbers are called argument(s) and must be matched with the type of parameters, If they not matched the TypeScript compiler generates a compile-time error. */
document.write("Calculator type: " + calculatorType("Normal calculator.") + " <br/>");
document.write("Addition: " + add(10, 2)+ " <br/>");
document.write("Substraction: " + substract(10, 2) + " <br/>");
document.write("Multiplication: " + multiply(10, 2) + " <br/>");
document.write("Division: " + divide(10, 2));

Example – 2:

/* A function declaration which has “any” as its return type and 1 number type parameter. This function can return any type. */
function displayPi(pi:number): any {
    return pi
};

// Function is assigned to “any” type local variable pi.
var pi: any = displayPi(3.14);

// Print local variable which displays function result.
document.write("PI: " + pi);

Example – 3:

/* A function declaration which has implicitly void as its return type and 1 string type parameter. */
function calculatorType(name: string) {

// print result.
    document.write("Calculator type: " + name + " <br/>");
};

/* Multiple functions declaration, each function has explicitly void as its return type, 2 number type parameters and different name. Function name and its parameter-list are collectively called function signature. */
function add(v1: number, v2: number): void {
    document.write("Addition: " + (v1 + v2) + " <br/>");
};

function substract(v1: number, v2: number): void {
    document.write("Substraction: " + (v1 - v2) + " <br/>");
};

function multiply(v1: number, v2: number): void {
    document.write("Multiplication: " + (v1 * v2) + " <br/>");
};

function divide(v1: number, v2: number): void {
    document.write("Division: " + (v1 / v2));
};

/* Above functions are called. Each function is called through its name. Notice that, 2 numbers add(10, 2) are passed from each function, these numbers are called argument(s) and must be matched with the type of parameters, If they not matched the TypeScript compiler generates a compile-time error. */
calculatorType("Normal calculator");
add(10,2)
substract(10, 2);
multiply(10, 2);
divide(10, 2);

Example – 4, A Mark Sheet Program Where Functions Are Declared In A Class:

class MarkSheet { // a class declaration

// properties declaration of different types.
    studentName: string;
    tsMarks: number;
    aspMarks: number;
    htmlMarks: number;

// parameterized constructor which creates a marksheet object.
    constructor(studentName: string, tsMarks: number, aspMarks: number, htmlMarks: number) {
        this.studentName = studentName;
        this.tsMarks = tsMarks;
        this.aspMarks = aspMarks;
        this.htmlMarks = htmlMarks;
    }

/* function declaration which returns student name. It has string as its return type, empty parameter-list and public access by default. */
    getName():string {
        return (this.studentName);
    }

/* function declaration which returns total marks. It has number as its return type, empty parameter-list and public access by default. */
    totalMarks() : number {
        return 300;
    }

/* function declaration which returns obtain marks. It has number as its return type, empty parameter-list and public access by default. */
    obtMarks():number {
        return (this.tsMarks+ this.aspMarks + this.htmlMarks);
    }

/* function declaration which returns percentage. It has number as its return type, 2 number type parameters and public access by default. */
    percentage(total: number, obtain: number): number {
        return obtain / total * 100.0;

    }
}

/* object creation of mark sheet 1, provide different kinds of information as arguments to its constructor. */
var markSheet1: MarkSheet = new MarkSheet("ABC", 85, 70, 80);

/* Following statements call the different functions of above class through its class reference (reference variable) and also print them. */
document.write("Marksheet 1: <br/>");
document.write("Student name: " + markSheet1.getName() + "<br/>");
document.write("TypeScript marks: " + markSheet1.tsMarks+ "<br/>");
document.write("Asp marks: " + markSheet1.aspMarks + "<br/>");
document.write("Html marks: " + markSheet1.htmlMarks + "<br/>");
document.write("Total marks: " + markSheet1.totalMarks() + "<br/>");
document.write("Obtain marks: " + markSheet1.obtMarks() + "<br/>");
document.write("Percentage: " + markSheet1.percentage(markSheet1.totalMarks(), markSheet1.obtMarks()));

// Following lines creates 2 white spaces.
document.write("<br/>");
document.write("<br/>");

/* object creation of mark sheet 2 from same class, provide different kinds of information as arguments to its constructor. */
var markSheet2: MarkSheet = new MarkSheet("XYZ", 65, 50, 40);

/* Following statements call the different functions of above class through its class reference (reference variable) and also print them. */
document.write("Marksheet 2: <br/>");
document.write("Student name: " + markSheet2.getName() + "<br/>");
document.write("TypeScript marks: " + markSheet2.tsMarks + "<br/>");
document.write("Asp marks: " + markSheet2.aspMarks + "<br/>");
document.write("Html marks: " + markSheet2.htmlMarks + "<br/>");
document.write("Total marks: " + markSheet2.totalMarks() + "<br/>");
document.write("Obtain marks: " + markSheet2.obtMarks() + "<br/>");
document.write("Percentage: " + markSheet2.percentage(markSheet2.totalMarks(), markSheet2.obtMarks()));

Example – 5, Different Kinds Of Functions With The Different Kinds Of Parameters Are Declared In A Class:

class FunctionsDemo {

/* function declaration which returns language. It has string as its return type, 1 parameter and public access by default. */
    getName(language: string): string {

        return language;
    }

/* function declaration which returns language. It has “any” as its return type, 1 parameter and public access by default. */
    funcAnyKeyword(language: any): any {

        return language;
    }

/* function declaration which returns language. It has string as its return type, 1 default parameter syntax and public access by default. */
    funcDefaultParameter(language: string = "TypeScript"):string {
        return language;
    }

/* function declaration which returns language. It has implicitly string as its return type, 1 optional parameter syntax and public access by default. */
    funcOptionalParameter(language?: string) {

        return language;
    }
/* function declaration which returns language. It has implicitly string array as its return type, 1 rest parameter syntax and public access by default. */
    funcRestParameter(...language: string[]) {
        return language[0];
    }

/* function declaration which prints language. It has void as its return type, 1 parameter and public access by default. This function is being used as a call back function. */
    funcPrint(language: string): void {
        document.write("funcCallback(): " + language);
    }

/* function declaration which calls another call back function funcPrint(). It has implicitly void as its return type, 2 parameters and public access by default. */
    funcCallback(callBack: (language: string) => void, pLanguage: string) {
        callBack(pLanguage);
    }
}

// An object creation of above class
var functionsDemo: FunctionsDemo = new FunctionsDemo();

/* Following statements call different functions which are declared in above class and also print them. */
document.write("getName(): " + functionsDemo.getName("TypeScript") + "<br/>");
document.write("funcAnyKeyword(): " + functionsDemo.funcAnyKeyword("TypeScript") + "<br/>");
document.write("funcDefaultParameter(): " + functionsDemo.funcDefaultParameter() + "<br/>");
document.write("funcOptionalParameter(): " + functionsDemo.funcOptionalParameter() + "<br/>");
document.write("funcRestParameter(): " + functionsDemo.funcRestParameter("TypeScript") + "<br/>");
functionsDemo.funcCallback(functionsDemo.funcPrint, "Asp.NET");






Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .