UIU: Learn C by Examples : Functions


Program 1


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int reverse(int n)
{
    if (n == 0) return 0;
    else printf ("%d\n", n);
    reverse(--n);
}

int main ()
{
    int n = 5;

    reverse(5);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 2


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int fun(int n)
{
    if (n > 0) {
        fun(--n);
        printf("%d ", n);
        fun(--n);
    }
}

int main ()
{
    fun(3);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 3


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int summation(int n)
{
    if (n == 0) return 0;
    return n + summation(n - 1);
}

int main ()
{
    printf("%d\n", summation(5));

	return 0;
}

// @END_OF_SOURCE_CODE

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s