GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • Python Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Functions in Python - SyntaxDB - Python Syntax Reference

  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Print
      • User Input
      • Commenting
      • Read from Text File
      • Write to File
      • Import Module
    • Variables
      • Variable Declaration
      • Lists
      • List Comprehensions
      • Tuples
      • Dictionaries
      • Sets
    • Control Flow
      • If Statement
      • For Loop
      • While Loop
      • Ternary Operator
      • Try Catch Block
      • With Statement
      • Generators
    • Functions
      • Functions
      • First Class Functions
      • Lambda
      • Map
      • Filter
    • Object Oriented Programming
      • Class Declaration & Structure
      • Class and Instance Variables
      • Constructor
      • Instantiation
      • Methods
      • Inheritance
Categories
Language
Python
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: 3.4.3

Categories

  • User Program Communication
    • Print
    • User Input
    • Commenting
    • Read from Text File
    • Write to File
    • Import Module
  • Variables
    • Variable Declaration
    • Lists
    • List Comprehensions
    • Tuples
    • Dictionaries
    • Sets
  • Control Flow
    • If Statement
    • For Loop
    • While Loop
    • Ternary Operator
    • Try Catch Block
    • With Statement
    • Generators
  • Functions
    • Functions
    • First Class Functions
    • Lambda
    • Map
    • Filter
  • Object Oriented Programming
    • Class Declaration & Structure
    • Class and Instance Variables
    • Constructor
    • Instantiation
    • Methods
    • Inheritance

Functions

Functions in Python

A function is a block of code that can be called from another location in the program or class.


Syntax
#function definition
def function_name(passed_params):
    #code to be executed
    return value #optional return statement

#function call
function_name(passed_params)

#function call with associative parameter variables
function_name(param1=value1, param2=value2)

Notes

It is used to reduce the repetition of multiple lines of code.

A function can either return nil, a value, or an array of values.

When declaring a function, parameters can be passed as regular variables (no prefix to variable), tuples for a variable number of args (using a single asterisk prefix *), or as a dictionary (using a double asterisk prefix **).

By default, functions do not have access to outside variables. To access global variables, use the 'global' keyword in front of the variable name. Otherwise, these global variables will be initialized inside the function with local scope.


Example
def find_minimum(a, b):
    if a < b:
        return a
    else:
        return b

#function call
min_number = find_minimum(5, 10) #should return 5

See Also
Documentation

Defining Funtions - Python Documentation

Add to Slack

< Generators   |   First Class Functions >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.