<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shilpa Syal 👩‍💻</title>
    <description>The latest articles on DEV Community by Shilpa Syal 👩‍💻 (@shilpasyal55).</description>
    <link>https://dev.to/shilpasyal55</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F148194%2F6fd769a9-fcb6-4527-96c2-a6e8562499d7.jpg</url>
      <title>DEV Community: Shilpa Syal 👩‍💻</title>
      <link>https://dev.to/shilpasyal55</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shilpasyal55"/>
    <language>en</language>
    <item>
      <title>Javascript Basics: Understanding Function</title>
      <dc:creator>Shilpa Syal 👩‍💻</dc:creator>
      <pubDate>Wed, 07 Aug 2019 16:30:52 +0000</pubDate>
      <link>https://dev.to/shilpasyal55/javascript-basics-understanding-function-3dfm</link>
      <guid>https://dev.to/shilpasyal55/javascript-basics-understanding-function-3dfm</guid>
      <description>&lt;p&gt;This article will explain the basic architecture of function, its parameters, invocation, return values and many more things.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;A function is a block of code designed to perform a particular task and it can be called any number of times.&lt;/em&gt;&lt;/strong&gt; It reduces the redundant code in our program and makes our code modular and efficient.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Key Points of Function&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;A function is only executed when it is called/invoked.&lt;/li&gt;
&lt;li&gt;In JavaScript, functions are first-class objects as they can have properties and methods like an Object.&lt;/li&gt;
&lt;li&gt;The difference between Object and Function is that functions can be called but objects can’t.&lt;/li&gt;
&lt;li&gt;We can pass values to the function and that values can only be used inside the function scope.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Defining a Function&lt;/strong&gt;
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;SYNTAX&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;JavaScript function starts with &lt;code&gt;function&lt;/code&gt; keyword followed by the function name, a list of parameters enclosed in parenthesis &lt;code&gt;(param1,param2...)&lt;/code&gt; and a pair of curly braces &lt;code&gt;{..}&lt;/code&gt; that enclosed the function statements also known as a function body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function welcomeUser(name){
       alert("Hi!! Welcome Back");
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The function name can contain letters, numbers, underscores and dollar signs(mostly written in camel case).&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Function Invocation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To execute the code written inside the function body, we have to invoke or call the function. A function can be invoked by writing the name of the function followed by the parentheses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;welcomeUser();  //will output "Hi!! Welcome Back" 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;As our logic is contained in the welcomeUser() function, we can reuse it as many times as we want.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Final Code
function welcomeUser(){
       alert("Hi!! Welcome Back");
}
welcomeUser();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Function Parameters&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the above example, the &lt;code&gt;welcomeUser()&lt;/code&gt; is a basic function that alerts the &lt;code&gt;”Hi!! Welcome Back”&lt;/code&gt; but you can also pass parameters to add more functionality and logic to the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function welcomeUser(name) {
    alert("Welcome!!" + name );
}
welcomeUser("xyz");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, &lt;code&gt;welcomeUser(name)&lt;/code&gt; function is accepting one parameter called name in parenthesis and this name parameter will behave as a local variable to that function and can be used anywhere in the function.&lt;/p&gt;

&lt;p&gt;In the above example, we are passing a value of "xyz" as an argument to the welcomeUser function, which the function is accessing through name parameter. Now we can use the name parameter anywhere inside the function, which will output the value "xyz".&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Parameters vs Arguments&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;So a lot of us gets confused between the terms parameters and arguments. While they both look very similar yet there is quite a distinction between them. Let's have a look at the below example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let car1 = "audi"
let car2 = "bmw"
function carFunc(param1, param2) {
  console.log(param1, param2);
}
carFunc(car1,car2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Function parameters are variables in the function definition&lt;/strong&gt; and are separated by commas inside the parentheses (). Here &lt;code&gt;param1&lt;/code&gt; and &lt;code&gt;param2&lt;/code&gt; are the two parameters.&lt;/p&gt;

&lt;p&gt;On the other hand, Arguments are values that you passed during the function invocation, "audi" and "bmw" are the two arguments received by the function.&lt;/p&gt;

&lt;p&gt;Arguments(Primitives) are passed to functions by value (copy of the variable is passed to function). If the function changes the value of an argument, this doesn't change the actual variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function changeName(val){
  val = "xyz";       //this will not change the actual variable
}
let name = "abc";
console.log(name);  //"abc"
changeName(name);
console.log(name);  //"abc"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the name variable value is not changed as it is passed by value to the function.&lt;/p&gt;

&lt;p&gt;But, &lt;strong&gt;Objects and Arrays are passed by references i.e their memory location is passed&lt;/strong&gt; and if the function changes the referred object’s properties, that change is visible outside the function and will change the actual object that is passed. Let’s have a look below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function changeName(obj){
   obj.name = "xyz"
}
let person = {
  name: "abc",
  age: 25
}
console.log(person.name) // "abc"
changeName(person);
console.log(person.name) // "xyz"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Function Returning Value&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In JavaScript, functions always return a value. If no return value is specified, the function will return the default value.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If the function is called with a new keyword(Constructor Function), the default value is the value of its this parameter otherwise the default return value is undefined&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum(a,b) {
    let c = a + b;
}
sum(2,3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sum function will return &lt;code&gt;undefined&lt;/code&gt; as expected.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Return Keyword&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You can return value from the function using return keyword. The value that function return is actually returned back to the caller function and it can be then used immediately and can be stored in a variable for further use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum(a,b) {
    return a + b;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;sum&lt;/code&gt; function returns the sum of our two input variables a and b .&lt;br&gt;
We can execute the function and then store the return value to a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let c = sum(2,3);
console.log(c);   // Outputs: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another interesting thing about the return statement is that it stops the function execution immediately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function checkAge(age){
   if(age &amp;gt; 20)
   {
     return true;
     console.log(age);
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the return statement immediately stops the execution of our function and returns true. The line after the return statement console.log(age) is never executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AX-VhbxnfzeODP5Kju_fiKA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AX-VhbxnfzeODP5Kju_fiKA.jpeg" alt="Function basic Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Before you go:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Functions are Objects&lt;/li&gt;
&lt;li&gt;A function will return a default value if the return statement is not provided.&lt;/li&gt;
&lt;li&gt;Parameters are variables in the function definition whereas Arguments are the actual values passed during function invocation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the next article, we will explore different types to define functions and how they work.&lt;/p&gt;

&lt;p&gt;Thank you for Reading &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
