Return Statements and Return Values in Cpp

Return Statements and Return Values

A. Return statement syntax

The return statement exits a function and returns a value (if applicable) to the calling code.

Syntax: return expression;

The expression can be a literal, variable, or an expression that evaluates to the return type specified in the function declaration.

B. Returning values from a function

Functions can return values using the return statement.

The return type in the function declaration specifies the data type of the value to be returned.

The return statement transfers the program control back to the calling code and provides the value to be assigned or used.

C. Void functions and their purpose

Void functions do not return a value; their return type is explicitly mentioned as void.

Void functions are used when the purpose of the function is to perform a task or operation without returning any specific value.

They are useful for procedures that execute actions or modify data without requiring a return value.

D. Examples demonstrating return statements and return values

Example 1: Function returning a value:

int square(int num) {
    return num * num;
}
int main() {
    int result = square(5);
    cout << "Square: " << result << endl;
    return 0;
}

Example 2: Void function without a return statement:

void printMessage() {
    cout << "Hello, world!" << endl;
}
int main() {
    printMessage();
    return 0;
}

Example 3: Void function with a return statement (to exit early):

void checkNumber(int num) {
    if (num < 0) {
        cout << "Negative number found. Exiting function early." << endl;
        return;
    }
    cout << "Number is positive." << endl;
}
int main() {
    checkNumber(-5);
    return 0;
}

Function Prototypes

A. Definition and concept of function overloading

Function overloading refers to the ability to have multiple functions with the same name but different parameters.

It allows programmers to define functions with similar functionality but different parameter types, numbers, or order.

The compiler differentiates between the overloaded functions based on the parameters used in the function call.

B. Advantages and uses of function overloading

Improved code readability and organization:

Function overloading allows programmers to use the same function name for related operations, making the code more readable and intuitive.

It promotes code organization by grouping similar functions under a common name.

Flexibility and convenience:

Function overloading provides flexibility to handle different data types or variations of parameters without creating multiple functions with different names.

It simplifies function calls by allowing the programmer to choose the appropriate function based on the arguments provided.

Code reusability and maintenance:

Function overloading promotes code reusability by allowing the same function name for different functionality variations.

If changes are required in the logic of a function, modifications can be made to the overloaded version, ensuring consistency throughout the codebase.

C. Examples illustrating function overloading

Example 1: Overloading a function with different parameter types:

void display(int num) {
    cout << "Integer number: " << num << endl;
}
void display(float num) {
    cout<< "Floating-point number: " << num << endl;
}
int main() {
    display(5);
    display(3.14f);
    return 0;
}

Example 2: Overloading a function with different number of parameters:

int addNumbers(int num1, int num2) {
    return num1 + num2;
}
int addNumbers(int num1, int num2, int num3) {
    return num1 + num2 + num3;
}
int main() {
    cout << "Sum: " << addNumbers (5, 7) << endl;
    cout << "Sum: " << addNumbers (2, 4, 6) << endl;
    return 0;
}

Example 3: Overloading a function with different parameter order:

void printName (string firstName, string lastName) {
    cout << "Full Name: " << firstName << " " << lastName << endl;
}
void printName (string lastName, string firstName) {
    cout << "Reversed Name: " << lastName << "," << firstName << endl;
}
int main() {
    printName("John", "Doe");  
    printName("Doe", "John");
    return 0;
}

Learn via Video Course

C++ Programming (English) Logo

C++ Programming (English)

2056

18 hrs