VOOZH about

URL: https://www.geeksforgeeks.org/dsa/functions-in-programming/

⇱ Functions in programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Functions in programming

Last Updated : 26 Mar, 2026

A function is a block of code that performs a specific task and can be reused whenever needed.

  • Avoids writing the same code again and again.
  • Makes programs simple and easy to understand by breaking into smaller parts.
  • Accepts input (parameters) and gives output.
  • Finding and fixing errors become easier.
👁 function1

Built-in functions

  • These predefined functions provided by programming languages or libraries to perform common tasks such as mathematical calculations, input/output, or string operations.
  • For example, the sqrt() function from the C++ <cmath> library calculates the square root of a number.

Output
Square Root: 5

User-defined

  • These functions are functions written by programmers to perform specific tasks required in a program.
  • They help organize code and allow reuse of logic whenever needed.

Output
Hello, World!

Function Visualizer

Where are Functions Used in DSA?

In DSA, functions are used to implement algorithms and solve problems in a structured and reusable way.

  • Implement algorithms like searching and sorting.
  • Handle recursion (like factorial, tree traversal).
  • Break problems into smaller subproblems.
  • Operate on data structures like arrays, linked lists, trees, and graphs.
  • Make code clean and easy to understand.
Comment
Article Tags:
Article Tags: