Labs ICT
Pro Login

Functions

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.

Try it Yourself →