Introduction to Functions in C

🔧 Introduction to Functions in C

In C programming, a function is a reusable block of code that performs a specific task. Functions help you break down your program into smaller, manageable parts, improve readability, and allow code reuse.

📌 Why Use Functions?

  • Divide and conquer — break large problems into smaller tasks.
  • Code reuse — write once, use many times.
  • Improve readability and maintenance.

🔹 Syntax of a Function in C

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
return_type function_name(parameters) {
// Code to execute
}
return_type function_name(parameters) { // Code to execute }
return_type function_name(parameters) {
    // Code to execute
}

📝 Example: Basic Function That Prints a Message

This function prints a simple greeting when called.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
// Function declaration
void greet() {
printf("Hello from a function!\n");
}
int main() {
greet(); // Function call
return 0;
}
#include <stdio.h> // Function declaration void greet() { printf("Hello from a function!\n"); } int main() { greet(); // Function call return 0; }
#include <stdio.h>

// Function declaration
void greet() {
    printf("Hello from a function!\n");
}

int main() {
    greet(); // Function call
    return 0;
}
  

Try It Now

📝 Example: Function with Parameters

This function takes a name and prints a personalized greeting.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
// Function with parameter
void greet(char name[]) {
printf("Hello, %s!\n", name);
}
int main() {
greet("Coder");
return 0;
}
#include <stdio.h> // Function with parameter void greet(char name[]) { printf("Hello, %s!\n", name); } int main() { greet("Coder"); return 0; }
#include <stdio.h>

// Function with parameter
void greet(char name[]) {
    printf("Hello, %s!\n", name);
}

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

Try It Now

📝 Example: Function That Returns a Value

Here we create a function that adds two numbers and returns the result.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
// Function that returns a value
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(5, 3);
printf("Sum is: %d\n", sum);
return 0;
}
#include <stdio.h> // Function that returns a value int add(int a, int b) { return a + b; } int main() { int sum = add(5, 3); printf("Sum is: %d\n", sum); return 0; }
#include <stdio.h>

// Function that returns a value
int add(int a, int b) {
    return a + b;
}

int main() {
    int sum = add(5, 3);
    printf("Sum is: %d\n", sum);
    return 0;
}
  

Try It Now

📘 Function Structure in C

  • Declaration: Tells the compiler about the function name and return type.
  • Definition: Contains the actual code for the function.
  • Call: Executes the function.

🎯 Recap

  • void means the function doesn’t return a value.
  • Functions can accept arguments and return results.
  • Organize your code using functions for clarity and reuse.

💡 Practice Time!

Try creating functions for tasks like checking even/odd numbers, calculating factorials, or converting units. Functions make your code modular and awesome!