πŸŽ‰ New: Top 75 PHP Interview Questions for 2026 β€” Free for all learners

Computer Programming Functions

P
php Guru
Β· December 1, 2025 Β· 4 min read Β· Updated December 1, 2025

πŸ“Œ Key Takeaways

  • Computer Programming Functions
  • Computer Programming Functions – Definition, Syntax, Examples in C, C++, Java, Python & PHP
  • What Is a Function?
  • Why Use Functions?
  • Syntax of a Function (General Concept)
  • Types of Functions
Advertisement

Computer Programming Functions – Definition, Syntax, Examples in C, C++, Java, Python & PHP

A function is a block of code designed to perform a specific task. Instead of writing the same code repeatedly, you place it inside a function and call it whenever needed. Functions improve reusability, readability, and organization of your programs.


What Is a Function?

A function typically includes:

  1. Function Name – used to call the function
  2. Parameters – input values
  3. Return Type – the type of value returned
  4. Function Body – the actual code
  5. Return Statement – sends output back to the caller

Why Use Functions?

  • Avoid repeating code (DRY principle)
  • Organize large programs
  • Make code reusable
  • Improve debugging and testing
  • Allow teamwork (each function can be developed separately)

Syntax of a Function (General Concept)

return_type function_name(parameters) {
    // code to execute
    return value;
}

Types of Functions

  1. Built-in Functions
    • Provided by the programming language
    • Examples: printf(), sqrt(), strlen()
  2. User-Defined Functions
    • Created by the programmer
  3. Functions With Parameters
  4. Functions Without Parameters
  5. Functions With Return Value
  6. Functions Without Return Value

Functions in Different Programming Languages

programming functions, function examples, C functions, C++ functions, Java methods, Python functions, PHP functions, recursion, function syntax, parameters, return type

1. Functions in C

Example: Function Without Parameters

#include <stdio.h>

void greet() {
    printf("Hello from C!n");
}

int main() {
    greet();
    return 0;
}

Example: Function With Parameters & Return Value

int add(int a, int b) {
    return a + b;
}

2. Functions in C++

C++ functions work like C but can also be placed inside classes.

Normal Function

int multiply(int a, int b) {
    return a * b;
}

Member Function (Inside Class)

class Math {
public:
    int square(int x) {
        return x * x;
    }
};

3. Functions in Java

In Java, functions are part of classes, so they are called methods.

Example

class Demo {
    static void greet() {
        System.out.println("Hello from Java!");
    }

    static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        greet();
        System.out.println(add(5, 10));
    }
}

4. Functions in Python

Python functions are very simple to define using the def keyword.

Example

def greet():
    print("Hello from Python!")

def add(a, b):
    return a + b

greet()
print(add(3, 7))

5. Functions in PHP

PHP functions begin with the function keyword.

Example

<?php
function greet() {
    echo "Hello from PHP!";
}

function add($a, $b) {
    return $a + $b;
}

greet();
echo add(4, 6);
?>

Arguments vs Parameters

Parameters

Variables listed in the function definition.

Arguments

Values sent when calling the function.


Return Statements

  • A function may return one value.
  • If nothing is returned, languages use either:
    • void (C, C++, Java)
    • None (Python)
    • null (PHP)

Example:

return x + y

Recursive Functions

A recursive function calls itself inside its own definition.

Example (Python)

def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n - 1)

Function Overloading

Available in C++ & Java
Not available in C
Possible via tricks in Python & PHP

Example (C++)

int sum(int a, int b);
double sum(double a, double b);

Anonymous (Lambda) Functions

Python

add = lambda x, y: x + y

Java

(x, y) -> x + y

PHP

$add = fn($x, $y) => $x + $y;

Best Practices for Using Functions

  • Keep functions small and focused
  • Use meaningful names
  • Avoid too many parameters
  • Reuse existing functions
  • Document function purpose
  • Write testable functions


FAQ

1. What is a function in programming?

A function is a reusable block of code designed to perform a specific task.

2. Why are functions important?

They reduce code repetition, improve organization, and make code easier to maintain.

3. Can a function return multiple values?

Most languages return one value, but Python and PHP can return multiple values in arrays or tuples.

4. What is recursion?

Recursion is when a function calls itself to solve smaller parts of a problem.

5. Are methods and functions the same?

Methods are functions inside classes (Java, C++, Python).
Functions can exist independently (C, PHP, Python).

6. Can two functions have the same name?

Yes, in languages that support function overloading (Java, C++).

P
php Guru
← Previous Post
Computer Programming Strings
Next Post β†’
Computer Programming Classes and Objects

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 3   +   5   =