i have programe in c but i need to use function and i have no idea how do it can any one help me please and one more thing i cant use loop inside case so it must be out

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int row ,n,c,temp,k,count=1;
    int input ;
    printf("1.UpRight\n");
    printf("2.UpSide\n");
    printf("3.Triangle\n");
    printf("4.Triangle with A\n");
    do 
    {
        printf ("Which option will you choose \n") ;
        scanf("%d" ,&input);
        printf("Enter the number of rows ");
        scanf("%d",&n);
        switch(input)
        {
        case 1:
            for(c=1;c<=n;c++)
            {
                for(k=1;k<=c;k++)
                {
                    printf("*");
                }
                printf("\n");
            }
        return 0;
        break;

        case 2:          
            for(c=n;c>=1;--c)
            {
                for(k=1;k<=c;++k)
                {
                   printf("*");
                }
                printf("\n");
            }
            return 0;
            break;

        case 3:         
            temp = n;
            for ( row = 1 ; row <= n ; row++ )
            {
                for ( c = 1 ; c < temp ; c++ )
                    printf(" ");
                    temp--;
                    for ( c = 1 ; c <= 2*row - 1 ; c++ )
                        printf("*");
                        printf("\n");
            }
            return 0;
            break;

        case 4:        
            temp = n;
            for ( c = 1 ; c <= n ; c++)
            {
                for( k = 1 ; k < temp ; k++)
                printf(" ");
                for ( k = 1 ; k <= c ; k++)
                {
                    printf("*");
                    if ( c > 1 && count < c)
                    {
                        printf("A");    
                        count++; 
                    }      
                }    
                printf("\n");
                temp--;
                count = 1;
            }
            return 0;
            break;

            default:            
                printf( "Bad input, quitting!\n" );
                break;
        }
    }
    while(true)
}

Recommended Answers

All 2 Replies

please help me

Functions are used to pack repeated code together. The general form of a function is this:

<return-type> <identifier>(<arguments>)
{
    <body>
}

This means for example that these two programs are effectively identical:

A version with no functions

#include <stdio.h>

int main()
{
    int mode,num;
    printf("Please choose to print (1) Increasing numbers or (2) Decreasing numbers: ");
    scanf("%d",&mode);
    if (mode!=1&&mode!=2)
    {
        printf("Error: invalid mode...\n");
        return 1;
    }
    printf("Please enter the starting number: ");
    scanf("%d",&num);
    if (mode==1)
    {
        //print increasing
        int i;
        for (i=0; i<=num; ++i)
            printf("%d\n",i);
    }
    else
    {
        //print decreasing
        int i;
        for (i=num; i>=0; --i)
            printf("%d\n",i);
    }
    return 0;
}

A version with functions: (note that void means 'nothing')

#include <stdio.h>

//Function to print numbers increasing
void printIncreasing(int num)
{
    int i;
    for (i=0; i<=num; ++i)
        printf("%d\n",&num);
}

//Function to print numbers decreasing
void printDecreasing(int num)
{
    int i;
    for (i=num; i>=0; --i)
        printf("%d\n",&num);
}

int main()
{
    int mode,num;
    printf("Please choose to print (1) Increasing numbers or (2) Decreasing numbers: ");
    scanf("%d",&mode);
    if (mode!=1&&mode!=2)
    {
        printf("Error: invalid mode...\n");
        return 1;
    }
    printf("Please enter the starting number: ");
    scanf("%d",&num);
    if (mode==1)
        printIncreasing(num);
    else
        printDecreasing(num);
    return 0;
}

The benefits of using functions are incredible. For one, they prevent code duplication, so you don't hurt your wrists as much. For another they simplify logic, instead of having your code be one monolithic chunk of statements, you can break it up into manageable bites. They also allow for special techniques to be used to solve problems in a different way (EG: recursion).

In general, you should try to split up your programs into specific tasks. Then write a function that does each one. While it is a small difference in small programs, in big programs it can be astounding the level of improvement.

For example, I have a C compiler that I wrote (to provide me with extra compilation information). The main() function looks something like this:

int main(int argc, char *argv[]) //don't worry too much about argc and argv
{
    FILE *input,*output;
    //some code to load files/parse arguments here

    compile(input,output,debug);
    fclose(input);
    fclose(output);
    return 0;
}

And that compile function itself looks something like this:

void compile(FILE *input, FILE *output)
{
    TokenList tokens = tokenize(input);
    LexemeList lexemes = parse(tokens);
    SyntaxTree code = build(lexemes);
    generate(code,output);
}

The general rule is: each function should do 1 specific thing, if it does more than one it should be split up.

Good luck.

PS: Don't forget the semicolon on line 85. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.