function
A named, reusable block of code that takes inputs, called arguments, and can return a result.
In more detail
Defining a function does not run it; calling it does, usually by writing its name followed by parentheses. Functions let you name a piece of work, reuse it without copying code and test it on its own. In many languages functions are also values that can be stored, passed to other functions and returned.
Examples
func usage(total, used int) (int, error)V8 counts how often each function runs to decide which ones to optimise.Related terms
- multiple return values — A Go function returning more than one result at once, most often a useful value together with an error.
- function signature — The name, parameters and their order that define how a function must be called.
- method — A function that belongs to an object or type and is called on a value of it, as in name.upper().
- anonymous function — A function written inline without a name, often started as a goroutine or passed to another function.
- callback — A function you pass to other code so it can call you back later, for example when an event happens or a timer fires.
- hot path — Code that runs very often, such as on every request, where small costs add up.
- def — The Python keyword that starts a function definition: def, the name, parameters in parentheses, a colon, then an indented body.
- function call — Running a function by writing its name followed by parentheses holding any arguments.
- parameter — A name in a function definition that receives the value of an argument each time the function is called.
- argument — A value you pass to a function when you call it, written inside the parentheses of the call.
- return statement — The statement return expression, which ends a function call immediately and hands the value back to the caller.
Taught in
- What Go Is and Why It Matters — Concurrent Systems Programming with Go
- Go Workspaces and Modules — Concurrent Systems Programming with Go
- Your First Program and go run — Concurrent Systems Programming with Go
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Pointers and Memory — Concurrent Systems Programming with Go
- Interfaces and Why They Are Implicit — Concurrent Systems Programming with Go
- Type Assertions and the Empty Interface — Concurrent Systems Programming with Go
- Errors as Values — Concurrent Systems Programming with Go
- V8 Engine & JIT Pipeline: Bytecode & Turbofan — Node.js Internals & Backend Services
- What Python Is, And Where It Came From — Hands-on Python: Complete Video Course & Book
- Installing Python On Windows — Hands-on Python: Complete Video Course & Book
- First Run, And Fixing A Broken Install — Hands-on Python: Complete Video Course & Book
- Several Functions, And Flow Of Control — Hands-on Python: Complete Video Course & Book
- Returning Values — Hands-on Python: Complete Video Course & Book
- Local Scope And Global Constants — Hands-on Python: Complete Video Course & Book
- The Exception Family Tree — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Thirty Questions, One Sneaky Print — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Forty Minutes, Thirty Items: How PCEP Is Run — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Interpreter, Compiler, And What Counts As Syntax — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Literals, Names And Four Number Bases — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Operators And Who Binds First — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Control Flow Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Lists: Slices, Methods And Aliases — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Tuples, And The Comma That Makes Them — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Collections Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Functions, Return Values And Recursion — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Parameters, Arguments And Scope — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Try, Except, Else And Finally — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Functions And Exceptions Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Twelve Traps, One Shape Each — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Mock, Review And Exam Day — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Import Variants, dir() And sys.path — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- math, random And platform — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Your Own Modules And Packages — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Modules Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exception Classes Of Your Own — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exceptions Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comparing, Slicing And Never Changing — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Classes, Objects And Two Stacks — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Methods And The self Parameter — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Introspection: hasattr, __name__, __module__ And __bases__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Special Methods: __str__, __eq__ And Friends — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- List Comprehensions, Filtered And Nested — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Lambdas, map() And filter() — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Generators And Iterators — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Closures: Functions That Remember — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- read, readline, errno And bytearray — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
References
- The Go spec: function declarations
- A Tour of Go: functions
- MDN: Function
- Python tutorial: defining functions
- Python glossary: function