C - Function Designs

Here are some basic C function designs with example source code and illustrations:

1. Function with No Parameters and No Return Value:

#include <stdio.h>

void greet() {
    printf("Hello, world!\n");
}

int main() {
    greet();
    return 0;
}
    
2. Function with Parameters and Return Value:

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(5, 3);
    printf("The sum is: %d\n", result);
    return 0;
}
    
3. Function with Parameters and No Return Value:

#include <stdio.h>

void printRectangle(int length, int width) {
    for (int i = 0; i < length; i++) {
        for (int j = 0; j < width; j++) {
            printf("* ");
        }
        printf("\n");
    }
}

int main() {
    int length = 4;
    int width = 3;
    printRectangle(length, width);
    return 0;
}
    
4. Recursive Function:

#include <stdio.h>

int factorial(int n) {
    if (n <= 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

int main() {
    int num = 5;
    int fact = factorial(num);
    printf("Factorial of %d is %d\n", num, fact);
    return 0;
}
    
5. Function with Default Parameter Value:

#include <stdio.h>

void greetWithMessage(char *message) {
    if (message == NULL) {
        message = "Hello, world!";
    }
    printf("%s\n", message);
}

int main() {
    greetWithMessage(NULL); // Uses the default message
    greetWithMessage("Hi there!"); // Uses the provided message
    return 0;
}