0

I am not sure whether I am framing the question right or not. But please cooperate. In one of the videos from 'Week 2' 'Walkthrough' before starting the main program the 'GetPositiveInt' function was mentioned as 'int GetPositiveInt(void);'

in another video 'PrintName' function was mentioned before the mail program as 'void PrintName(string name)'.

Are both of the declaration different? If so, what is the difference between them and what purpose do they serve best?

0

The data type left of the function's name, is the return value of the function, that is the type of the value the function returns. Inside the parentheses are the types, and the names, of the arguments of the functions, that is the types and the names of the variables that you want to pass to the function to do something with.

For example, the GetPositiveInt has a return type of int because it returns an int (the positive integer as the function's name suggests) while it doesn't have any arguments, that is what void means, because you don't have to pass it something to work. It just reads it from the user's input.

The PrintName on the other hand, has a return type of void, because it doesn't return something to the user, it just prints it to the screen, but has an argument of type string because it is that string that the program will print to the string.

Let's see one more function example.

int multiply(int number1, int number2)
{
    return number1 * number2;
}

That is a silly function but will serve our cause. The function multiply takes in two int arguments, number1 and number2. And it returns an int, that is the multiplication between the two integer it was given. Hope that answers your question.

Note: You can have as many input arguments as you want, but you can always return only one value.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .