pls whats the difference between writing a procedure,method and function.

Dani AI

Generated

A compact, practical clarification that builds on and : the three words describe overlapping ideas, not three completely different language constructs. "Function" highlights an input-to-output mapping (ideally without hidden side effects). "Procedure" emphasizes a sequence of steps and is commonly used when the code performs actions (side effects) rather than producing a value. "Method" is the term used in object-oriented languages for a routine bound to a class or instance; methods can be function-like (returning a value) or procedure-like (returning void).

A small Java sketch that illustrates the common cases:

public class Example {
    public int sum(int a, int b) { return a + b; }      // value-returning (function-like)
    public void save(String s) { /* side-effect: write, mutate */ } 
    public static int max(int a, int b) { return a > b ? a : b; } // class-level utility
    public Example() { /* constructor — special, no return type */ }
}

Practical distinctions that matter when designing code: prefer pure, small methods for computation (easy to test and reuse) and isolate side effects in well-named void methods. In Java 8+ behavior can be passed around via functional interfaces and lambdas (making "functions" more first-class), but the things that live inside classes are still called methods. Also note other uses of "procedure" (for example, database stored procedures) where the context changes the meaning.

To apply these ideas: make return-vs-void explicit in names (e.g., computeTotal vs saveRecord), document whether a method mutates object state, and keep methods focused on a single responsibility. This resolves the practical differences when writing or reading Java code.

Recommended Answers

All 2 Replies

As you have posted this in the Java Forum I assume your questions relates to the Java Language.

Well talking about functions and methods:
Functions are a block of code that are perform a specific task and return a value. These help in modularizing code into logical pieces. They also help in sprucing-up (trimming) the code as a function can be called from various places and the same code need not be written in different places hindering the readability of the code. Also since the code is not repeatitively written in many places maintenance is easy, as you just have to fix a bug (or make a change) in one place instead of many.

In Java, functions are referred to as "methods". Other langauges such as C, C++ etc prefer to call them functions.

Procedures : Procedures may perform several different tasks. Although I don't remember this distinction being made in programming langauges atleast. Sometimes functions may also be referred to as procedures, as you may have heard some lanaguages, which are modularized on functions, such as C being called procedure oriented langauges or procedural langauges.

Refer to the INTERNET for more.

Re procedures - they are a sequence of steps that implement something useful. (As opposed to non-procedural languages such as HTML that describe the required results but do not give step-by-step instructions on how to achieve them). Java is procedural, and all procedures are implemented in methods (*).
So one answer to your question is "none". Java just has methods, that implement procedures, and may implement functions (if they return a result)
(*) OK, for the real Java buffs, there are also static initialisers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.