[CSI] Class Computer Usage

Mikhail Nesterenko mikhail at cs.kent.edu
Wed Feb 28 13:17:03 EST 2018


> when using return statements you did an example in class where you were
> able to end the function with just return (not with anything else). When
> would that be possible to do? wouldn't you have to have what you want it to
> return with it? I may just be remembering it wrong.
> 

The syntax of a return statement is this:

    return expression; 

The type of the expression has to match the return type of the
function stated in the function head. For example:

	 int func(){ // return type is integer
	     cout << "Hello, World" << endl;
	     return 55+8; // expression type is integer
	 }

The semantics of the return statement execution is that the return
expression is evaluated, its value substitutes the function
invocation. For example, the above function may be invoked as:

	 int i;
	 i = func() + 105;

In this case the return-expression evaluates to 55+8= 63. This value
substitutes the invocation func() in the expression, which then
evaluates to 63 + 105 = 168. This value is assigned to variable i.

A special case are void-functions that do not return the value. In
this case, a return statement may still be used. It may not have an
expression and its semantics is immediately terminate the function and
return the execution back to the caller function. For example:

       void funcExample(int n){
           if (n < 0) 
	      return;
	   else
	      cout << "Hello, World" << endl;
       }

In this case, if the parameter value is less than zero, the function
immediately ends.

Thanks,
--
Mikhail


More information about the cs13001 mailing list