Constructor

G

Gondal

Hello !!
i hava confusion about Constructor is function or procedure with
respect to c/c++
bcos we generaly we define .... function return value but procedure....
plz argu. that!!
 
P

Paulus de Boska

A ctor is not a function or, rather, method, hence it never has a
return type. Same in C++.
So you can't even compare it to a method returning void.
A ctor is a ctor, a special code block executed only once when an
instance is created.
 
R

Roedy Green

i hava confusion about Constructor is function or procedure with
respect to c/c++
bcos we generaly we define .... function return value but procedure....
plz argu. that!!

it is neither fish nor fowl. Constructors are their own thing. If you
look at one on the byte code level it is just some code that
initialises fields in an object, so it is closest to an instance
method that returns no value. The constructor does not allocate ram.
It gets passed the ram for the new object pre-zeroed.


see http://mindprod.com/jgloss/contructor.html
 
Z

zero

Hello !!
i hava confusion about Constructor is function or procedure with
respect to c/c++
bcos we generaly we define .... function return value but procedure....
plz argu. that!!

Like the others have said, a constructor is neither. As an aside, I
personally think the distinction between function and procedure is
nonsensical in languages like C/C++ & Java. Functions that don't return
anything are just that - functions with a void return type. IMO the
different names stem from different languages (eg pascal uses procedures),
not from a real functional difference.
 
R

Ranganath Kini

Gondal said:
Hello !!
i hava confusion about Constructor is function or procedure with
respect to c/c++
bcos we generaly we define .... function return value but procedure....
plz argu. that!!

The Constructor is a special method in a Java class. It is special
because unlike other methods in Java, a class's constructor cannot be
inherited/overridden by a subclass.

Also like C/C++, Java asserts that all methods declare to return
something, or declare to return a void. But constructors get a special
treatment, in that they are not required to declare to return anything.

Hope it helps!
 
O

Oliver Wong

zero said:
As an aside, I
personally think the distinction between function and procedure is
nonsensical in languages like C/C++ & Java. Functions that don't return
anything are just that - functions with a void return type. IMO the
different names stem from different languages (eg pascal uses procedures),
not from a real functional difference.

The different names comes from Math. Math defines a function as a
mapping between two sets, called the "domain" and "range". For every element
in its domain (the input), it must map to exactly one element in the range
(the output). Thus, technically, something which "returns" two values is not
considered a function (though it could return a single element, where that
element it itself a set or list or something with cardinality greater than
one; this is like returning a Vector<String> in Java -- you're not returning
multiple strings; you're returning a single vector of strings).

Functions, according to the mathematical definition, should have not
side effects. A function shouldn't "print to the console" or anything like
that; it should just, given an input, return an output.

Also note that functions shouldn't have state, nor return something
different given the same input.

"Sine" and "Cosine" are considered to be functions in Math.

A procedure, on the other hand, comes from the English term, meaning a
way of doing something. There is nothing implying that a value should be
returned or anything like that. "Place the key in the ignition, and turn
clockwise to start the engine" is a procedure, for example.

According to the Java Language Specification, Java doesn't have
"functions" or "procedures". They have "methods".

- Oliver
 
S

Stefan Ram

Oliver Wong said:
Functions, according to the mathematical definition, should have not
side effects. A function shouldn't "print to the console" or anything like
that; it should just, given an input, return an output.

Even in programming languages, functions do not have effects.

Only the /evaluation/ of a function application might have
an effect.

This also happens in mathematics: For example, the evaluation
of the function "sin( 0 )" by a person might have the side effect
that he misses his train, because the evaluation takes time.

The difference is that in mathematics the effect
of the evaluation is not /specified/ by the function. While the
evaluation of "sin( 0 )" might have the effect that someone
misses a train, this is not specified for "sin" to happen.

In programming, some effects of evaluations of an operation
invocation are specified for the operation.
Also note that functions shouldn't have state, nor return
something different given the same input.

That is a true difference. However, in slightly complicated
way a call to a getter can be interpret as a mathematical
function, that returns a unique get-operation (always the
same), which then uniquely maps a state to a value. With the
additional convention that this value is considered as the
value of the original expression - which is where it
deviates from mathematical conventions. (This is the essence
of the use an meaning of monads in functional languages.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top