Why and What of Functions and Function Declaration
Why and What of Functions and Function Declaration
of Functions
and Function
Declaration
Lecture CheckList
1. Introduction to functions.
2. In Javascript, Functions are first-class citizens.
3. Advantages of using functions.
4. When should you consider using Functions?
5. Introduction to the function declaration.
6. The basic syntax for function declaration.
7. Calling a function.
8. Parameters, Arguments & Return statement.
Introduction to functions.
The concept of functions came into existence to support the way we organize and
structure code. A function is a block of code that performs a specific task and can
be reused throughout the program.
This allows us to write more efficient and modular code, as well as the ability to
easily update or change the behavior of the program by modifying the functions.
Additionally, by using functions we can make the code more readable and easier
to understand.
Some functions will be in-built in javascript which can be directly used and some
must be user-defined functions. We will be looking into them in further lectures.
In Javascript, Functions are first-class
citizens.
Functions in JavaScript are first-class citizens, meaning they can be assigned to
variables, passed as arguments to other functions, and returned from functions.
Advantages of using functions.
1. Functions allow us to define a block of code once and reuse it multiple times
throughout your program. So we need not to repeat the same lines of code
every time.
3. Functions can make code more readable by giving it a clear structure and
making it easy to understand what the code is doing.
5. Functions are the optimal way to manage the space which leads to higher
performance of the program.
When should you consider using
Functions?
Functions should be used in a number of situations to take advantage of these
benefits.
One of the main use cases for functions is when you need to perform a specific
task multiple times throughout your code. By defining that task in a function, you
can call it whenever you need to perform that task, rather than duplicating the
code.
Functions can also be used to organize your code into logical units, making it
easier to understand and maintain.
Functions can also be used to improve the readability and maintainability of your
code by giving it a clear structure and making it easy to understand what the
code is doing.
In JavaScript, functions must be declared before they can be used because of the
way the JavaScript engine processes code.
When a function is declared, the JavaScript engine reserves a spot in memory for
it and assigns the function's name to that memory location, so that it can be
called later on.
If a function is called before it is declared, the JavaScript engine will not be able
to find the function and will throw a reference error. So that is why it is important
to declare a function before calling/using it.
● The function body is the block of code that is executed when the function is
called.
Calling a Function.
Calling a function in JavaScript is very simple. Calling a function simply means
executing a block of code that has been declared previously.
The most common way of calling a function is by using the function name
followed by parentheses ().
Parameters, Argument & Return
Statement.
A parameter is a variable that is declared within the parentheses of the function
declaration. It serves as a placeholder or a reference for the input value that will
be passed to the function when it is called.
Any statements that are written after the return statement are not executed.
THANK YOU