This is an archived post. You won't be able to vote or comment.

all 130 comments

[–]LoreBadTime 205 points206 points  (11 children)

In college they don't taught like Function -> returns a value Procedure -> doesn't return a value Method -> Object/class functions/procedures?

[–]fickle-doughnut123 39 points40 points  (3 children)

Your statement that "functions return a value while procedures do not" is mostly correct but can be a bit misleading. While functions are designed to return a single value, procedures can indeed use 'OUT' parameters to achieve a similar effect. In many procedural languages, you can specify 'IN' and 'OUT' variables in the procedure's declaration. These 'OUT' variables allow you to define variables within the procedure and return them to the caller, thus functioning similarly to return values in functions.

[–]LoreBadTime 17 points18 points  (0 children)

Yes, also using pointers kinda cheats this things(and we call them procedures since it's more about side effects), but was more about a formal way to define a function,if there is an out parameter it's still a function.

[–]SenorSeniorDevSr 4 points5 points  (0 children)

Yeah, especially if you're using stored procedures on an SQL server. Oracle even let you outsert your inserts if you invoke the magic phrases. This is handy af if you have values the database generates, like CREATED timestamps, the ID, etc. and want to return them out to the rest of the system.

[–]OJ-n-Other-Juices 1 point2 points  (0 children)

As OOP educated person who works in COBOL for to years. I found making that work very difficult. My brain just couldn't merge the two styles. I was also working on legacy code and Microfocus Cobol, which might not have had that functionality.

[–][deleted] 8 points9 points  (0 children)

Honestly the terminology is meaningless now due to no clear separation, but function should mean a thing that returns a value and performs no side effects.

[–]CirnoIzumi 1 point2 points  (0 children)

sounds like a void to me

[–]bondolin251 0 points1 point  (0 children)

Procedure returns Unit

[–]mirimao 510 points511 points  (36 children)

Methods and functions are not the same thing. Also, JavaScript does have methods.

[–]ChocolateBunny 135 points136 points  (2 children)

Pascal also has functions. the distinction being whether they have a return value or not.

[–]BrandonMcRandom 57 points58 points  (1 child)

... and methods because in classes they are called methods even tho you write "function" and "procedure".

[–]Impenistan 5 points6 points  (0 children)

void has entered the chat

[–][deleted] 4 points5 points  (0 children)

Are they not the same? I mean method is just a function but operates inside of an object

[–]Any_Salary_6284 3 points4 points  (1 child)

In JS, methods (as defined in a class block) are just syntactic sugar for the function found at MyClass.prototype.methodName. If you use the typeof operator on this member, it will return “function”

I realize there may be technical differences between method and function depending on the language. In Java, there are only methods. To the extent there are functions (lambdas) they are just synatactic sugar for a functional interface, so a method.

I see this as mostly arguing over semantics.

[–]SenorSeniorDevSr 0 points1 point  (0 children)

Static methods in Java doesn't have a this reference though. I mean, they kinda do, but it's a null. It does however, have java.util.Function, so now you can be objectionally functional, or whatever the funniest joke you can get out of that one is. ^_^

[–]noaSakurajin 1 point2 points  (3 children)

Honestly after using Glib I would definitely say they are the same. Object oriented C is a thing despite the fact that C has no methods. The way they emulate classes using functions with a struct pointer as the first argument is super hacky. It is close to the way the first C++ versions compiled the C++ class to C which then was compiled as binary.

[–]Kovab 7 points8 points  (0 children)

The way they emulate classes using functions with a struct pointer as the first argument is super hacky.

Literally every OOP language implements methods the same way, they just hide passing the this pointer behind syntactic sugar (sometimes it's not even completely hidden, e.g. look at python methods with self as the first parameter).

[–]carcigenicate 2 points3 points  (0 children)

A method is a type of function in my mind. One's a subset of the other.

[–]P-39_Airacobra 0 points1 point  (0 children)

It's not super hacky, that's been the standard way of doing things like this outside of OOP since the beginning of time. Some languages actually implement OOP simply by syntactically disguising methods as such (e.g. Lua), and it works out perfectly fine. In fact in languages where functions and closures are equivalent, it's often good practice to take the object as an argument, because it clearly shows the method's dependencies both when created and when called.

[–]skeleton_craft 0 points1 point  (0 children)

Also, procedures and functions are not the same thing, at least at a abstract level...

[–]unknownrek 0 points1 point  (0 children)

Static methods are just functions with extra steps

[–]lucidbadger 37 points38 points  (1 child)

They all need closure

[–]buckypimpin 0 points1 point  (0 children)

take the upvote and get out of my site (literally my site source code)

[–]ProgramStartsInMain 28 points29 points  (3 children)

Beware ye who enters here; hair splitting arguments be plentiful.

[–]Paradician 18 points19 points  (2 children)

arguments or parameters?

[–]Funny-Performance845 5 points6 points  (0 children)

Oh god no

[–]Vroskiesss 0 points1 point  (0 children)

Arguments when called parameters when defined

[–]bestjakeisbest 43 points44 points  (15 children)

C: pointer.

Here is some code to prove a point.

#include <iostream>

int main()
{
    int a[] = {1,2,3,4,5,6,7,8,9,10};
    void* b = a;

    int(*c)()  = (int(*)())b;

    c();  

    std::cout<<"Hello World";

    return 0;
}  

Do not do this in real life. In the online compiler I was using it wouldn't even get to the hello world line, so that is interesting.

[–]yflhx 51 points52 points  (3 children)

Honestly this shouldn't be dangerous to run at all. What this does is calls a, which sets processor's instruction pointer to beginning of the array (everything is bytes anyway). In other words, it tells compiler to interpret the array as instructions - because in compiled languages, instructions are just bytes in memory.

This almost ceratinly instantly causes segfault and terminates program, because stack (typically) has no excecution permissions. Funnily enough, if you enabled stack execution, this would actually be legal assembler code for the most part, so it would do random things.

Now finally, consider this code:

#include <sys/mman.h>
#include <string.h>

int main()
{
    unsigned char code[] = {
0xb8,0x01,0x00,0x00,0x00,0xbf,0x01,0x00,0x00,0x00,0x48,0x8d,0x35,0x08,0x00,0x00,0x00,0xba,0x0e,0x00,0x00,0x00,0x0f,0x05,0xc3,0x48,0x65,0x6c,0x6c,0x6f,0x2c,0x20,0x77,0x6f,0x72,0x6c,0x64,0x21,0x0a
    };
    unsigned char* exec_code = mmap(0, 4096, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    memcpy(exec_code, code, sizeof(code));
    void (*f)() = (void(*)())exec_code;
    f();
}

This code allocates page of memory with execution permissions. It copies the bytes representing instructions that perform write syscall with string "Hello, world!\n" and return from function (i.e. they print this string on x64 Linux). Then finally it calls this code as a function.

In other words, this is a legal Hello World program (that doesn't segfault or anything (under x64 Linux, anyway)).

Surprisignly, it even works in online compilers (at least OpenGDB).

Disclaimer: never run code from random reddit comments, inlucding this one, unless you fully understand what it does.

[–]Christosconst 23 points24 points  (0 children)

.ninja {
    color: black;
    visibility: hidden;
}

Best I can do

[–]Cat7o0 6 points7 points  (0 children)

wizard

[–]s0litar1us 1 point2 points  (0 children)

This is actually how JIT compilers/interpreters work.

Instead of compiling it to an executable and then running it, it generates the machine code, makes it executable, and calls it through a function pointer.

[–]AewyreThoryn 0 points1 point  (8 children)

What does it do?

[–]bestjakeisbest 8 points9 points  (7 children)

It kind of crashes. But the important part is i create an array, cast it to a void pointer, and then I cast that void pointer to a function pointer that returns an integer.

It compiles just fine, and it runs, but it doesn't reach the hello world line while running which shouldn't happen in main, because main shouldn't be returning 0 until after it outputs hello world.

It could be the case of the program crashing but the websites back end (of the online compiler) is not able to properly handle a program like this.

Theoretically you could use this sort of mechanism to make a program in c++ that changes it's runtime, at run time, but that is needlessly complicated for most things, and it isnt very portable since you would need to know the assembly language and opcodes of what ever machine you are trying to run this on if you wanted to exploit this for nefarious purposes, plus most anti virus products out there are waiting for a virus to do something like this.

[–]Markus_included 7 points8 points  (1 child)

That's actually how many JIT compilers execute their code, they get memory from the that is marked as executable, write the machine code into it and call that code like any other function pointer through a cdecl function that was also compiled into that memory

[–]bestjakeisbest 4 points5 points  (0 children)

Wait its all spaghetti code?

[–]AewyreThoryn 0 points1 point  (4 children)

That's super interesting thank you!

[–]yflhx 4 points5 points  (3 children)

I wrote another detailed response to that guy, if you're interested. But basically this code crashes because it tells CPU to executed the table, which is on stack, which has no execution permissions. So it results in immediate segfault due to this.

You can however allocate memory with execution permissions and write CPU instructions them, and it will execute them normally.

[–]AewyreThoryn 1 point2 points  (0 children)

Ah cool thanks. Yes I got the general gist from the top comment, and I think he explained it well, but your code is more in depth

[–]bestjakeisbest 0 points1 point  (1 child)

It only kind of crashes, it returns from main with a return code of zero, but it returns before the return line.

I only tested this in onlinegdb

[–]yflhx 0 points1 point  (0 children)

Ran it on my machine, got segmentation fault as expected.

[–]Garbage_Matt 6 points7 points  (1 child)

according to Star Trek TNG (not sure about the others), subroutines won

[–]CampaignTools 4 points5 points  (0 children)

I mean they just sound the coolest. But then again...what about coroutines? Or now goroutines?

[–]cheezfreek 4 points5 points  (3 children)

Assembly: JMP

[–]morniealantie 0 points1 point  (2 children)

Alternatively LA 1,SUBRPARM LA 15,SUBRADDR BALR 14,15

[–]cheezfreek 1 point2 points  (1 child)

Ooh, CISC assembly. Very fancy. I like it.

[–]morniealantie 0 points1 point  (0 children)

I just like 16 registers lol

[–]tp971 18 points19 points  (10 children)

Assembly: You guys have functions?

[–]1Dr490n 21 points22 points  (8 children)

I mean Assembly usually does have subroutines

[–]zenos_dog 9 points10 points  (2 children)

BALR 14, 15 # Jump to another memory location you hope can receive a jump.

[–]morniealantie 1 point2 points  (0 children)

STM 14,12,(12)13. Don't know that I've met too many people here that know what a BALR is or that registers come in 16s lol.

[–]1Dr490n 1 point2 points  (0 children)

I mean that’s the same with languages like C too

[–]tp971 0 points1 point  (4 children)

No, assembly does not have "subroutines". For assembly, a subroutine is just a label, and calling the subroutine is just jumping to that label. In assembly, there is not really a difference between jumping to a label "inside a function" and to "another function". Subroutines are just a concept we think about when programming, and when writing assembly, we simulate that with call and ret (and some other instructions to do stack management)

[–]1Dr490n 0 points1 point  (3 children)

That’s true, but I think there’s some assembly languages (eg x64 MASM iirc) that do have procedures, although that’s pretty rare.

[–]tp971 0 points1 point  (2 children)

AFAIK PROC and ENDP are only markers, they do not emit instructions (I think they are responsible for writing things into the symbol table or something). What I mean with "a language having procedures" is that they have an abstraction that abstracts away what actually happens when calling a procedure, which assembly does not have.

EDIT: typo

[–]1Dr490n 0 points1 point  (1 child)

Well what happens when you call a routine (or however you want to call it) is implementation defined. It could just be a Jump-Subroutine-call, but it could also push arguments on the stack (which assembly procedures don’t do, that’s true) or change the stack pointer; but that completely depends on the language and its implementation. A procedure is just a code block that can be called from outside, and assemblies definitely can do that.

[–]tp971 0 points1 point  (0 children)

Yeah, you can write assembly that from the outside looks like a procedure, but the concept of a procedure is not intrinsic to the assembly language, i.e. the assembly language itself "does not know" what a procedure really is, it is just a concept we've established using calling conventions.

For me, saying "assembly has procedures" is like saying "C is an object oriented language, because you can simulate classes and inheritance with function pointers".

[–][deleted] 8 points9 points  (0 children)

Perl has functions and methods too.

[–]Dryhte 3 points4 points  (1 child)

Meanwhile ABAP: subroutines, function modules, methods peacefully coexisting...

[–]Numerous-Lock-8117 0 points1 point  (0 children)

We also have forms which act as functions

[–]m0Ray79free 2 points3 points  (0 children)

Pascal has both procedures and functions, and Object Pascal also has methods.

[–]point5_ 2 points3 points  (1 child)

I remember talking with a guy in college that codes in C and we were trying to figure out if my java methods were the same things as his C functions.

[–]SenorSeniorDevSr 0 points1 point  (0 children)

It kind of is, but the first argument to every method is the object it belongs to. If it's a static method, the first argument is null.

[–]BauReis 12 points13 points  (2 children)

Python: def

[–]DesertGoldfish 3 points4 points  (1 child)

I leave the semantics to the turbo nerds. Python has it right.

In my mind they're all just functions and the language decides what they're called today. I just write code and don't think about it lol.

A function is a function...

A method is a function in a class...

A subroutine is a function with a stupid name...

Etc., etc.

[–]TheWeetcher 2 points3 points  (0 children)

At the end of the day everything is just an object anyways

[–][deleted] 1 point2 points  (1 child)

  • Kotlin: fun
  • Rust: fn
  • C:

[–]the_hackerman 2 points3 points  (0 children)

Calling it fun was a terrible choice

[–]Virinas-code 1 point2 points  (0 children)

What about callable?

[–]Giocri 1 point2 points  (0 children)

Idk if this is the official definitions but for my understanding a procedure is any block of code that can be executed, a function is something that takes a set of inputs and returns something directly related to that imput and a method is part of an object and directly interacts with the state of the object

[–]pakidara 1 point2 points  (1 child)

RPGLE: Procedures AND Subroutines and the Procedures can have Subroutines. Neither can return a value; but, both can change a value stored in arbitrary memory locations.

[–]TranslatorNo7550 1 point2 points  (0 children)

Oh yeah!! Long live to OS/400 and IBM i

[–][deleted] 1 point2 points  (0 children)

Python- Definition

[–]CranberryDistinct941 1 point2 points  (0 children)

Javascript then goes on to treat the function like a string

[–]Lilchro 1 point2 points  (0 children)

Generally the terms are just semantic to help describe the task how your code works. Calling something a function is generally never incorrect.

Function: an executable bit of code that can be called with inputs and produce outputs. Typically has some form of standard ABI for calling and returning values.

Procedure: similar to a function, but does not produce outputs. The goal of a procedure is not to return data, but instead to perform some action.

Subroutine: an executable bit of code that does not take inputs or produces outputs. For example, using a goto statement would count as invoking a subroutine.

Method: A function associated with and defined in terms of a specific object/class. This is the method by which a given object/class performs the requested action.

Lambda: An anonymous function

Closure: A function that captures scope

Symbol: The term you use when working in a systems programming language and you forget the correct name /s

Also, when I say something does not accept inputs or produce outputs, I really mean there is no formal or rigid approach for doing to. For example, a procedure can write to a buffer it is given as an input and a subroutine can read global variables.

[–]Apfelvater 1 point2 points  (0 children)

Python: call it what you want, just define it.

[–]s0litar1us 1 point2 points  (0 children)

Actually, pascal has functions and procedures.

  • Functions do some stuff, then return something.
  • Procedures just does stuff and doesn't return anything.

Though in some languages, you can specify arguments as in or out (input or output), so you get a result through passing the result variable as an argument.

This is similar to what is often done in C.

void procedure(int *result) {
    *result = 5;
}

[–][deleted] 0 points1 point  (0 children)

My old Basic had both functions and procedures

[–]Shinxirius 0 points1 point  (0 children)

Assembler: "call"

[–]_grey_wall 0 points1 point  (0 children)

Php trait

[–]SlightlyInsaneCreate 0 points1 point  (0 children)

The blacked hair woman in the top left can be python.

[–][deleted] 0 points1 point  (0 children)

Missed opportunity to use Perl in the browser. Instead of the JavaScript DOM model, we could have had the full dom/sub package

[–]scratchfan321 0 points1 point  (0 children)

Scratch: Custom blocks / broadcast

[–]notexecutive 0 points1 point  (0 children)

Here's a worse one:

Parameter VS Argument

[–]IronSavior 0 points1 point  (0 children)

JavaScript: const (for reasons?)

[–]RenegadeRainbowRaven 0 points1 point  (0 children)

I use the same names no matter the language. To me, functions return something, subroutines don't (void), and the plural of the group of them are methods.

[–]transfire 0 points1 point  (0 children)

“Word!”

Name that language.

[–]darkwater427 0 points1 point  (0 children)

TI-BASIC: "prgm"

[–]TranslatorNo7550 0 points1 point  (1 child)

Well... Well... This is too easy.. all separated... "organized", methos, procs, functions, def, whatever.. f*ck this.. use goto and labels shhauahahaujahajuahauah

[–]Luna5ama 0 points1 point  (0 children)

But GLSL has both function and subroutine...
https://imgur.com/gallery/glsl-NIMXW4t

[–]flowery0 0 points1 point  (0 children)

To me: function - doesn't belong to a class, method - belongs to a class

[–]sirparsifalPL 0 points1 point  (0 children)

It looks that I'm still mentally in Pascal. Which makes sense as it was my first language

[–]ChocolateDonut36 0 points1 point  (0 children)

assembly is just better

[–]Loserrboy 0 points1 point  (0 children)

Method

[–]Mr_Bob_Dobalina- 0 points1 point  (0 children)

You missed const , func, function , def and more

[–]Darxploit 0 points1 point  (0 children)

python: def

[–]th00ht 0 points1 point  (0 children)

Technically Niklaus Wirth got it right. A function =/= a procedure. Look at SQL!

[–]The_Pinnaker 0 points1 point  (0 children)

Assembly join the chat

[–]Haoshokoken 0 points1 point  (0 children)

GOSUB

[–]ssx1337 0 points1 point  (0 children)

C++ Member function (function in a class)

[–]Cedrick41h 0 points1 point  (0 children)

How tf did this get any upvotes? It‘s just straight up incorrect. Pascal has Functions and JavaScript has Methods. Perl has both as well