Jump to content
×
×
  • Create New...
  • 0

Function c++


csmmrr04

Question

4 answers to this question

Recommended Posts

  • 0

 return "returns" a value. Simple as that :D

In short: You can use this to create your own functions that return a value. Simple example:

 

int add(int a1, int a2)

{

 return a1 + a2;

}

 

int main()

{

  int result = add(1, 1);

  printf("The result is %d", result);

  return 0; //in main() this tells the system that the program succeeded

  return 1; //in main() this tells the system that the program failed

}

 

Note that return ends the function immediately. That means, that in our example, the return 1; is completely nonsense and will not be reached since the main() function already returned 0.

In int main() it's important to return 0 if the program succeeds (compilers will add that tho if you miss it) and otherweise 1 for program errors.

 

Have fun :D

  • Love 1
Link to comment
  • 0
  • Premium

thanks

So the car can only have values ​​1 and 0 , we understand Yes I saw that in some file is found and return false return true ... Can you explain to that?

 

You could better read a book.

There's two types of return:

int add(int a, int 
{
	return a + b;
}

The above return an int value, as specified in the declaration. You CAN'T return other types.

void test(int a, int 
{
	int c = a + b;
	return;
	int d = c + a;
}

The above return will not return anything and will stop the execution of function. That means that

	int d = c + a;

will not be executed. It will stop at return;

Note: Returning a value also stops the function execution.

 

Returning true or false is the same thing, but the declaration of your function is of type bool:

bool function(int val)
{
	if (val == 2)
		return false;
	else
		return true;
}

or simplier

bool function(int val)
{
	return (val == 2);
}
  • Love 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.