Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="description" content="What are function?">
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>Functions</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /* Functions
- Functions are blocks of code designed to perform a task. A function defenition
- consists of a function name, parameters to the function, and statements that define
- the function.
- */
- // 1. Declaration and definition
- function myName(myParameter) { // my function name and parameters in (). This is how we input data.
- return myStatement = myParameter; // my statement
- }
- console.log(myName("argument")); // calling the function and argument for our parameter
- // The parameter in our function is simply a placeholder for our value passed through our argument
- // when we call the function.
- // 2. We can also assign function to variables
- var myFunc = function(str, str2){return str + " " + str2}; // we use return when we want a value out of our function
- console.log(myFunc("Hello", "World"));
- // Function scope and Closure
- // In the example below we show we can access globally scoped variables in our function
- function randomNum() {
- var myNum = Math.floor(Math.random()*10); // This will give me a random number value between 1 and 10
- return myNum; //The variable is in our local scope
- }
- var random = randomNum(); //Value of function stored globaly
- function test(ran){
- newNum = ran + 5;
- return newNum;
- }
- console.log(random);
- console.log(test(random)); //invoking our stored function value to the test function
- console.log(newNum);
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/* Functions
- Functions are blocks of code designed to perform a task. A function defenition
- consists of a function name, parameters to the function, and statements that define
- the function.
- */
- // 1. Declaration and definition
- function myName(myParameter) { // my function name and parameters in (). This is how we input data.
- return myStatement = myParameter; // my statement
- }
- console.log(myName("argument")); // calling the function and argument for our parameter
- // The parameter in our function is simply a placeholder for our value passed through our argument
- // when we call the function.
- // 2. We can also assign function to variables
- var myFunc = function(str, str2){return str + " " + str2}; // we use return when we want a value out of our function
- console.log(myFunc("Hello", "World"));
- // Function scope and Closure
- // In the example below we show we can access globally scoped variables in our function
- function randomNum() {
- var myNum = Math.floor(Math.random()*10); // This will give me a random number value between 1 and 10
- return myNum; //The variable is in our local scope
- }
- var random = randomNum(); //Value of function stored globaly
- function test(ran){
- newNum = ran + 5;
- return newNum;
- }
- console.log(random);
- console.log(test(random)); //invoking our stored function value to the test function
- console.log(newNum);
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement