Problem Solving using C Language

0 of 76 lessons complete (0%)

Functions

Return Expression

return Expression in C

In C programming, the return statement is used to exit a function and optionally return a value to the calling function. The return expression defines the value or result that is passed back to the calling function. The expression is evaluated, and its value is returned as the function’s output.

Syntax of return Statement:

return expression;
  • expression: This is the value that will be returned to the calling function. It can be a constant, a variable, or a complex expression. If a function doesn’t return any value, the expression is omitted, and void is used as the return type.

Example of return Expression:

1. Returning Simple Expressions

In this example, the function returns the sum of two integers:

#include <stdio.h>

// Function declaration
int add(int, int);

int main() {
    int x = 5, y = 10;
    int result = add(x, y);  // Function call
    printf("Sum: %d\n", result);
    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;  // Return expression: 'a + b' is evaluated and returned
}
  • Return Expression: a + b is evaluated to return the sum of the two parameters a and b.
  • Returned Value: The sum is returned to the calling function (main), and stored in the variable result.

2. Returning Complex Expressions

You can also return more complex expressions that involve multiple operations:

#include <stdio.h>

float calculate(float, float, float);

int main() {
    float a = 10.0, b = 5.0, c = 2.0;
    float result = calculate(a, b, c);  
    printf("Result: %.2f\n", result);
    return 0;
}

float calculate(float x, float y, float z) {
    return (x + y) * z / (x - y); 
}
  • Return Expression: (x + y) * z / (x - y) is a complex expression involving multiple arithmetic operations.
  • Returned Value: The result of the expression is returned to main() and assigned to result.

3. Returning Boolean Values

You can return a Boolean-like value (represented by 0 or 1 in C) based on conditional logic:

#include <stdio.h>

int isEven(int);

int main() {
    int number = 4;
    if (isEven(number)) {
        printf("The number is even.\n");
    } else {
        printf("The number is odd.\n");
    }
    return 0;
}

int isEven(int num) {
    return num % 2 == 0; 
}
  • Return Expression: num % 2 == 0 is a conditional expression. It returns 1 (true) if the number is even, and 0 (false) if odd.
  • Returned Value: The result (1 or 0) is returned based on the condition.

4. Using return in void Functions

#include <stdio.h>

void printMessage(void);

int main() {
    printMessage();  
    return 0;
}

void printMessage(void) {
    printf("This is a message.\n");
    return;  
}
  • Return Expression: There is no return expression because the function is void.
  • Return Statement: The return; simply exits the function.

5. Returning Pointers

Functions can also return pointers as return values:

#include <stdio.h>

int* findMax(int*, int*);

int main() {
    int a = 10, b = 20;
    int* max = findMax(&a, &b);  
    printf("Max: %d\n", *max);
    return 0;
}

int* findMax(int* x, int* y) {
    if (*x > *y) 
    {
        return x;  
    } else 
    {
        return y;  
    }
}
  • Return Expression: The expression returns the pointer to the larger value between *x and *y.
  • Returned Value: The function returns the memory address of the larger value.

Multiple Return Statements:

A function can have multiple return statements, typically used in conditional logic. However, once a return statement is encountered, the function execution stops, and the value is returned.

#include <stdio.h>

int checkNumber(int num);

int main() {
    int number = 5;
    int result = checkNumber(number);  // Function call
    printf("Result: %d\n", result);
    return 0;
}

int checkNumber(int num) {
    if (num > 0) {
        return 1;  
    } else if (num < 0) {
        return -1; 
    } else {
        return 0; 
    }
}
  • Multiple Return Statements: Based on the condition (num > 0, num < 0, num == 0), different return values (1, -1, 0) are provided.

Summary of Return Values and Types:

  1. Return Value: The value a function sends back to the caller after its execution. It can be a constant, a variable, or an expression.
  2. Return Type: Specifies the data type of the value returned by the function (int, float, char, void, etc.).
  3. Void Functions: Functions with a void return type don’t return any value.
  4. Multiple Return Statements: A function can have multiple return statements, but it will only return once per execution.