A function is a block of code that performs a certain specialized task. The benefit is that instead of writing that code over and over again on every web page it is needed, you can simply refer to that block of code(function) and instruct it to perform the task.
For example- sometimes you need to count the number of spaces in paragraphs, and programming is needed to calculate that. There are three ways to achieve this objective
Method # 1 -Write the coding yourself and paste the whole code, everywhere it is required. Lot of repitition.
Method # 2- Write the coding yourself, give that block of code a name and place it in a separate file. Now on any page it is required, call it by its name to perform the required task.
Method # 3- If available, Use a predefined function, those created by others or come pre-loaded with the programming language or software. Have you Noticed the 'Word Count' feature in Microsoft Word? every time you use that, at the backend a function is called.
You will come across many pre built functions in JS and other programming languages. For examples, string functions for calculating lenght of a word.
Example of a mathematical function-A Mathematical Function for calculating power of a number. 8 ^ 3 is the simple instruction you will give, at the backend code will be 8 x 8 x 8 and there will be instructions in the function on how to display result on screen. like instead of simple dispalying result 512, it can be programmed to output 'The answer to your question is 512 my liege!"
Basic Function Types
Built-in Functions- preloaded in javascript interpreters, you don't need to define them.
User defined functions- Ones you can specify by encapsulating some code in a block and giving that block a name.
Our main focus in this chapter will be Custom/User defined Functions
For example- sometimes you need to count the number of spaces in paragraphs, and programming is needed to calculate that. There are three ways to achieve this objective
Method # 1 -Write the coding yourself and paste the whole code, everywhere it is required. Lot of repitition.
Method # 2- Write the coding yourself, give that block of code a name and place it in a separate file. Now on any page it is required, call it by its name to perform the required task.
Method # 3- If available, Use a predefined function, those created by others or come pre-loaded with the programming language or software. Have you Noticed the 'Word Count' feature in Microsoft Word? every time you use that, at the backend a function is called.
You will come across many pre built functions in JS and other programming languages. For examples, string functions for calculating lenght of a word.
Example of a mathematical function-A Mathematical Function for calculating power of a number. 8 ^ 3 is the simple instruction you will give, at the backend code will be 8 x 8 x 8 and there will be instructions in the function on how to display result on screen. like instead of simple dispalying result 512, it can be programmed to output 'The answer to your question is 512 my liege!"
Basic Function Types
Built-in Functions- preloaded in javascript interpreters, you don't need to define them.
User defined functions- Ones you can specify by encapsulating some code in a block and giving that block a name.
Our main focus in this chapter will be Custom/User defined Functions
Aucun commentaire:
Enregistrer un commentaire