I have a simple question, because I don’t understand the functionality correctly.
Having the code like that:
int function(a, b)
{
return a*b;
}
it is clear for me that if a and b are int then it returns the result.
However having such:
int function1(arg1, arg2)
{
//something
if (B)
{
//do something
return;
}
arg1[0] = variable1;
arg1[1] = variable2;
arg2 = variable3;
return;
}
I want to name the interfaces, so inputs and outputs, and put the function body into the ‘blackbox’. Inputs are those that are arguments of the function, am I correct? Then outputs is integer array arg1 and the integer arg2, is that right? If so, how can input be output, or if I’m wrong how to identify it?
Also, what happens if B is true, at the return point? Does a function1 return nothing? If so, why is not void type?
sorry for a little bit chaos and for such funny example, but thanks to that I’m gonna be able to understand the concept.
This code is actually invalid C++:
It is invalid because function parameters must have type, and
aandbdon’t have a type specified. This would be valid:If you want to design a function that can accept parameters of unspecified type, you can use templates for that:
This will work for any type (such as
int) so long as that type makes sense when used withoperator*as witha*b.. For example,std::stringwon’t work.In C++, all functions that are declared to return a type must return that type at every return point. That makes this code also invalid:
You cannot return
voidfrom a function declared to return anint. If you need to “escape” from a function that is declared to return something, you can throw an exception:However, given the context of this question I suspect this is not what you’re looking for, and you should consider exceptions to be an advanced topic for now. these are not the droids you’re looking for.