What is return type?how can we use it?
What is return type?how can we use it?
Collapse
X
-
The return type is used when you want your function to be an RVAL.
In this code:
The 10 is an RVAL (the value is to the right of the = operator). data is the LVAL.Code:int data = 10;
If the 10 need to be calculated, then you can use a function that returns an int:
The compiler will call the function to get the int to assign to data. Calculate() is now an RVAL.Code:int data = Calculate();
Comment
-
The starting point is with functions: what a function is and how it can be used. Let us know if you have questions about that.
Some languages (such as VisualBasic.NET , Pascal, Fortran, Ada) make a distinction between subroutines and functions. They consider a subroutine to be a callable block of code; while a function is a subroutine that also returns a value.
In C, both are called functions. However, a function that returns void is analogous to a subroutine.
You don't include a return value in a function definition for the fun of it -- you do it if and only if it makes sense to you for a value of some sort to be associated with the function. If you can't think of any useful value to return then define the function to return void.Comment
Comment