DWEC

Logo

Desenvolupament web en entorn client (DAW)

View the Project on GitHub jrodr236/DWEC

Funcions

Teoria completa

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>

<p>This example calls a function which performs a calculation, and returns the result:</p>

<p id="demo"></p>

<script>
function myFunction(p1, p2) {
    return p1 * p2;
}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>

</body>
</html>

demo

Sintaxi

function name(parameter1, parameter2, parameter3) {
    code to be executed
}

Diferències amb Java:

L’operador () invoca a la funció

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>

<p>Accessing a function without () will return the function definition instead of the function result:</p>
<p id="demo"></p>

<script>
function toCelsius(f) {
    return (5/9) * (f-32);
}
document.getElementById("demo").innerHTML = toCelsius;
</script>

</body>
</html>

demo