๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“• STUDY/JavaScript

[JavaScript] ๊ธฐ์ดˆ๋ฌธ๋ฒ•-5 (ํ•จ์ˆ˜ / ํ™”์‚ดํ‘œ ํ•จ์ˆ˜)

[JavaScript] ๊ธฐ์ดˆ๋ฌธ๋ฒ•-5 (ํ•จ์ˆ˜ / ํ™”์‚ดํ‘œ ํ•จ์ˆ˜)


โœ… ํ•จ์ˆ˜

 

function ํ•จ์ˆ˜๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜){
    ํ•จ์ˆ˜์—์„œ ์‹คํ–‰ํ•  ๋ฌธ์žฅ;
}
 
์žฅ์ 
- ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐ˜๋ณตํ•ด์„œ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†์–ด ์ค‘๋ณต์„ ์ค„์—ฌ์คŒ.
- ์ฝ”๋“œ์˜ ์œ ์ง€ ๋ณด์ˆ˜๊ฐ€ ์‰ฌ์›€. (์ผ๋ถ€ ํ•จ์ˆ˜๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋จ)
 

 

1. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜
1-1. 'error'๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
function showError(){
  console.log("error");
}

showError();
 
 
2. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ํ•จ์ˆ˜
2-1. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ด๋ฆ„์„ ๋ฐ›์•„ 'Hello, ์ด๋ฆ„'์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
function sayHello(name){
  const msg = `Hello, ${name}`;
  console.log(msg);
}

sayHello('Hyunjin');  // Hello, Hyunjin
sayHello('Mike'); // Hello, Mike
sayHello('Jane'); // Hello, Jane
โ–ถ ์ค‘๋ณต์„ ์ค„์—ฌ์คŒ
 

 

2-2. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด 2-1 ์ถœ๋ ฅ, ์•„๋‹ˆ๋ฉด 'Hello'๋งŒ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
function sayHello(name){
  let msg = 'Hello';
  if(name){
    msg = `Hello, ${name}`;
  }
  console.log(msg);
}

sayHello(); // Hello
sayHello('Hyunjin'); // Hello, Hyunjin
 

 

2-3. 2-2์˜ ์ฝ”๋“œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ˆ˜์ •
function sayHello(name){
  let msg = 'Hello';
  if(name){
    msg += ', ' + name;
  }
  console.log(msg);
}

sayHello(); // Hello
sayHello('Hyunjin'); // Hello, Hyunjin
๐Ÿ“› ์ฃผ์˜์‚ฌํ•ญ ๐Ÿ“›
msg ๋Š” sayHell() ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ์“ฐ์ด๋Š” ์ง€์—ญ๋ณ€์ˆ˜ โ–ถ ํ•จ์ˆ˜ ๋ฐ”๊นฅ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์˜ค๋ฅ˜๋ฐœ์ƒ
 
 
2-4. ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์œ ๋ฌด์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
function sayHello(name){
  let newName = name || 'friend';
  let msg = `Hello, ${newName}`;
  console.log(msg);
}

sayHello(); // Hello, friend 
sayHello('Jane'); // Hello, Jane

โœ… ์ „์—ญ๋ณ€์ˆ˜์™€ ์ง€์—ญ๋ณ€์ˆ˜

- ์ „์—ญ๋ณ€์ˆ˜ : ์ „์ฒด ์˜์—ญ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
- ์ง€์—ญ๋ณ€์ˆ˜ : ์„ ์–ธ๋œ ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

1. ์ „์—ญ๋ณ€์ˆ˜์™€ ์ง€์—ญ๋ณ€์ˆ˜ msg๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
let msg = "Hello"; // ์ „์—ญ๋ณ€์ˆ˜
console.log(msg); // 1

function sayHello(name){
    let msg = "Welcome" // ์ง€์—ญ๋ณ€์ˆ˜
    console.log(msg + ' ' + name); // 2
}

sayHello('Hyunjin'); // 3
console.log(msg);

// ๊ฒฐ๊ณผ
/*
1) Hello 
2) Welcome Hyunjin
3) Hello 
*/
 
2. ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ ์–ธ๋œ msg ์ˆ˜์ •ํ•ด์„œ ์ถœ๋ ฅํ•˜๊ธฐ
let msg = "Hello";
console.log("ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ „"); // 1
console.log(msg); // 2

function sayHello(name){
    console.log("ํ•จ์ˆ˜ ๋‚ด๋ถ€"); // 3
    if(name){
        msg += `, ${name}`;
    }
    console.log(msg); // 4
}

sayHello();
sayHello('Hyunjin');
console.log("ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„"); // 5
console.log(msg); // 6

// ๊ฒฐ๊ณผ
/*
1) ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ „
2) Hello
3) ํ•จ์ˆ˜ ๋‚ด๋ถ€
4) Hello
3) ํ•จ์ˆ˜ ๋‚ด๋ถ€
4) Hello, Hyunjin
5) ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„
6) Hello, Hyunjin
*/

โœ… ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ธฐ๋ณธ ๊ฐ’ ์„ค์ •ํ•˜๊ธฐ

 

1. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด name์„ ์•„๋‹ˆ๋ฉด default ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
function sayHello(name = 'friend'){
  let msg = `Hello, ${name}`;
  console.log(msg);
}

sayHello(); // Hello, friend
sayHello('Jane'); // Hello, Jane
 
๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ default ๊ฐ’์„ ์„ค์ •ํ•˜๋ฉด, ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด์˜ค์ง€ ์•Š์„ ๋•Œ ํ•ด๋‹น ๋ณ€์ˆ˜์— default ๊ฐ’์„ ์ €์žฅํ•จ!

โœ… ๋ฐ˜ํ™˜ํ˜• ํ•จ์ˆ˜

๋ฐ˜ํ™˜?

return์„ ํ†ตํ•ด ๊ฐ’์„ ํ•จ์ˆ˜ ๋ฐ–์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ

 

1. ๋ฐ˜ํ™˜ํ˜• โŒ ํ•จ์ˆ˜
function showError(){
    AudioListener('์—๋Ÿฌ ๋ฐœ์ƒ');
}
๐Ÿ“› ์ฃผ์˜ ๐Ÿ“›
return๋ฌธ์ด ์—†๋‹ค๊ณ  ํ•ด์„œ ์•„๋ฌด ๊ฐ’๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์€ ์•„๋‹˜!
ํ•ญ์ƒ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•จ!

 

 

2. ๋ฐ˜ํ™˜ํ˜•์ด ์žˆ๋Š” ํ•จ์ˆ˜
function showError(){
    AudioListener('์—๋Ÿฌ ๋ฐœ์ƒ');
    return;
}

 

์—ฌ๊ธฐ์„œ return์€ ํ•จ์ˆ˜ ์ข…๋ฃŒ์˜ ์˜๋ฏธ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Œ!
 
 
3. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ˆซ์ž ๋‘๊ฐœ๋ฅผ ๋ฐ›์œผ๋ฉด, ๋‘ ์ˆ˜์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
function add(num1, num2){
  return num1 + num2;
}
const result = add(2,3);
console.log(result); // 5
๐Ÿ’ก ํ•จ์ˆ˜ ์ด๊ฒƒ๋งŒ ์ƒ๊ฐํ•˜์ž! 
ํ•จ์ˆ˜๋Š” ํ•œ๊ฐ€์ง€ ๋™์ž‘๋งŒ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ!
ํ•จ์ˆ˜์˜ ์ด๋ฆ„์€ ๋™์ž‘๊ณผ ๊ด€๋ จ์ด ์žˆ์–ด์•ผ ํ•จ!

โœ… ํ•จ์ˆ˜์˜ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์„ ์–ธ ๋ฐฉ๋ฒ•

 

1. ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ : ์–ด๋””์„œ๋“  ํ˜ธ์ถœ ๊ฐ€๋Šฅ
sayHello();

function sayHello(){ // ํ˜ธ์ด์ŠคํŒ… - ๋ฐ‘์—์„œ ์„ ์–ธํ–ˆ์ง€๋งŒ ๋ฒ”์œ„๊ฐ€ ์œ„๊นŒ์ง€ ๊ฐ€๋Šฅ
    console.log('Hello');
}

// Hello
ํ˜ธ์ด์ŠคํŒ…์ด๋ž€?
์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์„ ์–ธ ์ „์— ๋ฏธ๋ฆฌ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ
์ฆ‰, ํ•จ์ˆ˜ ์„ ์–ธ๋ณด๋‹ค ํ˜ธ์ถœ์„ ๋จผ์ €ํ•ด๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฒƒ!
(๋‹จ let, const์—๋Š” ์ ์šฉ ๋ถˆ๊ฐ€๋Šฅ)
 

 

2. ํ•จ์ˆ˜ ํ‘œํ˜„์‹ : ์ฝ”๋“œ์— ๋„๋‹ฌํ•ด์•ผ์ง€๋งŒ ์‹คํ–‰ ๊ฐ€๋Šฅ
let sayBye = function() {
  console.log('Bye');
}
sayBye(); // Bye

 

๋งŒ์•ฝ ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ์ฒ˜๋Ÿผ ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ๋จผ์ €ํ•œ๋‹ค๋ฉด?

sayHi(); // ์˜ค๋ฅ˜ ๋ฐœ์ƒ
let sayHi = function() {
  console.log('Hi');
}
์˜ค๋ฅ˜ ๋ฐœ์ƒ์œผ๋กœ ์‹คํ–‰์ด ์•ˆ๋จ!

 

3. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
 
3-1. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜
let showError = () =>{
    console.log("error");
}

 

3-2. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ํ•จ์ˆ˜

let add = (num, num2) =>{
    num1 + num2;
};

 

3-3. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•˜๋‚˜์ผ ๊ฒฝ์šฐ ( ) ์ƒ๋žต๊ฐ€๋Šฅ

let sayHello = name => `Hello, ${name}`

 

3-4. return๋ฌธ์ด ํ•œ ์ค„์ผ ๊ฒฝ์šฐ ์ถ•์•ฝ ๊ฐ€๋Šฅ

let add = (num, num2) => num1 + num2;

โœ… ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ๋‹ค์–‘ํ•œ ์˜ˆ์ œ

ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๋ฐ”๊ฟ”๋ณด๊ธฐ

 

1. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ด๋ฆ„์„ ๋ฐ›์•„์„œ 'Hello, ์ด๋ฆ„'์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜

1) ํ•จ์ˆ˜ ํ‘œํ˜„์‹

const sayHello = function(name){
  const msg = `Hello, ${name}`;
  console.log(msg);
};
sayHello('hyunjin') // Hello, hyunjin
 
2) ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
let sayHello = name => {
  const msg = `Hello, ${name}`;
  console.log(msg);
};
sayHello('Hyunjin'); // Hello, hyunjin

 

 

2. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ˆซ์ž ๋‘๊ฐœ๋ฅผ ๋ฐ›์•„ ๋‘ ์ˆ˜์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

1) ํ•จ์ˆ˜ ํ‘œํ˜„์‹

const add = function(num1, num2){
  const result = num1+num2;
  return result;
}
console.log(add(1, 2)); // 3
 
2) ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
let add = (num1, num2) => {
  const result = num1 + num2;
  return result;
}
console.log(add(1,2)); // 3

 

3) ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ

console.log(add(1, 2)); // 3
function add(num1, num2){
  return num1 + num2;
}