Funções
Funções são blocos de código reutilizáveis. Veja como diferentes linguagens definem e chamam funções.
Declaração Básica de Funções
- JavaScript
- PHP
- Rust
- Go
- Python
- Zig
- C#
- C++
- C
- Java
- Ruby
- Swift
// Function declaration
function greet(name) {
return "Hello, " + name;
}
// Arrow function
const greet = (name) => {
return "Hello, " + name;
};
// Arrow function (shorthand)
const greet = name => "Hello, " + name;
// Function call
greet("John");
<?php
// Function declaration
function greet($name) {
return "Hello, " . $name;
}
// With type hints
function add(int $a, int $b): int {
return $a + $b;
}
// Function call
greet("John");
// Function declaration
fn greet(name: &str) -> String {
format!("Hello, {}", name)
}
// Multiple parameters
fn add(a: i32, b: i32) -> i32 {
a + b // No semicolon = return
}
// Function call
greet("John");
// Function declaration
func greet(name string) string {
return "Hello, " + name
}
// Multiple return values
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
// Function call
greet("John")
# Function declaration
def greet(name):
return "Hello, " + name
# With type hints
def add(a: int, b: int) -> int:
return a + b
# Default parameters
def greet(name: str = "World") -> str:
return f"Hello, {name}"
# Function call
greet("John")
// Function declaration
fn greet(name: []const u8) ![]const u8 {
return "Hello, " ++ name;
}
// With error handling
fn divide(a: f64, b: f64) !f64 {
if (b == 0) return error.DivisionByZero;
return a / b;
}
// Function call
_ = greet("John");
// Function declaration
public static string Greet(string name)
{
return "Hello, " + name;
}
// Expression-bodied member
public static string Greet(string name) => "Hello, " + name;
// Default parameters
public static string Greet(string name = "World")
{
return "Hello, " + name;
}
// Function call
Greet("John");
// Function declaration
std::string greet(const std::string& name) {
return "Hello, " + name;
}
// Function with default parameter
std::string greet(const std::string& name = "World") {
return "Hello, " + name;
}
// Function call
greet("John");
// Function declaration
char* greet(const char* name) {
static char result[100];
sprintf(result, "Hello, %s", name);
return result;
}
// Function call
greet("John");
// Method declaration
public static String greet(String name) {
return "Hello, " + name;
}
// Method call
greet("John");
# Function declaration
def greet(name)
"Hello, " + name
end
# Default parameters
def greet(name = "World")
"Hello, " + name
end
# Function call
greet("John")
// Function declaration
func greet(name: String) -> String {
return "Hello, " + name
}
// Shorthand
func greet(name: String) -> String {
"Hello, " + name
}
// Default parameters
func greet(name: String = "World") -> String {
"Hello, " + name
}
// Function call
greet(name: "John")
Recursos Avançados de Funções
- JavaScript
- PHP
- Rust
- Go
- Python
- Zig
- C#
- C++
- C
- Java
- Ruby
- Swift
// Default parameters
function greet(name = "World") {
return "Hello, " + name;
}
// Rest parameters
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
// Arrow function with this binding
const obj = {
name: "John",
greet: function() {
setTimeout(() => {
console.log(this.name);
}, 100);
}
};
<?php
// Default parameters
function greet($name = "World") {
return "Hello, " . $name;
}
// Variadic functions
function sum(...$numbers) {
return array_sum($numbers);
}
// Type declarations
function process(array $items, callable $callback): array {
return array_map($callback, $items);
}
// Closures
let add = |a: i32, b: i32| a + b;
// Higher-order functions
fn apply<F>(f: F, value: i32) -> i32
where
F: Fn(i32) -> i32,
{
f(value)
}
// Function pointers
fn double(x: i32) -> i32 {
x * 2
}
let func: fn(i32) -> i32 = double;
// Variadic functions
func sum(numbers ...int) int {
total := 0
for _, n := range numbers {
total += n
}
return total
}
// Function as value
var operation func(int, int) int
operation = func(a, b int) int {
return a + b
}
# Default parameters
def greet(name="World"):
return f"Hello, {name}"
# *args and **kwargs
def process(*args, **kwargs):
print(args)
print(kwargs)
# Lambda functions
add = lambda a, b: a + b
# Decorators
def my_decorator(func):
def wrapper():
print("Before")
func()
print("After")
return wrapper
// Function pointers
const Operation = fn(i32, i32) i32;
fn add(a: i32, b: i32) i32 {
return a + b;
}
const op: Operation = add;
// Comptime functions
fn comptimeAdd(comptime a: i32, comptime b: i32) i32 {
return a + b;
}
// Default parameters
public static string Greet(string name = "World")
{
return "Hello, " + name;
}
// Params keyword
public static int Sum(params int[] numbers)
{
return numbers.Sum();
}
// Lambda expressions
Func<int, int, int> add = (a, b) => a + b;
// Action and Func delegates
Action<string> print = name => Console.WriteLine(name);
// Default parameters
std::string greet(const std::string& name = "World") {
return "Hello, " + name;
}
// Variadic templates (C++11+)
template<typename... Args>
void print(Args... args) {
((std::cout << args << " "), ...);
}
// Lambda functions
auto add = [](int a, int b) { return a + b; };
// Function pointers
int add(int a, int b) {
return a + b;
}
int (*operation)(int, int) = add;
// Variadic functions
#include <stdarg.h>
int sum(int count, ...) {
va_list args;
va_start(args, count);
int total = 0;
for (int i = 0; i < count; i++) {
total += va_arg(args, int);
}
va_end(args);
return total;
}
// Method overloading
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
// Lambda expressions (Java 8+)
Function<Integer, Integer> square = x -> x * x;
// Method references
Function<String, Integer> length = String::length;
# Default parameters
def greet(name = "World")
"Hello, " + name
end
# Keyword arguments
def create_user(name:, email:, age: 18)
{ name: name, email: email, age: age }
end
# Blocks
[1, 2, 3].each do |n|
puts n
end
# Procs and lambdas
square = ->(x) { x * x }
// Default parameters
func greet(name: String = "World") -> String {
"Hello, " + name
}
// Variadic parameters
func sum(_ numbers: Int...) -> Int {
numbers.reduce(0, +)
}
// Closures
let add = { (a: Int, b: Int) -> Int in
return a + b
}
// Trailing closures
numbers.map { $0 * 2 }
Principais Conclusões
- Estilos de declaração --
function(JavaScript, PHP),def(Python, Ruby),fn(Rust, Zig),func(Go, Swift). Arrow functions e lambdas são comuns em linguagens modernas. - Passagem de parâmetros -- Valores padrão são suportados na maioria; variádicos via
*args/**kwargs(Python),...(PHP, Go, Swift, C),params(C#). Zig usa!para retornos falíveis. - Valores de retorno -- Go permite múltiplos retornos; Rust usa
Result/Option; Zig usa error unions; Python/Ruby permitem retornos implícitos da última expressão.