One Pager Cheat Sheet
- We will learn about
functions
and their usage in programs, focusing on function parameters, return and call statements. - Functions provide a
reusable
block of code that follows a certain structure, making it easier to organize and reuse code in a program. - By using built-in functions or defining user-defined functions, programmers can create code that allows them to easily perform specific tasks in their program.
- In JavaScript,
functions
are declared using thefunction
keyword, followed by the function name andparenthesis
, with thebody of the function
denoted using curly braces. - We
invoked
afunction
byspecifying
its name andenclosing
it withparenthesis
, executing all the statements inside it. - It is not possible to
call a function
before it isdefined
orimplemented
. - Functions can take in external information through function parameters, which are specified inside the parenthesis after the function name and separated by commas.
- The number of parameters for a function is completely up to the programmer, as they serve as
placeholders
to pass invalues
when thefunction is called
. - A function can
return
avalue
to its caller, as seen in the code block with thesum(a, b, c)
function, which is then stored in theresult
variable and printed to show the calculated sum. - The variable
result
printsundefined
, as the functiondiv
does not have areturn
statement and therefore does not return a value. - The code prints "a is greater than b" to the console and returns 4 as the result, producing a console output of 'a is greater than b', 4.
- You can
reuse
and betterorganize
your code by using functions, however don't forget to call them when using them in your program.