Functions - A Crash Course

NSBCoding :: Coding :: C++

Go down

Functions - A Crash Course

Post  Elusive on Wed Jun 08, 2011 2:23 pm

Sometimes you have a piece of code you might want to use many times. Other times you just have a very long program to write. Functions are useful in both cases. A function is basically a block of code given a name, like a variable. You can call that name many times, from different places in the same program.

Code:
void function1() // This is how a function is declared - the void means it's not returning anything and "function1" is its name.
{
    printf("Inside function\n");
}

int main()
{
    printf("Before function\n");

    function(); // This is 'calling' the function - imagine taking the code in the function and sticking it here. When the function ends, the program will resume from this point.

    printf("After function\n");
}

All functions can return something - it's like a variable that does calculations.

Code:
int function1() // We are now returning an int.
{
    int a = 5;
    return a; // Same as "return 5;".
}

int main()
{
    int x;
    x = function1(); // Imagine replacing the function with the returned value, in this case 5.
                            // x now equals 5.
    printf("%d", x);
}

An interesting feature of functions is related to the scope of a variable - the scope is where you can access it. If you declare a variable in main, you can access it from anywhere inside main - but not from other functions. If you declare a variable in any other function, you can only access it from within that function and it is destroyed when you exit the function.



Code:
// Note: this code will not compile because of errors indicated below.

int g = 4; // A variable declared outside all functions becomes global: it can be accessed from any function.

void function1()
{
    int x = 5;
    a = 2; // Does not work, because a is not declared within this function.

    g = 59; // Works, because g is declared outside all functions
}

int main()
{
    int a = 6;
    function1();
    printf("%d", x); // Does not work, because x is not declared here.

    g = 9001; // Works, because g is declared outside all functions
}

Now, because functions cannot access variables inside other functions, there needs to be a way (other than global variables) to share information.

Code:
int myfavefunction(int x, int y) // Variables declared inside the parenthesis are known as parameters.
{
    return x + y
}

int main()
{
    int a = 5, b = 3;
   
    printf("%d", myfavefunction(a, b)); // Note I can call the function from just about anywhere, and it works like a variable. The arguments passed correspond to the parameters: x == 5, y == 3. Only the values are passed, and will match up with the declared names on the "other side". The return value should be 8 (5 + 3).
}

Recursion refers to a function calling itself. For a much more detailed explanation, see here.

Code:
int function1(int a = 0) // You can specify a default value, this is the value a will have if nothing else is passed.
{
    if (a > 9000)
        return a; // A function will exit when it returns.
    return function1(a + 1); // This is essentially a kind of loop. It will call itself with a higher "a" value over 9000 times.
}

Now that the extremely convoluted crash course in functions is done, try to figure out how this recursive function for calculating Fibonacci numbers works.

Code:

int Fibonacci(int x)
{
    if (x == 0) return 0;
    if (x == 1) return 1;
    return Fibonacci(x - 1) + Fibonacci(x - 2);
}

Elusive
Newbie

Posts : 10
Join date : 2011-06-04

View user profile

Back to top Go down

Back to top

- Similar topics

NSBCoding :: Coding :: C++

 
Permissions in this forum:
You cannot reply to topics in this forum