All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is a function in programming?

Post cover: What is a function in programming?
This content has been automatically translated from Ukrainian.
A function is a fundamental building block of programming that defines a set of instructions or actions that are executed when it is called. Functions allow you to organize code into understandable and reusable blocks. Functions can accept input data (arguments) and return a result.
In different programming languages, functions may have different names, such as "methods" in object-oriented programming. The term method is more commonly heard in discussions about Ruby code, while the term function is used for JS.

Characteristics of Functions

  • A function has a name by which it can be called.
  • A function can accept one or more arguments – input data used to perform calculations or actions within the function.
  • It is a set of instructions that are executed when the function is called.
  • A function can return a value as a result of its execution.
Conceptually, this is a list of the main characteristics that functions have. Let's look at examples (Ruby and JS)

Example of a Function (Method) in Ruby

def sum_method(a, b)
  # Add two numbers and return the result
  a + b
end

result = sum_method(3, 4)
puts result # Will output 7

Example of a Function in JavaScript

function sumFunction(a, b) {
  // Add two numbers and return the result
  return a + b;
}

let result = sumFunction(3, 4);
console.log(result); // Will output 7

Why Are Functions Needed?

Functions allow you to reuse code and avoid duplication. For example, if you need to perform the same calculations in different places in the program, you can define a function and call it whenever needed. Functions are the foundation ^_^

This post doesn't have any additions from the author yet.

The scope of a local variable in Ruby
03 Jun 16:46

The scope of a local variable in Ruby

meme code
meme code@memecode
What is the difference between int and bigint in Ruby? Minimum and maximum values.
13 Jun 06:37

What is the difference between int and bigint in Ruby? Minimum and maximum values.

meme code
meme code@memecode
What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with a limit of 4 bytes
13 Jun 07:18

What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with a limit of 4 bytes

meme code
meme code@memecode
What is immutability and mutability?
19 Jun 07:48

What is immutability and mutability?

meme code
meme code@memecode
What will be the result of adding 10.5 and 10?
23 Jun 13:23

What will be the result of adding 10.5 and 10?

meme code
meme code@memecode
[Ruby] What is the difference between variables that start with @, @@, and $?
23 Jun 14:00

[Ruby] What is the difference between variables that start with @, @@, and $?

meme code
meme code@memecode
[Fix] extconf.rb failed during the installation of the Ruby library Gosu
27 Jun 16:38

[Fix] extconf.rb failed during the installation of the Ruby library Gosu

meme code
meme code@memecode
How to make an empty git commit?
28 Jun 08:33

How to make an empty git commit?

meme code
meme code@memecode
Ruby library Gosu for creating 2D games
29 Jun 08:48

Ruby library Gosu for creating 2D games

meme code
meme code@memecode
Gosu Ruby Tutorial - пройдемось по офіційній документації
03 Jul 11:50

Gosu Ruby Tutorial - пройдемось по офіційній документації

meme code
meme code@memecode
We are writing a demo game Drones vs Zombies (Gosu / Ruby)
12 Jul 12:17

We are writing a demo game Drones vs Zombies (Gosu / Ruby)

meme code
meme code@memecode
How to fix a Windows crash caused by CrowdStrike?
19 Jul 13:53

How to fix a Windows crash caused by CrowdStrike?

meme code
meme code@memecode