Function definition and call
Defines a function that returns a string and calls it.
C C23
C23
#include <stdio.h>
static void greet(const char *name, char *out, size_t n) {
snprintf(out, n, "Hello, %s", name); // C strings require self-managed buffers
}
int main(void) {
char buf[64];
greet("Alice", buf, sizeof buf);
printf("%s\n", buf);
return 0;
}
Output
Hello, Alice
C++ C++26
C++26
#include <cstdio>
#include <string>
static std::string greet(const std::string& name) {
return "Hello, " + name;
}
int main() {
std::printf("%s\n", greet("Alice").c_str());
return 0;
}
Output
Hello, Alice
C# v11.0.100
v11.0.100
string Greet(string name) => "Hello, " + name; // local function
Console.WriteLine(Greet("Alice"));
Output
Hello, Alice
Go v1.27.4
v1.27.4
package main
import "fmt"
func greet(name string) string {
return "Hello, " + name
}
func main() {
fmt.Println(greet("Alice"))
}
Output
Hello, Alice
Java v27.0.1
v27.0.1
public class Main {
static String greet(String name) {
return "Hello, " + name;
}
public static void main(String[] args) {
System.out.println(greet("Alice"));
}
}
Output
Hello, Alice
JavaScript v26.13.1
v26.13.1
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice"));
Output
Hello, Alice
PHP v8.6.0
v8.6.0
function greet($name) {
return "Hello, " . $name;
}
echo greet("Alice");
Output
Hello, Alice
Python v3.15.2
v3.15.2
def greet(name):
return "Hello, " + name
print(greet("Alice"))
Output
Hello, Alice
Rust v1.99.1
v1.99.1
fn greet(name: &str) -> String {
format!("Hello, {}", name)
}
fn main() {
println!("{}", greet("Alice"));
}
Output
Hello, Alice
TypeScript v26.13.1
v26.13.1
function greet(name: string): string {
return "Hello, " + name;
}
console.log(greet("Alice"));
Output
Hello, Alice
Language differences
C: the function requires the caller to supply the output buffer.
Go: type annotations required; strings concatenate with +.
Java: type annotations required; concatenation with +.
JavaScript: no parameter or return type declarations needed.
PHP: no parameter or return type declarations needed (PHP 7+ supports optional annotations); strings concatenate with ..
Python: no parameter or return type declarations needed (3.5+ supports optional annotations); concatenation with +.
Rust: type annotations required; concatenation commonly via format! (which returns an owned String whose ownership passes to the caller).