ﮏﯾ ﻡﺎﺠﻧﺍ ﯼﺍﺮﺑ ﻩﺪﺷ ﯽﺣﺍﺮﻃ ﺪﮐ ﺯﺍ ﮎﻮﻠﺑ ﮏﯾ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺎﺗ ﮏﯾ
ﻪﮐ ﺩﻮﺷ ﯽﻣ ﺍﺮﺟﺍ ﯽﻧﺎﻣﺯ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺎﺗ ﮏﯾ
// Function to compute the product of p1 and p2
function myFunction(p1, p2) {
return p1 * p2;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<p>Call a function which performs a calculation and returns the result:</p>
<p id="demo"></p>
<script>
function myFunction(p1, p2) {
return p1 * p2;
}
let result = myFunction(4, 3);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
،ﺩﻮﺷ ﯽﻣ ﻒﯾﺮﻌﺗ function
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺎﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺎﺗ ﮏﯾ
ﺪﺷﺎﺑ ﺭﻻﺩ ﺖﻣﻼﻋ ﻭ ﻂﺧﺮﯾﺯ ،ﺩﺍﺪﻋﺍ ،ﻑﻭﺮﺣ ﻞﻣﺎﺷ ﺪﻧﺍﻮﺗ ﯽﻣ ﻊﺑﺍﻮﺗ ﻡﺎﻧ
:ﺪﻧﺍ ﻩﺪﺷ ﺍﺪﺟ ﻢﻫ ﺯﺍ ﺎﻣﺎﮐ ﺎﺑ ﻪﮐ ﺪﺷﺎﺑ ﯽﯾﺎﻫﺮﺘﻣﺍﺭﺎﭘ ﻡﺎﻧ ﻞﻣﺎﺷ ﺖﺳﺍ ﻦﮑﻤﻣ ﺰﺘﻧﺍﺮﭘ
{} :ﺩﺮﯿﮔﯽﻣ ﺭﺍﺮﻗ ﯼﺮﻓﺮﻓ ﯼﺎﻫﺖﮐﺍﺮﺑ ﻞﺧﺍﺩ ﺭﺩ ،ﻊﺑﺎﺗ ﻂﺳﻮﺗ ،ﺩﻮﺷ ﺍﺮﺟﺍ ﺪﯾﺎﺑ ﻪﮐ ﯼﺪﮐ
function
name(parameter1, parameter2, parameter3) {
// code to be executed
}
ﺖﺳﺍ ﻩﺪﺷ ﺖﺳﺮﻬﻓ ﺭﺩ() ﺰﺘﻧﺍﺮﭘ ﻞﺧﺍﺩ ﺭﺩ ﺎﻫﺮﺘﻣﺍﺭﺎﭘ ﻊﺑﺎﺗ
ﺪﻨﺘﺴﻫ ﺮﯾﺩﺎﻘﻣArguments ﻊﺑﺎﺗ
.ﺪﻨﻨﮐ ﯽﻣ ﺭﺎﺘﻓﺭ ﯽﻠﺤﻣ ﯼﺎﻫﺮﯿﻐﺘﻣ ﻥﺍﻮﻨﻋ ﻪﺑ (ﺎﻫﺮﺘﻣﺍﺭﺎﭘ) ﺎﻫ ﻥﺎﻣﻮﮔﺭﺁ ،ﻊﺑﺎﺗ ﻞﺧﺍﺩ ﺭﺩ
(ﯼﺍﺪﺻ) ﺽﺮﻓ "ﯼﺰﯿﭼ" ﻪﮐ ﺩﻮﺷ ﯽﻣ ﺍﺮﺟﺍ ﯽﻧﺎﻣﺯ ﻊﺑﺎﺗ ﻞﺧﺍﺩ ﺪﮐ
(ﺪﻨﮐ ﯽﻣ ﮏﯿﻠﮐ ﻪﻤﮐﺩ ﯼﻭﺭ ﺮﺑﺭﺎﮐ ﻪﮐ ﯽﻧﺎﻣﺯ) ﺪﻫﺩ ﯽﻣ ﺥﺭ ﺩﺍﺪﯾﻭﺭ ﮏﯾ ﻪﮐ ﯽﻣﺎﮕﻨﻫ
ﺩﻮﺷ ﯽﻣ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺪﮐ ﺯﺍ ﻪﮐ ﯽﻣﺎﮕﻨﻫ
(ﻩﺪﺷ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺩﻮﺧ) ﺭﺎﮐﺩﻮﺧ ﺕﺭﻮﺻ ﻪﺑ
ﺖﺧﻮﻣﺁ ﺪﯿﻫﺍﻮﺧ ﻊﺑﺎﺗ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺩﺭﻮﻣ ﺭﺩ ﯼﺮﺘﺸﯿﺑ ﺕﺎﻋﻼﻃﺍ ﻪﻟﺎﻘﻣ ﻦﯾﺍ ﺭﺩ ﺍًﺪﻌﺑ
،ﺪﺳﺭ ﯽﻣ return
ﺕﺭﺎﺒﻋ ﻪﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺘﻗﻭ
ﺩﺍﺩ ﺪﻫﺍﻮﺧ ﻡﺎﺠﻧﺍ ﺍﺭ ﺭﺎﮐ ﻦﯾﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ،ﺪﺷﺎﺑ ﻩﺪﺷ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺭﻮﺘﺳﺩ ﮏﯾ ﺯﺍ ﻊﺑﺎﺗ ﺮﮔﺍ
ﺖﺳﺍ ﯽﺘﺸﮔﺯﺎﺑ ﺭﺍﺪﻘﻣ .ﺪﻨﻨﮐ ﯽﻣ ﻪﺒﺳﺎﺤﻣ ﺍﺭ ﯽﺘﺸﮔﺯﺎﺑ ﺭﺍﺪﻘﻣ ﺐﻠﻏﺍ ﻊﺑﺍﻮﺗ
:ﺪﯿﻧﺍﺩﺮﮔﺮﺑ ﺍﺭ ﻪﺠﯿﺘﻧ ﻭ ﺪﯿﻨﮐ ﻪﺒﺳﺎﺤﻣ ﺍﺭ ﺩﺪﻋ ﻭﺩ ﺏﺮﺿ ﻞﺻﺎﺣ
// Function is called, the return value will end up in x
let x = myFunction(4, 3);
function myFunction(a, b) {
// Function returns the product of a and b
return a * b;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<p>Call a function which performs a calculation and returns the result:</p>
<p id="demo"></p>
<script>
let x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;
function myFunction(a, b) {
return a * b;
}
</script>
</body>
</html>
ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺩﺪﺠﻣ ﺪﮐ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﻊﺑﺍﻮﺗ ﺎﺑ
.ﺩﻮﺷ ﻩﺩﺎﻔﺘﺳﺍ ﺎﻫﺭﺎﺑ ﺪﻧﺍﻮﺗ ﯽﻣ ﻪﮐ ﺪﯿﺴﯾﻮﻨﺑ ﯼﺪﮐ ﺪﯿﻧﺍﻮﺗ ﯽﻣ
.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺕﻭﺎﻔﺘﻣ ﺞﯾﺎﺘﻧ ﺪﯿﻟﻮﺗ ﯼﺍﺮﺑ ﻒﻠﺘﺨﻣ ﯼﺎﻫ ﻥﺎﻣﻮﮔﺭﺁ ﺎﺑ ﺪﮐ ﮏﯾ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺎﻤﺷ
:ﺪﻨﮐ ﯽﻣ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺍﺭ ﻊﺑﺎﺗ() ﺮﮕﻠﻤﻋ
:ﺱﻮﯿﺴﻠﺳ ﻪﺑ ﺖﯾﺎﻬﻧﺭﺎﻓ ﻞﯾﺪﺒﺗ
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
let value = toCelsius(77);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<p>Invoke (call) a function that converts from Fahrenheit to Celsius:</p>
<p id="demo"></p>
<script>
function toCelsius(f) {
return (5/9) * (f-32);
}
let value = toCelsius(77);
document.getElementById("demo").innerHTML = value;
</script>
</body>
</html>
:ﺪﻫﺩ ﻥﺎﺸﻧ ﺍﺭ ﯽﺘﺳﺭﺩﺎﻧ ﺦﺳﺎﭘ ﺪﻧﺍﻮﺗ ﯽﻣ ﺖﺳﺭﺩﺎﻧ ﯼﺎﻫﺮﺘﻣﺍﺭﺎﭘ ﺎﺑ ﻊﺑﺎﺗ ﮏﯾ ﻪﺑ ﯽﺳﺮﺘﺳﺩ
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
let value = toCelsius();
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<p>Invoke (call) a function to convert from Fahrenheit to Celsius:</p>
<p id="demo"></p>
<script>
function toCelsius(f) {
return (5/9) * (f-32);
}
let value = toCelsius();
document.getElementById("demo").innerHTML = value;
</script>
</body>
</html>
:ﻊﺑﺎﺗ ﻪﺠﯿﺘﻧ ﻪﻧ ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﻊﺑﺎﺗ() ﻥﻭﺪﺑ ﻊﺑﺎﺗ ﮏﯾ ﻪﺑ ﯽﺳﺮﺘﺳﺩ
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
let value = toCelsius;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<p>Accessing a function without () returns the function and not the function result:</p>
<p id="demo"></p>
<script>
function toCelsius(f) {
return (5/9) * (f-32);
}
let value = toCelsius;
document.getElementById("demo").innerHTML = value;
</script>
</body>
</html>
ﻭ ،ﺩﺭﺍﺩ ﻩﺭﺎﺷﺍ ﻊﺑﺎﺗ ﯽﺷ ﻪﺑ toCelsius
،ﺪﯿﻨﮐ ﯽﻣ ﻩﺪﻫﺎﺸﻣ ﻻﺎﺑ ﯼﺎﻫ ﻝﺎﺜﻣ ﺭﺩ ﻪﮐ ﺭﻮﻄﻧﺎﻤﻫ
ﻭ ﻒﯿﻟﺎﮑﺗ ،ﺎﻫ ﻝﻮﻣﺮﻓ ﻉﺍﻮﻧﺍ ﺭﺩ ،ﺪﯿﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﺎﻫﺮﯿﻐﺘﻣ ﺯﺍ ﺎﻤﺷ ﻪﮐ ﯽﺷﻭﺭ ﻥﺎﻤﻫ ﻪﺑ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﻊﺑﺍﻮﺗ
:ﻊﺑﺎﺗ ﮏﯾ ﯽﺘﺸﮔﺯﺎﺑ ﺭﺍﺪﻘﻣ ﻩﺮﯿﺧﺫ ﯼﺍﺮﺑ ﺮﯿﻐﺘﻣ ﮏﯾ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﯼﺎﺟ ﻪﺑ
let x = toCelsius(77);
let text = "The temperature is " + x + " Celsius";
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺮﯿﻐﺘﻣ ﺭﺍﺪﻘﻣ ﮏﯾ ﻥﺍﻮﻨﻋ ﻪﺑ ﺎًﻤﯿﻘﺘﺴﻣ ﺍﺭ ﻊﺑﺎﺗ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺎﻤﺷ
let text = "The temperature is " + toCelsius(77) + " Celsius";
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<p>Using a function as a variable:</p>
<p id="demo"></p>
<script>
let text = "The temperature is " + toCelsius(77) + " Celsius.";
document.getElementById("demo").innerHTML = text;
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
</script>
</body>
</html>
.ﺖﺧﻮﻣﺁ ﺪﯿﻫﺍﻮﺧ ﻊﺑﺍﻮﺗ ﺩﺭﻮﻣ ﺭﺩ ﯼﺮﺘﺸﯿﺑ ﺕﺎﻋﻼﻃﺍ ﺵﺯﻮﻣﺁ ﻦﯾﺍ ﺭﺩ ﺍًﺪﻌﺑ
ﺪﻧﻮﺷ ﯽﻣ ﻞﯾﺪﺒﺗ ﻪﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺎﺗ ﮏﯾ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ ﯼﺎﻫﺮﯿﻐﺘﻣ
ﺪﻨﺘﺴﻫ ﯽﺳﺮﺘﺳﺩ ﻞﺑﺎﻗ ﻂﻘﻓ ﯽﻠﺤﻣ ﯼﺎﻫﺮﯿﻐﺘﻣ
// code here can NOT use carName
function myFunction() {
let carName = "Volvo";
// code here CAN use carName
}
// code here can NOT use carName
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<p>Outside myFunction() carName is undefined.</p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
let text = "Outside: " + typeof carName;
document.getElementById("demo1").innerHTML = text;
function myFunction() {
let carName = "Volvo";
let text = "Inside: " + typeof carName + " " + carName;
document.getElementById("demo2").innerHTML = text;
}
myFunction();
</script>
</body>
</html>
.ﺪﻧﻮﺷ ﻩﺩﺎﻔﺘﺳﺍ ﻒﻠﺘﺨﻣ ﻊﺑﺍﻮﺗ ﺭﺩ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﻥﺎﺴﮑﯾ ﻡﺎﻧ ﺎﺑ ﯽﯾﺎﻫﺮﯿﻐﺘﻣ ،ﺪﻧﻮﺷ ﯽﻣ ﯽﯾﺎﺳﺎﻨﺷ ﺩﻮﺧ ﻊﺑﺍﻮﺗ ﻞﺧﺍﺩ ﺭﺩ ﻂﻘﻓ ﯽﻠﺤﻣ ﯼﺎﻫﺮﯿﻐﺘﻣ ﻪﮐ
.ﺪﻧﻮﺷ ﯽﻣ ﻑﺬﺣ ﻊﺑﺎﺗ ﻞﯿﻤﮑﺗ ﺯﺍ ﺲﭘ ﻭ ﺪﻧﻮﺷ ﯽﻣ ﺩﺎﺠﯾﺍ ﻊﺑﺎﺗ ﮏﯾ ﻉﻭﺮﺷ ﻡﺎﮕﻨﻫ ﯽﻠﺤﻣ ﯼﺎﻫﺮﯿﻐﺘﻣ