English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can someone explain what exactly a function is in Python?
Also, what's the difference between the "return" and "print" functions?
How would "return" give you data if you can't print it?

2006-08-07 07:56:08 · 4 answers · asked by Elmo 2 in Computers & Internet Programming & Design

4 answers

A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called.

The Return Statement:
6.7 The return statement

return_stmt ::= "return" [expression_list]

Download entire grammar as text.
return may only occur syntactically nested in a function definition, not within a nested class definition.

If an expression list is present, it is evaluated, else None is substituted.

return leaves the current function call with the expression list (or None) as return value.

When return passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the function.

In a generator function, the return statement is not allowed to include an expression_list. In that context, a bare return indicates that the generator is done and will cause StopIteration to be raised.


The PRINT Statement
6.6 The print statement

print_stmt ::= "print" ( [expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]] )

Download entire grammar as text.
print evaluates each expression in turn and writes the resulting object to standard output (see below). If an object is not a string, it is first converted to a string using the rules for string conversions. The (resulting or original) string is then written. A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line. This is the case (1) when no characters have yet been written to standard output, (2) when the last character written to standard output is "\n", or (3) when the last write operation on standard output was not a print statement. (In some cases it may be functional to write an empty string to standard output for this reason.) Note: Objects which act like file objects but which are not the built-in file objects often do not properly emulate this aspect of the file object's behavior, so it is best not to rely on this.

A "\n" character is written at the end, unless the print statement ends with a comma. This is the only action if the statement contains just the keyword print.

Standard output is defined as the file object named stdout in the built-in module sys. If no such object exists, or if it does not have a write() method, a RuntimeError exception is raised.

print also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as ``print chevron.'' In this form, the first expression after the >> must evaluate to a ``file-like'' object, specifically an object that has a write() method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to None, then sys.stdout is used as the file for output.

2006-08-07 08:07:59 · answer #1 · answered by HotRod 5 · 0 0

A function sets up a group of instructions that can be used multiple times by referencing the function name. PHP example:

function doThis(var, vartwo) {
echo var;
vartwo++;
return vartwo;
}
echo dothis("hi", 2);

This would echo "hi" and the number 3.
if you sent in different variables then different things would be outputted.

RETURN returns a processed variable from the function and PRINT echoes it to the screen. They are totally different commands.
Sorry I didn't use python examples but the theory is the same.

2006-08-07 09:00:31 · answer #2 · answered by Anonymous · 0 0

a function is a group of code which should be in a particular structure,
the operator (return) it mean you can use particular value out of the function for example:
function MyValue( ){
some code
A = 100;
return A;
}

100 + A = 200;
and print is just to display a value.

2006-08-07 08:08:13 · answer #3 · answered by Mag 7 · 0 0

often, a parameter in a functionality is in basic terms a variable which you will assign any fee to, so as that your functionality could hire it while it is named. for example I definitely have a functionality that takes a selection and provides 2 to it functionality call(parameter){ parameter + 2; } run call(2); this implies run the functionality and assign the fee 2 to the parameter the place it is used interior the functionality itself to do the calculation. wish this helped.

2016-11-04 01:58:55 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers