Difference Between Statement and Function

Difference between statement and function

A for loop is a not usually a function, it is a special kind of statement called a flow control structure.

A statement is a command. It does something. In most languages, statements do not return values. Example:

print "Hello World"

A function is a subroutine that can be called elsewhere in the program. Functions often (but not necessarily) return values. Example:

function(a) { return a * 2 }

A control structure, also known as a compound statement, is a statement that is used to direct the flow of execution. Examples:

if (condition) then { branch_1 } else { branch_2 }
for (i = 0; i < 10; i += 1) { ... }

Also worth noting is that an expression is a piece of code that evaluates to a value. Example:

2 + 2

All examples are in pseudocode, not tied to any particular language. Also note that these are not exclusive categories, they can overlap.

What is the difference between a statement and a function in Python?

A statement is a syntax construct. A function is an object. There's statements to create functions, like def:

def Spam(): pass

So statements are one of the ways to indicate to Python that you want it to create a function. Other than that, there's really not much relation between them.

What is the difference between function and keyword/statements?

Python keywords are essentially special reserved words that have specific meanings and purposes and can’t be used for anything but those specific purposes. They come with the python standard library and you cannot change them in anyway. You can read more about keywords here If you run help("keywords") in your python interpreter, a bunch of keywords will be returned, and del is one of them.

Python statements refer to any line/lines of python code (which include stuff such as del motorcycles[0] and print(motorcycles[0])), and they do not have to meet any specific conditions to be classified as statements.

Python functions refer to a block of reusable code that does something and can returns a value each time they are run. They can either come with the standard library or be defined by users. You can refer to this for more information about functions.

Del in python is a keyword as it does something a normal python function cannot do: it interacts with the underlying memory and unbinds the variable from the memory. It is implemented with CPython.

What is exactly difference between a 'statement' and a 'function' in oracle sql?

In SQL, there is no such thing as a "CASE statement". Your example is a CASE expression. (CASE statements do exist in PL/SQL.)

The documentation states "An expression is a combination of one or more values, operators, and SQL functions that evaluates to a value." So a SQL function is not different from an expression, it is a specific type of expression.

Note that DECODE and CASE behave differently when comparing NULL values: DECODE considers two NULLs to be "the same", which is an exception to the rule that comparing a NULL to anything has an "unknown" result.

with data(a, b) as (
select 1,1 from dual union all
select 1,null from dual union all
select null,1 from dual union all
select null, null from dual
)
select a, b,
decode(a,b,'same','different') decode_result,
case when a = b then 'same' else 'different' end case_result
from data;

A B DECODE_RESULT CASE_RESULT
------ ------ ------------- -----------
1 1 same same
1 (null) different different
(null) 1 different different
(null) (null) same different

What is the difference between an expression and a statement in Python?

Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator () the subscription operator [] and similar, and can be reduced to some kind of "value", which can be any Python object. Examples:

3 + 5
map(lambda x: x*x, range(10))
[a.x for a in some_iterable]
yield 7

Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. Examples:

# all the above expressions
print 42
if x: do_y()
return
a = 7

JavaScript: difference between a statement and an expression?

Are all statements also expressions?

“Wherever JavaScript expects a statement, you can also write an expression. Such a statement is called an expression statement. The reverse does not hold: you cannot write a statement where JavaScript expects an expression. For example, an if statement cannot become the argument of a function.”

This is comes from a recent post by Axel Rauschmayer about this topic:
Expressions versus statements in JavaScript

Hope it helps.

Differences between Expression vs Function

If you think about it in the context of a pure FP, one that doesn't allow side-effects (therefore statements aren't required), then a function is a just a value with a specific type (A -> B). An expression is some combination of symbols that reduces to a value.

Consider the following examples:

a = 1 + (2 * 3);
b = a * 10;

add1 = x -> x + 1;

applyTwice = f -> x -> f(f(x))

add2 = applyTwice(add1)

a, b, add1, applyTwice & add2 are all names have been defined to hold the value computed by specific expressions. The first two expressions reduced to 7 and 70 respectively, and the latter three are function values. The final one, add2, is defined via an expression that reduces to a function value.

With regards to the points you raised:

  • A function has a name and can be referred to (though an expression could be assigned to a variable?)

Most languages allow you assign any expression to a name.

  • A function can take parameters (is an expression able to?)

Functions have parameters, unlike non-functions. As noted above, an expression can reduce to a function value.

  • A function can have a scope/encapsulation and contain multiple statements.

The function body is essentially just an expression, albeit one that contains unbound variables that reference the function parameters. Statements are a distraction - some languages also allow you to assign the result of a series of statements to a non-function variable, e.g.:

result = {
a = f();
b = g(a);
h(b);
}

(i.e. the return value of h(b) is assigned to result)



Related Topics



Leave a reply



Submit