<?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: ashwinib935</title>
    <description>The latest articles on DEV Community by ashwinib935 (@ashwinib935).</description>
    <link>https://dev.to/ashwinib935</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%2F920254%2F1e7c67b6-0a90-4a47-ab92-ac93e30a9aa5.png</url>
      <title>DEV Community: ashwinib935</title>
      <link>https://dev.to/ashwinib935</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashwinib935"/>
    <language>en</language>
    <item>
      <title>Functions in Javascript</title>
      <dc:creator>ashwinib935</dc:creator>
      <pubDate>Sat, 03 Sep 2022 16:39:41 +0000</pubDate>
      <link>https://dev.to/ashwinib935/functions-in-javascript-49m7</link>
      <guid>https://dev.to/ashwinib935/functions-in-javascript-49m7</guid>
      <description>&lt;p&gt;A function is a block of code that performs a specific task.&lt;br&gt;
In simple word function is a set of statements that perform some task on input which we provide and return an output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of Using a Function&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function makes the code reusable. You can declare it once and use it multiple times.&lt;/li&gt;
&lt;li&gt;Function makes the program easier as each small task is divided into a function. Dividing a complex problem into smaller chunks makes your program easy to understand and reusable.&lt;/li&gt;
&lt;li&gt;Function increases readability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Declaring a Function&lt;/strong&gt;&lt;br&gt;
The syntax to declare a function is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function functionName () {
    // function body   
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A function is declared using the function keyword.&lt;br&gt;
The basic rules of naming a function are similar to naming a variable. The body of function is written within {}.&lt;/p&gt;

&lt;p&gt;Example-1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function welcome(userName) {
   console.log("Welcome",userName);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Calling a Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the above program, we have declared a function named welcome(). To use that function, we need to call it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//calling function
welcome("Ashwini");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are calling function by their name i.e. welcome&lt;/p&gt;

&lt;p&gt;Example-2&lt;br&gt;
A program to add two numbers and return the addition.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//calling function
add(3,5);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above program the add function is used to sum of two numbers.&lt;br&gt;
The function is declared with two parameters a and b.&lt;br&gt;
The function is called using its name and passing two arguments 3 and 5.&lt;br&gt;
We can call a function as many times as we want. We can write one function and then call it multiple times with different arguments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Return&lt;/strong&gt;&lt;br&gt;
The return statement can be used to return the value to a function call.&lt;/p&gt;

&lt;p&gt;The return statement denotes that the function has ended. Any code after return is not executed.&lt;/p&gt;

&lt;p&gt;If nothing is returned, the function returns an undefined value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Expressions&lt;/strong&gt;&lt;br&gt;
Such a function can be anonymous; it does not have a name.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Function expressions in JavaScript are not hoisted, unlike function declarations. We can't use function expressions before We create them. When a function is created at the same time it is called. Function expressions are invoked to avoid polluting the global scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(sum); // undefined
//  even though the variable name is hoisted, the definition isn't. so it's undefined.

sum(); // TypeError: sum is not a function

var sum = function () {
  console.log('bar');
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
