Home Tutorials C Tutorial User-Defined Functions
User-Defined Functions

User-Defined Functions


The 4 Variations of User-Defined Functions

In C, functions communicate with the rest of the program in two ways: through Arguments (input) and Return Values (output). Based on this, we can structure functions in four different ways.

1. No Argument and No Return Value

The function is self-contained. it does not receive any data from the caller and does not send any data back. It usually just performs a fixed task like printing a message.

Syntax: void functionName(); Example:
void sayHello() {
    printf("Hello Students!\n");
}

2. With Argument and No Return Value

The caller sends data to the function, but the function does not send anything back. This is useful for displaying specific results calculated inside the function.

Syntax: void functionName(parameters); Example:
void printSquare(int n) {
    printf("Square is: %d", n * n);
}

3. No Argument and With Return Value

The function takes no input but performs a task and sends a result back to the caller. Often used for getting user input or generating a constant value.

Syntax: data_type functionName(); Example:
int getAge() {
    int a = 20;
    return a;
}

4. With Argument and With Return Value

The most flexible type. It takes inputs, processes them, and returns the result. This is the standard way of writing functions in professional software development.

Syntax: data_type functionName(parameters); Example:
int addNumbers(int x, int y) {
    return x + y;
}

💡 Pro Tip for Students

Always prefer Type 4 (With Argument and Return) whenever possible. It makes your functions "independent," meaning they don't depend on global variables, making your code much easier to test and reuse in other projects!

🏋️ Test Yourself With Exercises

Take our quiz on User-Defined Functions to test your knowledge.

Browse Quizzes »