Understanding function in C++

A function in C++ is a self-contained block of code that performs a specific task and can be called from other parts of your program. Functions help to make your code modular and reusable, allowing you to break down a complex program into smaller, more manageable parts.


Here is a simple example of a function in C++:

#include <iostream>

using namespace std;

// Function declaration
int addNumbers(int a, int b);

int main() {
    int x = 5, y = 10, sum;
    sum = addNumbers(x, y); // Function call
    cout << "The sum of " << x << " and " << y << " is " << sum << endl;
    return 0;
}

// Function definition
int addNumbers(int a, int b) {
    int result = a + b;
    return result;
}

 

In this example, the addNumbers function takes two integer arguments, a and b, and returns their sum as the result. The function is declared in the program before it is called in the main function, where it is used to calculate the sum of x and y. When the function is called, the values of x and y are passed as arguments, and the result of the function is stored in the variable sum. The result is then displayed on the screen using the cout statement.

 

'C++' 카테고리의 다른 글

Compiling C++ Code on a Mac using the Terminal and GCC  (0) 2023.02.10
Key Features of C++14  (0) 2023.02.10
Lambda Functions in C++  (0) 2023.02.10
Function Pointers in C++  (0) 2023.02.10
Overview of C++11 and C++14 Features  (0) 2023.02.10

Universally Unique Identifier (UUID) 는 리소스들 중에서 리소스를 유니크하게 식별하기 위한 식별자 이다. Javscript 에서 UUID를 생성하기 위한 방법으로 uuid 모듈을 사용하는 방법과 crypto에서 제공하는 raddomUUID() 함수를 사용하는 방법이 있다. 

 

1. UUID 모듈을 사용하여 UUID 생성하기.

 

// 설치하기
npm install uuid

// UUID 생성하기 (ES6 syntax)
import { v4 s uuidV4 } from "uuid"
console.log(uuidV4()); => '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

위에 코드와 같이 랜덤 UUID를 생성할 수 있다, 하지만 uuidV4() 함수를 호출 하기 위해서는 "uuid" 라이브러리를 항상 import 해주어야 하는데, Crypto 라이브러리에 randomUUID() 함수를 사용하게 되면 이러한 부분을 생략할 수 있다.

 

2. Crypto 모듈을 사용하여 UUID 생성하기

console.log(crypto.randomUUID());​

https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID

 

 

Crypto.randomUUID() - Web APIs | MDN

The randomUUID() method of the Crypto interface is used to generate a v4 UUID using a cryptographically secure random number generator.

developer.mozilla.org

 

'Programming > Javascript' 카테고리의 다른 글

Introducing React Hooks  (0) 2023.02.10

+ Recent posts