Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can somebody tell me the difference between there: (c++)

+9 votes
asked May 8, 2023 by Eidnoxon (5,160 points)

Can somebody tell me the difference between:

void myFunc(int a, int b){...}

and

int myFunc(int a, int b){...}

or the same but with the double, char, etc. keywords. I didn't even knew you can make functions with variable datatypes.

3 Answers

0 votes
answered May 9, 2023 by Caden Fechter (150 points)

The return type of these functions are different.

int myFunc(int a, int b) is required to return a value of type int.

void myFunc(int a, int b) is required to not return any value.

For example:

void myFunc(int a, int b) { return a + b; } // error - cannot return anything

int myFunc(int a, int b) { return; } // error - requires a return value

int myFunc(int a, int b) { return string("hi"); } // error - return value must be of type int, not string

The return type of the function always comes before the name of the function. Returning something from a function will act as a sort of "result" of the function:

#include <iostream>
int myFunc(int a, int b) { return a + b;)

int main() {

std::cout << myFunc(1, 5); }

This code will output 6 to the console, since the function adds its two arguments (which are 1 and 5) and returns them as its result. This result is then printed to the console using std::cout. Functions which have a return type of void must not yield a result. 

If you have any more questions, feel free to ask!

+1 vote
answered May 9, 2023 by Teddy (330 points)
void is the return type, when you put a int return type you will be returning a int,
+1 vote
answered May 9, 2023 by Peter Minarik (87,810 points)
edited May 9, 2023 by Peter Minarik

Let's consider the following toy project:

#include <stdio.h>

int Compare(int a, int b)
{
    return a - b;
}

void PrintComparison(int a, int b)
{
    int result = Compare(a, b);
    if (result < 0)
    {
        printf("%d is less than %d.\n", a, b);
        return;
    }
    
    if (result > 0)
    {
        printf("%d is larger than %d.\n", a, b);
        return;
    }
    
    // result == 0
    printf("%d is equal to %d.\n", a, b);
}

int main()
{
    PrintComparison(1, 2);
    PrintComparison(4, 3);
    PrintComparison(5, 5);

    return 0;
}

When you have a function, it must have a return value. It could be anything, such as an integral number, a floating point number, a string, a pointer, even structures defined by you, or nothing (void). This allows the function to communicate back some kind of result to the called.

In the first function, int Compare(int a, int b), we calculate the difference between two integral numbers and return the result: return a - b. Later on, this returned value is stored: int result = Compare(a, b);

In the second function, void PrintComparison(int a, int b), we have a void return value, so this function will not return anything to the caller. In this function, we print to the standard output what we get back from Compare. You can still call return in this function if you want to exit the function, but no value must be specified. You can see such calls when we check whether result is less or larger than 0.

I hope this clears things up. Good luck!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...