C lang:Definition function

Ax_note

in parameter for show_n_char() is formal parameter

C lang:Definition function_第1张图片

Aa_Definition function

#include 
#include 
#define NAME "ENOMOTHEM, INC."
#define ADDRESS "101 Beijing China"
#define PLACE "Megapolis, CA 00000"
#define WIDTH 40
#define WJXNUM 40
#define SPACE ' '

// function protype
void show_n_char(char ch, int num);

int main(void)
{
    int spaces;
// function call
    show_n_char('A',WJXNUM);
    printf("\n");
    putchar('\n');
    show_n_char('*', WIDTH);
    putchar('\n');
    show_n_char(SPACE, 12);
    printf("%s\n", NAME);
    spaces = (WIDTH - strlen(ADDRESS)) / 2;

    show_n_char(SPACE, spaces);
    printf("%s\n", ADDRESS);
    show_n_char(SPACE, (WIDTH - strlen(PLACE)) / 2);

    printf("%s\n", PLACE);
    show_n_char('*', WIDTH);
    putchar('\n');

    return 0;
}
//function definition
void show_n_char (char ch, int num)
{
    int count;

    for (count = 1; count <= num; count++)
        putchar(ch);
}

C lang:Definition function_第2张图片

Ab_Used return transport parameter             What F*k

#include 
int imin(int, int);

int main(void)
{
    int evil1, evil2;

    printf("Enter a pair of integers (q to quit):\n");

    while (scanf("%d %d", &evil1, &evil2) == 2)
    {
        printf("The lesser of %d and %d is %d.\n", evil1, evil2, imin(evil1, evil2));
        printf("Enter a pair of integers (q to quit):\n");

    }
    printf("Bye.\n");

    return 0;
}

int imin(int n, int m)
{
    int min;

    if (n < m)
        min = n;
    else
        min = m;

    return min;
}

C lang:Definition function_第3张图片


Ac_Tail recursion and for relize factorial

Ac_a Factorial

  • 0!=1
  • 1×2×3×4×…×(n-2)×(n-1)×n=n!
#include 
long fact(int n);
long rfact(int n);
int main(void)
{
    int num;

    printf("This program calculatess factorials.\n");
    printf("Enter a value in the range 0-12 (q to quit):\n");
    while(scanf("%d", &num) == 1)
    {
        if (num < 0)
            printf("No negative numbers, please.\n");
        else if (num > 12)
            printf("Keep input under 13.\n");
        else
        {
            printf("loop: %d factorial = %ld\n", num, fact(num));
            printf("recursion:%d factorial =%ld\n", num, rfact(num));
        }
        printf("Enter a value in the range 0-12 (q to quit):\n");

    }
    printf("Bye.\n");
    return 0;
}

long fact(int n)
{
    long ans;

    for (ans = 1; n > 1; n--)
        ans *= n;

    return ans;
}
long rfact(int n)
{
    long ans;

    if (n > 0)
        ans = n * rfact(n - 1);
    else
        ans = 1;
    return ans;
}

C lang:Definition function_第4张图片


Ad_recursion and inverted order

eg.
1. Oct 234
= 2x10^2+3x10^1+4x10^0
2. Bin 101
=1x2^2+0x2^1+1x2^0
3.5%3 = 2(1 to 2)
moda%b=a-(int)(a/b)*b

#include 

void to_binary(unsigned long n);

int main(void)
{
    unsigned long number;
    printf("Enter an integer (q to quit):\n");
    while (scanf("%lu", &number) ==1)
    {
        printf("Binary equivalent:");
        to_binary(number);
        putchar('\n');
        printf("Enter an integet (q to quit):\n");
    }

    return 0;
}

void to_binary(unsigned long n)
{
    int r;

    r =n % 2;
    if (n >= 2)
        to_binary(n /2);
    putchar(r == 0 ? '0' : '1');

    return;
}

C lang:Definition function_第5张图片

你可能感兴趣的:(C lang:Definition function)