Labs ICT
โญ Pro Login

Functions Introduction

Functions are the building blocks of any real C program. They let you wrap reusable pieces of code into named blocks โ€” call them when you need them, and keep your program organized.

Defining a Function

A function has a return type, a name, parentheses for parameters, and a body. Here's one that returns an integer:


#include 
int add(int a, int b) {
  return a + b;
}
int main() {
  int result = add(3, 4);
  printf("Sum: %d\n", result);
  return 0;
}
    
Try it Yourself โ†’

Return Values and void

If a function doesn't return anything, use void as the return type. main itself returns int โ€” usually 0 means success.


#include 
void greet() {
  printf("Hello, world!\n");
}
int main() {
  greet();
  return 0;
}
    
Try it Yourself โ†’

Function Prototypes

In C, you must declare a function before you use it. A prototype tells the compiler what the function looks like without providing the full body. Define the body later.


#include 
int square(int n);
int main() {
  printf("Square of 5: %d\n", square(5));
  return 0;
}
int square(int n) {
  return n * n;
}
    
Try it Yourself โ†’

๐Ÿงช Quick Quiz

What keyword declares a function that returns nothing?