Labs ICT
🎓 SIWES/IT Login

Functions

1 min read | C++ Tutorial
🎓

Doing SIWES / IT attachment?

Get a structured training programme, a supervised digital logbook, and a completion certificate with LabsICT SIWES / IT.

Explore SIWES / IT Training

Writing Functions

A function is a named block of reusable code. You give it a name, specify what it returns, and call it whenever you need that behavior.

int add(int a, int b) {
  return a + b;
}

int main() {
  int result = add(3, 4);
  cout << result;
  return 0;
}

The return type goes before the function name. Use void if the function doesn't return anything. Parameters go inside the parentheses.

void greet() {
  cout << "Hello!";
}

int main() {
  greet();
  return 0;
}

Call a function by writing its name followed by arguments in parentheses. The function runs and, if it has a return type, sends a value back.