Functions
Functions are blocks of code that perform specific tasks and can be reused throughout a program. They help in organizing code, making it modular, and reducing redundancy.
What is a Function?
A function is a reusable block of code that performs a specific task. Functions typically take input, process it, and return an output. The general structure of a function includes:
- Function name: Identifies the function.
- Parameters: Input values the function uses (optional).
- Return type: The value the function sends back as output (optional).
- Function body: The code that runs when the function is called.
Functions in Different Languages
- JavaScript
- Java
- Python
- C++
JavaScript Functions Overview
JavaScript supports both function declarations and expressions.
Video Explanation

Function Declaration
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice")); // Output: Hello, Alice!
Function Expression
const greet = function(name) {
return `Hello, ${name}!`;
};
console.log(greet("Bob")); // Output: Hello, Bob!
Arrow Functions
Introduced in ES6, arrow functions provide a concise way to write functions.
const greet = (name) => `Hello, ${name}!`;
console.log(greet("Charlie")); // Output: Hello, Charlie!
Function Scope and Closures
Functions in JavaScript can create closures, capturing variables from their surrounding scope.
function outerFunction() {
let outerVar = "I'm outer";
function innerFunction() {
console.log(outerVar); // Can access outerVar
}
return innerFunction;
}
const inner = outerFunction();
inner(); // Output: I'm outer
Java Functions (Methods) Overview
In Java, functions are defined within classes and are called methods. They have a return type, a name, and can have parameters.
Video Explanation

Method Declaration
public class Main {
public static void main(String[] args) {
greet("Alice");
}
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}
Return Values and Parameters
Methods can return values using the return keyword.
public static int add(int a, int b) {
return a + b;
}
Python Functions Overview
Python functions are simple to define and use. The def keyword is used for defining functions.
Video Explanation

Function Declaration
def greet(name):
return f"Hello, {name}!"
print(greet("Alice")) # Output: Hello, Alice!
Default Parameters
Functions can have default parameter values.
def greet(name="World"):
return f"Hello, {name}!"
print(greet()) # Output: Hello, World!
Lambda Functions
Python also supports lambda (anonymous) functions for simple, one-line functions.
add = lambda x, y: x + y
print(add(2, 3)) # Output: 5