Python Language Functions: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 4: Line 4:
Declaration:
Declaration:
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
def function_name([arguments]):
def function_name([parameters]):
   <function body>
   <function body>
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
def function_name(arg1, arg2, arg3):
def function_name(par1, par2, par3):
   <function body>
   <function body>
</syntaxhighlight>
</syntaxhighlight>


An '''argument''' is a value that is passed into the function as function's input.  
A '''parameter''' is a variable name used in the function definition. The parameters are handles for arguments for a particular function invocation.
 
 
When the function is invoked, we pass an '''argument''' for each parameter declared in the function definition. An argument is a value that is passed into the function as function's input.  Arguments are passed in parentheses and they are separated by commas.


When the function is invoked, we pass a '''parameter''' for each argument declared in the function definition. Parameters are passed in parentheses and they are separated by commas.
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
function_name([parameters])
function_name([arguments])
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
function_name(par1, par2, par3)
function_name(arg1, arg2, arg3)
</syntaxhighlight>
</syntaxhighlight>



Revision as of 05:17, 26 May 2021

Internal

Overview

Declaration:

def function_name([parameters]):
  <function body>
def function_name(par1, par2, par3):
  <function body>

A parameter is a variable name used in the function definition. The parameters are handles for arguments for a particular function invocation.


When the function is invoked, we pass an argument for each parameter declared in the function definition. An argument is a value that is passed into the function as function's input. Arguments are passed in parentheses and they are separated by commas.

function_name([arguments])
function_name(arg1, arg2, arg3)

Function Name Rules

The function name rules for function names is the same as for variable names.

Example

def something(a, b):
  c = a + b
  return c

Built-in Functions

type()

type() returns the type of the argument.

print()

print(). If more comma-separated arguments are used, every comma adds a space.

input()

input() instructs Python to pause and read data from stdin. input() returns a string.

s = input('this is the prompt')
print(s)

Other Functions

max('...'), type conversion functions float(), int(), str()