0% found this document useful (0 votes)
5K views

Functions

Uploaded by

katarichallenge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5K views

Functions

Uploaded by

katarichallenge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Functions

Function Definition:
A function definition is a block of code designed to perform a particular action.
function sum(a, b){
console.log(a + b);
}
In this case, we don’t get any output. Because we aren’t calling it.
Example:
In the below examples, function is executed when we call it.
function sum(a, b){
console.log(a + b);
}
Sum(2,3); //function call

Output:
5

//compare two values


<html>
<head></head>
<body>
<script>
function comp(a,b){
if(a>b){
console.log(a);
}
else{
console.log(b);
}
}
comp(10,20);

</script>
</body>
</html>

Output:
20
//increment using function
<html>
<head></head>
<body>
<script>
var y=0;
function inc(x){
console.log(y+x);
}
inc(100);
</script>
</body>
</html>

Output:
100

// function definition as output


<html>
<head></head>
<body>
<script>
var x = function abc(a,b){
console.log(a+b);
}
console.log(x);
</script>
</body>
</html>

Output:
ƒ abc(a,b){
console.log(a+b);
}
In this case, function definition is printing because we assigned function
definition to variable x, and then we are printing x in the console.
//ex
<html>
<head></head>
<body>
<script>

function add(x){
console.log(x+100); //output:110
}
var y=add(10); //return value will be stored in y.
console.log(y); // undefined

</script>
</body>
</html>

Output:
Undefined
In this case, there is no return statement so we get undefined.
The another way to write the console when we are returning..
<html>
<head></head>
<body>
<script>

function add(x){
console.log(x+100); //output:110
}
console.log(add(10)); //undefined

</script>
</body>
</html>
Output:
undefined

//ex

<html>
<head></head>
<body>
<script>
function sum(a,b){
return a+b;
}
var p = sum(2,3);
console.log(p); //or
console.log(sum(2,3));

</script>
</body>
</html>
Output:
5
In this case, we have return statement so we get the value.
//ex

<html>
<head></head>
<body>
<script>

function abc(x,y){
if(x>y){
return x;
}
else{
return y;
}
abc(5,4);

</script>
</body>
</html>
In this case the function is returning the value but we are not printing
anything.

//ex

<html>
<head></head>
<body>
<script>

function abc(x,y){
if(x>y){
return x;
}
}
var q=abc(2,3);
console.log(q);

</script>
</body>
</html>
Output:
undefined
In this case, condition is false so it will not execute that block of code. So
it returns undefined.
//ex

<html>
<head></head>
<body>
<script>
function abc(x,y){
if(x>y){
return x;
}
}
console.log(abc(5,4));

</script>
</body>
</html>
Output:
5
In this case, condition is true so it executes that block of code. So it
returns the x value.

//ex

<html>
<head></head>
<body>
<script>

function b(x,y){
if(x>y){
return x;
}
else{
return y;
}
}
console.log(b(4,5));
</script>
</body>
</html>
Output:
5
if the condition is true it will execute that block of code, or it will
execute the else block. In this example, the condition is false so it will
execute the else block, then the output will be y value.

//ex

<html>
<head></head>
<body>
<script>

function c(a,b){
if(a>b){
return a;
}
console.log(b);
}
console.log(c(5,4));

</script>
</body>
</html>
Output:
5
In this case, the condition is true so it executes that block of code.so it
was returning the value of a, and then it will stop the function.so
console.log(b) will not executed.

//ex

<html>
<head></head>
<body>
<script>

function c(a,b){
if(a>b){
return a;
}
console.log(b);
}
c(4,5);

</script>
</body>
</html>
Output:
5
In this case, the condition is false so it will not return anything.so it will
not stop the function or next lines.

So we get the value of b in the console.

//ex

<html>
<head></head>
<body>
<script>

function d(a,b){
if(a>b){
return a;
}
console.log(b);
}
d(4,3);

</script>
</body>
</html>

In this, the condition is true so it will executes that block of code. In if,
we are returning value of a, but we are not printing it. and if there is
return statement, it will stop the function and it will stop console.log(b).So
we don’t get any output.

//ex

<html>
<head></head>
<body>
<script>

function d(a,b){
if(a>b){
return a;
}
console.log(b);
}
var f = d(7,4);
console.log(f);

</script>
</body>
</html>
Output:
7
In this case, the condition is true so it executes that block of code.so it
was returning the value of a, and then it will stop the function and it will
not execute the console.log(b)
//ex

<html>
<head></head>
<body>
<script>

function d(a,b){
if(a>b){
console.log(a);
}
return(b);
}
console.log(d(4,5));

</script>
</body>
</html>
Output:
5
In this case, the condition is false so it will not execute that block. And
then we have return statement so we are returning value of b.

//ex

<html>
<head></head>
<body>
<script>

function d(a,b){
if(a>b){
console.log(a);
}
return(b);
}
console.log(d(5,4));

</script>
</body>
</html>
Output:
5
4
In this case, the condition is true so it will executes that block of code, so
we get value of a in the console. and then we are printing the return value of
b in the console.
So we get output:5

//ex

<html>
<head></head>
<body>
<script>

function d(a,b){
if(a>b){
console.log(a);
}
return(b);
}
d(4,5);

</script>
</body>
</html>

In this code, the condition is false so it will not execute that block, and
then we are returning b value. We are not printing b value so we don’t get any
output.

//ex

<html>
<head></head>
<body>
<script>

function d(a,b){
if(a>b){
console.log(a);
}
return(b);
}
d(5,4);

</script>
</body>
</html>
Output:
5
In this code, the condition is true so it will executes that block, so we get
value of a in the console. And then we are returning b value, but we don’t get
b value because we are not printing it.

So we get output 5.

You might also like