Category:C: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) |
Puppydrum64 (talk | contribs) No edit summary |
||
Line 82: | Line 82: | ||
//Define it to equal the output of the function sum using the previously defined variables "a" and "b" as arguments. |
//Define it to equal the output of the function sum using the previously defined variables "a" and "b" as arguments. |
||
//This is only valid if the return type of the function "sum" matches the type of the variable "c."</lang> |
//This is only valid if the return type of the function "sum" matches the type of the variable "c."</lang> |
||
===Declaring vs. Defining=== |
|||
This is a very unintuitive aspect of C that often confuses new users. Declaring a variable or function tells the compiler that a function may exist. Defining a variable or function assigns it a value or procedure, respectively. Compare the two examples below: |
|||
<lang C>int a; // The variable "a" has been declared, but not defined. |
|||
a = 2; // Now the variable has been defined.</lang> |
|||
<lang C>int a = 2; //The variable "a" has been both declared and defined.</lang> |
|||
* You cannot define a variable without declaring it first. |
|||
* Before a variable can be used, it must be defined. |
|||
Functions work the same way. You can declare a function without defining what it does. |
|||
<lang C> int foo(int bar); |
|||
// The function foo was declared. It takes an integer as an argument and returns an integer. |
|||
// What it actually does is currently unknown but can be defined later.</lang> |
|||
==Citation== |
==Citation== |
||
*[[wp:C_%28programming_language%29|Wikipedia:C (programming language)]] |
*[[wp:C_%28programming_language%29|Wikipedia:C (programming language)]] |