Problem Solving using C Language

0 of 77 lessons complete (0%)

Functions

Return values and their types

In C, a return value is the value that a function sends back to the part of the program that called it after completing its task. The return type specifies the type of data that a function returns. If a function does not return a value, the return type is void.

Types of Return Values:

  1. Integer Return Type (int): A function can return an integer value.
  2. Floating Point Return Type (float/double): A function can return a floating-point number.
  3. Character Return Type (char): A function can return a single character.
  4. Void Return Type (void): If a function does not return any value, the return type is void.
  5. Pointer Return Type (int*, char*, etc.): A function can return a pointer to a specific data type.

Syntax for Returning a Value:

return expression;

#include <stdio.h>

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

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

// Function definition
int add(int a, int b) {
    return a + b;  // Returning the sum of a and b
}
  • Return type: int because the function add() returns an integer.
  • Return value: The sum of the two integers a and b.

2. Floating-Point Return Type (float)

#include <stdio.h>

float divide(float, float);

int main() {
    float result = divide(9.0, 3.0);  
    printf("Quotient: %.2f\n", result); 
    return 0;
}

float divide(float a, float b) {
    return a / b; 
}
  • Return type: float because the function returns a floating-point number.
  • Return value: The division result of a and b.

3. Character Return Type (char)

#include <stdio.h>

char getGrade(int);

int main() {
    int score = 85;
    char grade = getGrade(score);  
    printf("Grade: %c\n", grade);  
    return 0;
}

char getGrade(int marks) {
    if (marks >= 90) {
        return 'A';
    } else if (marks >= 80) {
        return 'B';
    } else if (marks >= 70) {
        return 'C';
    } else {
        return 'F';
    }
}
  • Return type: char because the function returns a single character.
  • Return value: The grade based on the score.

4. Void Return Type (void)

A function with a void return type does not return a value. It performs some operation but doesn’t send anything back to the calling function.

#include <stdio.h>

void printHello(void);

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

void printHello(void) {
    printf("Hello, World!\n");  // No return statement
}
  • Return type: void because the function doesn’t return any value.
  • There is no return value. The function only prints “Hello, World!”

5. Pointer Return Type

A function can return a pointer to a particular data type.

#include <stdio.h>

int* getMax(int*, int*);

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

int* getMax(int* x, int* y) {
    if (*x > *y) {
        return x;  
    } else {
        return y;
    }
}
  • Return type: int* because the function returns a pointer to an integer.
  • Return value: A pointer to the maximum of the two integers.

Multiple Return Statements:

A function can have multiple return statements, typically in cases like conditional logic (if-else or switch-case), but it will only return once when the first return statement is encountered.

int checkEvenOdd(int num) {
    if (num % 2 == 0) {
        return 1;  
    } else {
        return 0; 
    }
}

Functions with No Return Value (void):

  • If a function does not return a value, its return type is void, and there is no need for a return statement unless you just want to exit the function early.
void printMessage(void) {
    printf("This function returns nothing.\n");
    return;  
}

int wrongFunction() {
    return 3.14;  
}

In this case, the return value should be explicitly cast to an integer:

int correctFunction() {
    return (int)3.14;  
}

So basically, return serves two purpose

  • On executing the return statement, it immediately transfers the control back to the calling function.
  • It returns the value present in the parentheses after return, to the calling function.