berndh
berndh

Reputation: 1445

programming in c++ basic thing

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.

Upvotes: 0

Views: 103

Answers (2)

user1610015
user1610015

Reputation: 6678

If B is true, the function returns before the output array is filled, but you still need to return something because the function has an "int" return type.

As for whether the parameters are input parameters or output parameters, that's up to you. However, the convention in C++ is that input parameters are const (if they are pointers or references), and non-const if they are output paramenters:

int function1(const int* arg1, int* arg2) // arg1 is an in param, arg2 is an out param

Upvotes: 0

John Dibling
John Dibling

Reputation: 101456

This code is actually invalid C++:

int function(a, b)
{
 return a*b;
}

It is invalid because function parameters must have type, and a and b don't have a type specified. This would be valid:

int function(int a, int b)
{
 return a*b;
}

If you want to design a function that can accept parameters of unspecified type, you can use templates for that:

template<class Val>  Val function(Val a, Val b)
{
  return a*b;
}

This will work for any type (such as int) so long as that type makes sense when used with operator* as with a*b.. For example, std::string won'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:

int function1(arg1, arg2)
{
 //something
 if (B)
 {
  //do something
  return;
  }
 arg1[0] = variable1;
 arg1[1] = variable2;
 arg2 = variable3;
 return;
}

You cannot return void from a function declared to return an int. If you need to "escape" from a function that is declared to return something, you can throw an exception:

int function1(arg1, arg2)
{
 //something
 if (B)
 {
  //do something
  throw std::runtime_error("whoops");
  }
 arg1[0] = variable1;
 arg1[1] = variable2;
 arg2 = variable3;
 throw std::runtime_error("whoops 2");
}

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.

Upvotes: 3

Related Questions