When programming, it is important to know which variables you can use where. I'll explain.
Let's say you declare a global integer x, and you define a function myFunct(). In the definition of myFunct(). You would obviously be able to use this integer x. But does it go the other way around? That is, can you use variable declared inside a function outside the function?
Simple answer. No.
When you call a function, the "flow" of your program halts so that it can go run your function and then return with the results. Let's say we have a function add2()
int add2(int x)
{
int y = 2;
return x+y;
}
Yes, I know there's no reason to declare this variable y, but it's for the sake of the example. In this case, The variable called y only "lives" until the end of the function. After that, this variable no longer exists, and hence it can no longer be referred to (Things like this can be overcome using the "static" keyword. I'll mention this sometime). In fact, this variable x no longer exists either. x is just a temporary holder for a value that is used while the function is running!
Functions are just one example of where knowledge of a scope could be useful. Let's say you were running a for loop.
int i;
for(i=0;i<5;i++)
{
//do stuff
}
Clearly, this is just an average for loop. Runs whatever code between its curly brackets 5 times. Now check this out:
for(int i=0;i<5;i++)
{
//do other stuff
}
Can you see the difference?
The difference is that in the first case, the variable i would be usable even after the for loop was executed. In the second case, i "dies" as soon as the loop terminates. Personally, I like to use the second method just because it allows me to use the same identifier for all my loops (assuming they're not nested) without fear of that identifier already being declared and used for another loop. I also just find it neater ;)
Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts
Sunday, 20 March 2011
Friday, 18 March 2011
Simple Functions
Functions are the basis for writing programs in C. They allow you to efficiently run repeated lines of code. For example, take a look at this mathematical function:
f(x) = x2
This function takes any real number x and squares it. Another way of writing this function could be:
f(x) = x*x
In C. Functions have 3 "Parts":
1) The function prototype which tells the program what to expect when the function is defined.
2) The function definition which tells the program how to execute the function.
3) The function call, which actually runs the function.
For this example, let's look at this square example. To declare a function, you must specify its return type (more on this is a sec) and then give the function an identifier and specify its parameter type. The parameters are the values that get passed into and used by the function (in f(x), our parameter is x). Here's a function prototype for our square function.
int square(int);
Later in your program, usually after your main method (unless you decide to declare it before, in which case a function prototype is not necessary), you will include your function definition. Here's the definition for square.
int square(int x)
{
return x*x;
}
A really simple function. The program will take whatever value gets passed into the function. That value is now represented by x. The function now calculates the square of x and returns the value back to wherever the function was called. Here's an example showing how to call this function
#include<stdio.h>
int square(int);
int main()
{
y=2;
printf("The square of %d is % d.\n",y,square(y));
return 0;
}
int square(int x)
{
return x*x;
}
Compile this and you'll see that the output of the program is:
The square of 2 is 4.
Notice how to call the function, you simply write the function name and then the values you want to pass into the function (These are called the arguments).
This function takes any real number x and squares it. Another way of writing this function could be:
f(x) = x*x
In C. Functions have 3 "Parts":
1) The function prototype which tells the program what to expect when the function is defined.
2) The function definition which tells the program how to execute the function.
3) The function call, which actually runs the function.
For this example, let's look at this square example. To declare a function, you must specify its return type (more on this is a sec) and then give the function an identifier and specify its parameter type. The parameters are the values that get passed into and used by the function (in f(x), our parameter is x). Here's a function prototype for our square function.
int square(int);
Later in your program, usually after your main method (unless you decide to declare it before, in which case a function prototype is not necessary), you will include your function definition. Here's the definition for square.
int square(int x)
{
return x*x;
}
A really simple function. The program will take whatever value gets passed into the function. That value is now represented by x. The function now calculates the square of x and returns the value back to wherever the function was called. Here's an example showing how to call this function
#include<stdio.h>
int square(int);
int main()
{
y=2;
printf("The square of %d is % d.\n",y,square(y));
return 0;
}
int square(int x)
{
return x*x;
}
Compile this and you'll see that the output of the program is:
The square of 2 is 4.
Notice how to call the function, you simply write the function name and then the values you want to pass into the function (These are called the arguments).
Tuesday, 15 March 2011
Back to basics
So I've been getting some comments and messages about my posts being too advanced, So I've decided that it would probably make more sense to start C from the beginning. In this post, I'll show you how to write a basic "Hello World!" program and explain the ins and outs of it. Some things will just be touched on right now and more fully explained in later posts.
#include<stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
To fully understand how the program works, I'll explain its components.
#include<stdio.h>
#include is a macro. This particular macro goes and finds a file on your computer, in this case stdio.h. This file is what we call a header file. Basically, this file contains a bunch of function definitions (again, more on this later). The #include macro tells the program to go look in the folder on your computer containing your standard C library header files and to find stdio.h. When you hit the "build" button, the compiler will take your source code and compile it into machine code, but even before that, something called the preprocessor (we'll mention this again much later) will physically copy all the text from stdio.h into your program. Now when your program gets compiled, the compiler sees all this code in your program, so it actually becomes a part of your program.
int main()
{
}
Functions are very important in any programming language. Every program in C must have a main function. Everything contained within the curly brackets will be code executed during runtime.
printf("Hello World");
This is a call the function which prints to the screen. This printf function is defined within the stdio.h file. Having included that file, we are able to call this function. Here, we can see that the text we are printing to the screen is "Hello World".
return 0;
To indicate to the operating system that the program has terminated successfully (ie. No runtime errors occurred/The program did not crash/etc) we return 0 at the end of out main. This signifies the end of our program.
I hope this has been useful to those not yet fully comfortable with C.
#include<stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
To fully understand how the program works, I'll explain its components.
#include<stdio.h>
#include is a macro. This particular macro goes and finds a file on your computer, in this case stdio.h. This file is what we call a header file. Basically, this file contains a bunch of function definitions (again, more on this later). The #include macro tells the program to go look in the folder on your computer containing your standard C library header files and to find stdio.h. When you hit the "build" button, the compiler will take your source code and compile it into machine code, but even before that, something called the preprocessor (we'll mention this again much later) will physically copy all the text from stdio.h into your program. Now when your program gets compiled, the compiler sees all this code in your program, so it actually becomes a part of your program.
int main()
{
}
Functions are very important in any programming language. Every program in C must have a main function. Everything contained within the curly brackets will be code executed during runtime.
printf("Hello World");
This is a call the function which prints to the screen. This printf function is defined within the stdio.h file. Having included that file, we are able to call this function. Here, we can see that the text we are printing to the screen is "Hello World".
return 0;
To indicate to the operating system that the program has terminated successfully (ie. No runtime errors occurred/The program did not crash/etc) we return 0 at the end of out main. This signifies the end of our program.
I hope this has been useful to those not yet fully comfortable with C.
Subscribe to:
Posts (Atom)