What are some differences between a function and an algorithm?



In the context of this lesson, what are some differences between a function and an algorithm?


Although they share some similarities, functions and algorithms have some important differences that make them separate concepts.

Functions are typically a concrete block of written code. They are a specific set of repeatable instructions, which take in an input and produce some output.

Functions can implement algorithms, but do not have to. For instance, a function can perform a simple task such as printing “Hello world” to the console.

In most programming languages, functions must be declared to work for specific types of input. For example, when you declare a function in Java that takes in an input value, you must declare the data type of that input, and the values you pass in must match that data type.

Algorithms on the other hand, are more conceptual, and typically an abstract idea rather than something concrete like a code block. They describe a logical sequence of steps to perform some task like solving a problem.

Unlike functions, which must have specified data types for their input values, algorithms can be generalized to work for any kind of input values, not limited to a specific type of input.