Functions

What is the difference between a function and a variable? they both hold info.
and how a function may be used differently than a variable?
Last edited on
a function does not contain anything. A variable contains a value.

You obviously don't understand what a function is, so I will try to make it clear to you:

A functions is a means by which you can express a set of steps in terms of a given argument (or input) that results in a return value (or output). Functions don't store anything because they aren't objects.
Last edited on
I didn't understand the last part.
Have you checked out the tutorial on this site? There are some code examples on the page for functions that might help.

http://www.cplusplus.com/doc/tutorial/functions/

Functions allow to structure programs in segments of code to perform individual tasks.

In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define a function is:

type name ( parameter1, parameter2, ...) { statements }

Where:
- type is the type of the value returned by the function.
- name is the identifier by which the function can be called.
- parameters (as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example: int x), and in fact acts within the function as a regular variable which is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from.
- statements is the function's body. It is a block of statements surrounded by braces { } that specify what the function actually does.

1
2
3
4
auto lambda_function_example = [@Jacobhaha](void)->void{
    std::cout<< "Then just take the last sentence of what I said to you...  (-,-')"<< std::endl;
};
lambda_function_example();


see what I did there? ;)
Last edited on
@JAcobhaha

A functions is a means by which you can express a set of steps in terms of a given argument (or input) that results in a return value (or output). Functions don't store anything because they aren't objects.


like he said functions dont store anything ... look at the function pass by reference as the world itself...in every function you use, youre like reflecting it definition to your main function
A function contains code while a variable contains data.

functions are active (executed by the processor) while variables are passive
Functions don't contain anything, they just allow us to refer to a set of code through an alias and set of inputs and outputs.

If it helps, you can think of it that way though...
Topic archived. No new replies allowed.