Function Example - CS Study Hub

Latest

Monday, September 1, 2025

Function Example

Example 1: Bad Code — Everything Inside main()

This is hard to read, hard to maintain, and not reusable.


#include <stdio.h>

int main() {
    int a, b, sum, product, difference;
    
    // Taking input
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    
    // Sum
    sum = a + b;
    printf("Sum: %d\n", sum);
    
    // Difference
    difference = a - b;
    printf("Difference: %d\n", difference);
    
    // Product
    product = a * b;
    printf("Product: %d\n", product);
    
    // Check even/odd
    if (sum % 2 == 0)
        printf("Sum is even.\n");
    else
        printf("Sum is odd.\n");
    
    return 0;
}

Problems:

  • Everything is jammed inside main().
  • Repetitive or unrelated logic mixed together.
  • Not reusable — you can’t use the same sum logic elsewhere.
  • Difficult to test or update.

Example 2: Good Code — Modular & Clean Using Functions

Here’s a clean, modular version of the same program that demonstrates all 4 types of functions in C:


#include <stdio.h>

// 1️⃣ void with no parameters
void greet() {
    printf("=== Simple Calculator ===\n");
}

// 2️⃣ void with parameters
void printResult(char operation[], int result) {
    printf("%s: %d\n", operation, result);
}

// 3️⃣ return type (non-void) with no parameters
int getNumber() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    return num;
}

// 4️⃣ return type (non-void) with parameters
int add(int x, int y) {
    return x + y;
}
int subtract(int x, int y) {
    return x - y;
}
int multiply(int x, int y) {
    return x * y;
}

// Function to check even/odd
void checkEvenOdd(int n) {
    if (n % 2 == 0)
        printf("%d is even.\n", n);
    else
        printf("%d is odd.\n", n);
}

int main() {
    int a, b, sum, difference, product;

    greet(); // Call void with no parameters

    a = getNumber(); // Call return type with no parameters
    b = getNumber();

    sum = add(a, b);               // return type with parameters
    difference = subtract(a, b);   // return type with parameters
    product = multiply(a, b);      // return type with parameters

    printResult("Sum", sum);           // void with parameters
    printResult("Difference", difference);
    printResult("Product", product);

    checkEvenOdd(sum); // reuse function

    return 0;
}

Explanation:

Function Type Example Function Description
void with no parameters greet() Performs a task (print message) without input or return
void with parameters printResult(char[], int) Prints result using provided arguments
Return type with no parameters getNumber() Takes input and returns an integer
Return type with parameters add(int, int), subtract(), multiply() Returns a computed value using inputs

Benefits of Modular Code:

  • ✅ Easier to read and maintain
  • ✅ Reusable functions
  • ✅ Easier debugging and testing
  • ✅ Cleaner and structured logic

💡 Conclusion

Using functions in C keeps your program modular, clean, and reusable. Even small programs benefit from structure — it makes them easier to extend later!

No comments:

Post a Comment