Difference between revisions of "JavaScript/C2/Functions-in-JS/English"
(Created page with "Title of the script:Functions Author: Jayesh Katta Ramalingaiah Domain Reviewer: Novice Reviewer: Keywords: JavaScript, HTML, Function, value, arguments, parameters {...") |
Nancyvarkey (Talk | contribs) |
||
Line 24: | Line 24: | ||
Learning Objectives | Learning Objectives | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
− | |||
− | |||
* '''Functions''' | * '''Functions''' | ||
* Different '''types''' of '''functions''' and | * Different '''types''' of '''functions''' and | ||
* '''Function declarations''' in '''JS''' | * '''Function declarations''' in '''JS''' | ||
− | |||
− | |||
|- | |- | ||
|| Slide: System Specifications | || Slide: System Specifications | ||
|| This tutorial is recorded using: | || This tutorial is recorded using: | ||
− | |||
− | |||
* '''Ubuntu Linux''' OS version 18.04 | * '''Ubuntu Linux''' OS version 18.04 | ||
* '''Visual Studio Code''' version 1.51.1 ('''code editor''') | * '''Visual Studio Code''' version 1.51.1 ('''code editor''') | ||
Line 46: | Line 40: | ||
|| Slide : Pre-requisites | || Slide : Pre-requisites | ||
|| To practice this tutorial, | || To practice this tutorial, | ||
− | |||
− | |||
* You should be familiar with writing and executing''' JS''' files'''.''' | * You should be familiar with writing and executing''' JS''' files'''.''' | ||
* If not, please go through the prerequisite tutorials on this website. | * If not, please go through the prerequisite tutorials on this website. | ||
− | |||
− | |||
|- | |- | ||
Line 57: | Line 47: | ||
|| | || | ||
* The files used in this tutorial are available in the''' Code files''' link on this tutorial page. | * The files used in this tutorial are available in the''' Code files''' link on this tutorial page. | ||
− | * Pls download and extract the | + | * Pls download and extract the files. |
* Make a copy and then use them for practising. | * Make a copy and then use them for practising. | ||
− | |||
− | |||
|- | |- | ||
Line 67: | Line 55: | ||
* A '''function '''is a '''block''' of '''code''' which is defined once and can be called multiple times. | * A '''function '''is a '''block''' of '''code''' which is defined once and can be called multiple times. | ||
* The main advantage of a '''function''' is reusability. | * The main advantage of a '''function''' is reusability. | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Types of Functions | || Slide: Types of Functions | ||
|| The different types of '''functions '''are, | || The different types of '''functions '''are, | ||
− | |||
− | |||
* '''Basic Function''' | * '''Basic Function''' | ||
* '''Parameterized Function''' | * '''Parameterized Function''' | ||
* '''Function as a value''' | * '''Function as a value''' | ||
− | * '''Function as | + | * '''Function as an Argument/Parameter''' |
* '''Function as Property''' | * '''Function as Property''' | ||
* '''Function Arguments''' | * '''Function Arguments''' | ||
Line 86: | Line 70: | ||
|- | |- | ||
− | || Switch to ''' | + | || Switch to '''Visual Studio code editor''' |
− | || Switch to the ''' | + | || Switch to the '''Visual Studio code editor.''' |
|- | |- | ||
Line 168: | Line 152: | ||
|| <nowiki>[Firefox] Click on the </nowiki>'''Console''' tab | || <nowiki>[Firefox] Click on the </nowiki>'''Console''' tab | ||
|| Now click on the '''console tab''' in the '''developer tools.''' | || Now click on the '''console tab''' in the '''developer tools.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 232: | Line 213: | ||
− | If you don’t use '''return keyword''' to | + | If you don’t use '''return keyword''' to '''return''' any value, by default '''undefined '''will be '''returned'''. |
Line 242: | Line 223: | ||
add(); | add(); | ||
− | || | + | ||Only when the '''function '''is '''called''', the '''function block ''' will get '''executed.''' |
− | + | ||
− | + | ||
− | Only when the '''function '''is '''called''', | + | |
Line 258: | Line 236: | ||
− | So, whatever is being returned by the '''add() function''', will get '''logged'''. | + | So, whatever is being '''returned''' by the '''add() function''', will get '''logged'''. |
|- | |- | ||
Line 265: | Line 243: | ||
|- | |- | ||
− | || Switch to ''' | + | || Switch to '''browser''' |
|| Switch back to the '''browser.''' | || Switch back to the '''browser.''' | ||
Line 314: | Line 292: | ||
'''console.log('''add()''');''' | '''console.log('''add()''');''' | ||
− | || And '''true '''is printed because '''add() | + | || And '''true '''is printed because '''add() function’s return value''' is '''logged.''' |
|- | |- | ||
Line 343: | Line 321: | ||
− | The only difference is we are passing a '''parameter a'''. | + | The only difference is we are '''passing''' a '''parameter a'''. |
Line 353: | Line 331: | ||
return a - 1; | return a - 1; | ||
− | || Here, we are subtracting the value and returning the subtracted value. | + | || Here, we are subtracting the value and '''returning''' the subtracted value. |
|- | |- | ||
Line 360: | Line 338: | ||
var result <nowiki>= sub(6);</nowiki> | var result <nowiki>= sub(6);</nowiki> | ||
− | || In this line, the '''function call''' is made | + | || In this line, the '''function call''' is made '''passing 6''' as the '''parameter'''. |
− | The returned value is stored in the '''result variable'''. | + | The '''returned''' value is stored in the '''result variable'''. |
|- | |- | ||
Line 386: | Line 364: | ||
'''5''' | '''5''' | ||
− | || The output is '''5 '''because we have passed the number '''6''' as the '''parameter'''. | + | || The output is '''5 '''because we have '''passed''' the number '''6''' as the '''parameter'''. |
− | That value is being subtracted by 1 and returned. | + | That value is being subtracted by 1 and '''returned'''. |
− | After returning, we are catching the value in a '''variable''' and '''logging''' it. | + | After '''returning''', we are catching the value in a '''variable''' and '''logging''' it. |
|- | |- | ||
Line 441: | Line 419: | ||
− | If we wish to, we can even pass some '''parameters '''to this. | + | If we wish to, we can even '''pass''' some '''parameters '''to this. |
|- | |- | ||
Line 447: | Line 425: | ||
return true; | return true; | ||
− | || This '''function '''just | + | || This '''function '''just '''returns true'''. |
|- | |- | ||
Line 454: | Line 432: | ||
var result = mul(); | var result = mul(); | ||
− | || The '''function call '''is similar to the basic | + | || The '''function call '''is similar to the basic and''' parameterized '''ones. |
− | The returned value would be caught by the '''result variable.''' | + | The '''returned''' value would be caught by the '''result variable.''' |
|- | |- | ||
Line 481: | Line 459: | ||
'''true''' | '''true''' | ||
− | || Here, as the '''function | + | || Here, as the '''function returns true, "True" '''is printed on the '''console'''. |
|- | |- | ||
Line 549: | Line 527: | ||
} | } | ||
− | || Here, we have declared three '''functions '''with names''' d1, d2 and d3''' respectively. | + | || Here, we have '''declared''' three '''functions '''with names''' d1, d2 and d3''' respectively. |
|- | |- | ||
Line 559: | Line 537: | ||
− | To this, we are passing 3 '''parameters '''named '''fn, fn1 '''and''' fn2.''' | + | To this, we are '''passing''' 3 '''parameters '''named '''fn, fn1 '''and''' fn2.''' |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 573: | Line 548: | ||
fn2(); | fn2(); | ||
− | || Inside, the '''display function''',we’re making '''function | + | || Inside, the '''display function''',we’re making '''function calls''' for the '''parameters'''. |
Line 583: | Line 558: | ||
display(d1, d2, d3); | display(d1, d2, d3); | ||
− | || While making the '''display function call, '''we’re passing 3 '''functions d1, d2 '''and '''d3''' respectively. | + | || While making the '''display function call, '''we’re '''passing''' 3 '''functions d1, d2 '''and '''d3''' respectively. |
− | Here, as we’re | + | Here, as we’re '''passing d1,d2 '''and '''d3''', these '''functions''' would be called '''callback functions.''' |
|- | |- | ||
Line 600: | Line 575: | ||
− | On making the '''function call''' for '''fn,fn1''' and''' fn2, ''' | + | On making the '''function call''' for '''fn, fn1''' and''' fn2, ''' |
− | '''d1,d2 '''and''' d3 functions '''will be executed. | + | '''d1, d2 '''and''' d3 functions '''will be executed. |
|- | |- | ||
Line 624: | Line 599: | ||
'''on JS''' | '''on JS''' | ||
− | || Here, if we see the output '''Spoken Tutorials on JS '''is being logged from the '''d1 | + | || Here, if we see the output '''Spoken Tutorials on JS '''is being '''logged''' from the '''d1, 'd2''' and '''d3 functions'''. |
|- | |- | ||
Line 668: | Line 643: | ||
}, | }, | ||
− | || Here, '''marks '''is a '''property''' in the '''student | + | || Here, '''marks '''is a '''property''' in the '''student object''', which holds an '''anonymous function.''' |
|- | |- | ||
Line 675: | Line 650: | ||
return 100; | return 100; | ||
− | || This '''function''' | + | || This '''function returns''' 100. |
|- | |- | ||
Line 682: | Line 657: | ||
var result = student.marks(); | var result = student.marks(); | ||
− | || To make a '''function call''', we need to call the '''property '''inside the '''student object'''. | + | || To make a '''function call''', we need to '''call''' the '''property '''inside the '''student object'''. |
Line 688: | Line 663: | ||
− | To make a '''function | + | To make a '''function call''' we add '''brackets '''similar to a '''function''' as a value |
Line 694: | Line 669: | ||
− | The returned value is stored in '''variable result. ''' | + | The '''returned''' value is stored in '''variable result. ''' |
|- | |- | ||
Line 715: | Line 690: | ||
'''100''' | '''100''' | ||
− | || Here, the output is 100 as the '''function returns''' 100 | + | || Here, the output is 100 as the '''function returns''' 100. |
|- | |- | ||
Line 747: | Line 722: | ||
marks(87, 78, 90, 81); | marks(87, 78, 90, 81); | ||
− | || So far, when we | + | || So far, when we '''passed parameters '''to a '''function''', we '''declared''' a '''parameter variable '''while creating the '''function'''. |
Line 760: | Line 735: | ||
function marks() {} | function marks() {} | ||
− | || And | + | || And we have not '''declared''' any '''variables '''to '''catch''' the values '''passed''' in the '''marks function'''. |
|- | |- | ||
Line 807: | Line 782: | ||
'''81''' | '''81''' | ||
− | || As we are '''passing | + | || As we are '''passing 87, 78, 90, 81''' as '''parameters, '''the output we get is '''87, 78, 90, 81.''' |
|- | |- | ||
Line 833: | Line 808: | ||
console.log(s2); | console.log(s2); | ||
− | || Next, let us understand the '''constructor | + | || Next, let us understand the '''constructor function'''. |
Line 921: | Line 896: | ||
|- | |- | ||
|| Only narration | || Only narration | ||
− | || Hence, we conclude that the '''constructor function''' | + | || Hence, we conclude that the '''constructor function returns''' an '''object.''' |
Line 936: | Line 911: | ||
|| Slide''': '''Summary''' ''' | || Slide''': '''Summary''' ''' | ||
|| In this tutorial, we have learnt about: | || In this tutorial, we have learnt about: | ||
− | |||
− | |||
* '''Functions''' | * '''Functions''' | ||
* Different types of '''functions''' and | * Different types of '''functions''' and | ||
* '''Function declarations''' in '''JS''' | * '''Function declarations''' in '''JS''' | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Assignment | || Slide: Assignment | ||
|| As an assignment: | || As an assignment: | ||
− | |||
− | |||
* Open the file '''assignment.js''' which you have created earlier | * Open the file '''assignment.js''' which you have created earlier | ||
* Clear the existing code | * Clear the existing code | ||
Line 958: | Line 927: | ||
* Open the file '''MyPage.html '''in a '''web''' '''browser'''. | * Open the file '''MyPage.html '''in a '''web''' '''browser'''. | ||
* Observe the output in the '''browser’s console''' | * Observe the output in the '''browser’s console''' | ||
− | |||
− | |||
|- | |- | ||
Line 966: | Line 933: | ||
* The video at the following link summarises the Spoken Tutorial project. | * The video at the following link summarises the Spoken Tutorial project. | ||
* Please download and watch it | * Please download and watch it | ||
− | |||
− | |||
|- | |- | ||
Line 974: | Line 939: | ||
* We conduct workshops using spoken tutorials and give certificates. | * We conduct workshops using spoken tutorials and give certificates. | ||
* For more details, please write to us. | * For more details, please write to us. | ||
− | |||
− | |||
|- | |- |
Revision as of 21:23, 17 January 2021
Title of the script:Functions
Author: Jayesh Katta Ramalingaiah
Domain Reviewer:
Novice Reviewer:
Keywords: JavaScript, HTML, Function, value, arguments, parameters
|
|
Slide: Title | Hello and Welcome to the spoken tutorial on “Functions in JS”. |
Slide:
Learning Objectives |
In this tutorial, we will learn about:
|
Slide: System Specifications | This tutorial is recorded using:
However you may use any other browser of your choice. |
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Functions |
|
Slide: Types of Functions | The different types of functions are,
Let us take an example and understand this better. |
Switch to Visual Studio code editor | Switch to the Visual Studio code editor. |
[Editor]
Open Folder -> Practice-JS |
In the Welcome page, click on the Open folder link at the top left corner.
|
[Editor] Click on
|
In the Explorer panel on the left, under Practice-JS click on the file named index.html |
[Editor] Type:
<head> <title>Functions</title> </head> <body> <script src="main.js"></script> </body> </html> |
In the index.html file, type the code as shown.
|
Press: Ctrl + S | Save the file. |
Press: Alt + L and Alt + O
|
Now press the key combinations Alt + L and Alt + O.
|
Show firefox | The default browser will open automatically and a new tab opens. |
[Firefox] Press Ctrl + Shift + I
|
Now press Ctrl + Shift + I keys together.
|
[Firefox] Click on the Console tab | Now click on the console tab in the developer tools. |
Switch to Editor | Switch back to the editor. |
[Editor] Click on
|
In the Explorer panel on the left, under Practice-JS click on the file named main.js |
[Editor] Type:
console.log(1+2); return true; }
console.log(add()); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
In JS, to create a basic function, we use the function keyword.
|
[Editor] Highlight:
|
Inside the function block, we can have multiple instructions.
|
[Editor] Highlight:
|
Functions always return some value.
|
[Editor] Highlight:
|
Only when the function is called, the function block will get executed.
|
[Editor] Highlight:
|
Here, I have written the add() function inside the console log statement.
|
Press: Ctrl + S | Save the file. |
Switch to browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
true |
Here in the output, we observe:
|
Switch to Editor | Switch back to the editor. |
[Editor] Highlight:
|
The first 3 is printed for the first function call. |
[Editor] Highlight:
|
The second 3 is printed for the second function call. |
[Editor] Highlight:
|
And true is printed because add() function’s return value is logged. |
[Editor] Type:
return a - 1; }
console.log(result); |
Next, let us learn about parameterized function.
|
[Editor] Highlight:
|
Here, we have created a function similar to the basic function.
|
[Editor] Highlight:
|
Here, we are subtracting the value and returning the subtracted value. |
[Editor] Highlight:
|
In this line, the function call is made passing 6 as the parameter.
|
[Editor] Highlight:
console.log(result); |
Here, we are logging the value on the console. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
The output is 5 because we have passed the number 6 as the parameter.
|
Switch to Editor | Switch back to the editor. |
[Editor] Type:
return true; };
console.log(result); |
Next, let us learn about the function as a value.
|
[Editor] Highlight:
|
Here, we have declared a variable with the name mul.
|
[Editor] Highlight:
|
We call function () as an anonymous function.
|
[Editor] Highlight:
return true; |
This function just returns true. |
[Editor] Highlight:
|
The function call is similar to the basic and parameterized ones.
|
[Editor] Highlight:
|
Here, we are logging the result variable. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
Here, as the function returns true, "True" is printed on the console. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
console.log('Spoken'); } function d2() { console.log('Tutorials'); } function d3() { console.log('on JS'); } function display(fn, fn1, fn2) { fn(); fn1(); fn2(); }
|
Next, let us learn about function as a parameter.
|
[Editor] Highlight:
console.log('Spoken'); }
console.log('Tutorials'); }
console.log('on JS'); } |
Here, we have declared three functions with names d1, d2 and d3 respectively. |
[Editor] Highlight:
|
Here, I have created another function named display.
|
[Editor] Highlight:
fn1(); fn2(); |
Inside, the display function,we’re making function calls for the parameters.
|
[Editor] Highlight:
|
While making the display function call, we’re passing 3 functions d1, d2 and d3 respectively.
|
Slide: callback function | Callback function is defined as a function which is passed as a parameter to another function. |
[Editor] Highlight:
|
d1, d2 and d3 will be taken as fn, fn1, fn2 in display function as the parameters.
|
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
Tutorials on JS |
Here, if we see the output Spoken Tutorials on JS is being logged from the d1, 'd2 and d3 functions. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
marks: function () { return 100; }, };
console.log(result); |
Next, let us learn about function as a property.
|
[Editor] Highlight:
var student = {}; |
Here, student is the object. |
[Editor] Highlight:
return 100; }, |
Here, marks is a property in the student object, which holds an anonymous function. |
[Editor] Highlight:
|
This function returns 100. |
[Editor] Highlight:
|
To make a function call, we need to call the property inside the student object.
|
console.log(result); | Here, we are logging the result. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
Here, the output is 100 as the function returns 100. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]); } }
|
Next, let us understand about function arguments.
|
[Editor] Highlight:
|
So far, when we passed parameters to a function, we declared a parameter variable while creating the function.
|
[Editor] Highlight:
|
And we have not declared any variables to catch the values passed in the marks function. |
[Editor] Highlight:
} |
Using the keyword arguments, we can access the parameters being passed.
|
[Editor] Highlight:
|
Here, we are just logging the value in the arguments. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight: 87 78 90 81 |
As we are passing 87, 78, 90, 81 as parameters, the output we get is 87, 78, 90, 81. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
this.name = stuName; this.age = stuAge; }
var s2 = new Student("S2", 11); console.log(s1); console.log(s2); |
Next, let us understand the constructor function.
|
[Editor] Highlight:
|
Here, we have created a function named Student and passing the parameters stuName and stuAge.
|
[Editor] Highlight:
this.age = stuAge; |
Inside the function block we have assigned, this.name as stuName and this.age as stuAge.
|
[Editor] Highlight:
var s2 = new Student("S2", 11); |
Here, we have made the function calls,
|
Only narration | The constructor function creates a new object with help of parameters and returns the object.
|
[Editor] Highlight:
console.log(s2); |
Here, we are logging s1 and s2 |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
Object { name: "S2", age: 11 } |
Here, we see we have 2 objects printed.
|
Only narration | Hence, we conclude that the constructor function returns an object.
|
With this we have come to the end of this tutorial.
| |
Slide: Summary | In this tutorial, we have learnt about:
|
Slide: Assignment | As an assignment:
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
Slide: Forum questions | Pls post your timed queries in this forum |
Slide: Acknowledgement | Spoken Tutorial Project is funded by Ministry of Education (MoE), Government of India |
Slide: Thanks | The script for this tutorial is contributed by Jayesh.
And this is Praveen from IIT Bombay signing off. Thank you for joining. |