Function Definition in C

Level 4116 keystrokes

Defines a function that calculates the square of a number and calls it from main.

C code

// Define a function that takes an int and returns an int
1int square(int n) {
// Calculate and return the square of n
2 return n * n;
3}
4
// Main function - program entry point
5int main() {
// Call square(5) and print result: 25
6 printf("%d\n", square(5));
// Call square(12) and print result: 144
7 printf("%d\n", square(12));
// Return 0 to indicate successful execution
8 return 0;
9}

Output

25
144
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • (× 6
  • )× 6
  • ;× 4
  • "× 4
  • ,× 2
  • {× 2
  • }× 2
  • \× 2
  • %× 2
  • *× 1

More C examples at level 4

← C typing practice