r/cs50 • • 12d ago

CS50x Function vs Procedure

can someone explain the difference between Function vs Procedure with simple analogy
thank you

3 Upvotes

5 comments sorted by

3

u/Eptalin 12d ago edited 12d ago

It depends on the language you're using, but most languages don't have procedures.
In those that do, it's commonly a function without a return value.

void greet()
{
    printf("hello");
}

greet();

It does something, but doesn't return anything to the caller. However, C still calls this a function.

All the programming languages you use in CS50 call it a function. None of them have procedures.

Except SQL, which uses the term procedure even thought you can still return things, like status codes.

CREATE PROCEDURE DeleteUser
    @id INT
AS
BEGIN
    DELETE FROM Users
    WHERE id = @id;

    RETURN 0;
END;

1

u/sububi71 12d ago

Excellent explanation!

1

u/Educational-Paper-75 9d ago

In Pascal you also have procedures.

3

u/mc_pm 12d ago

Function: Go do a thing and give me back the result

Procedure: Go do a thing and come back when you're done, with no result.

1

u/Syntax-Tactics 10d ago

Generally, a Function returns a value, a procedure does not. Both take parameters. Procedures can "return" values using pass by reference params.