Thread: a simple question on functions

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    a simple question on functions

    how do u convert a C program like the one below to a C program using function ?

    Code:
    #include <stdio.h>
    #include <conio.h>
    main()
    {
    int i,j;
    for(i=5;i>=1;i--)
    {
    for(j=i;j>=1;j--)
    printf("%d",j);
    printf("\n");
    }
    getch();
    }
    Note:The program output is an inverted right angled triangle starting from 5 and ending at 1

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You can start from here.
    here,here

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    lol i already know that i was just asking if someone can post the same code but in a function instead of main() ... u know the calls to functions .. i wanted to know how would it look like if its in a call to function program

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    see if you can fill in the blanks and call it in the right place:

    Code:
    void printTriangle(int i, int j)
    {
        ...
    }

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I'd prefer
    Code:
    void printTriangle(int height, int base)
    {
        ...
    }

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    not working... any ideas ?


    Code:
    #include <stdio.h>
    #include <conio.h>
    int printTriangle(int n)
    {
    int i,j;
    for(i=n;i>=1;i--)
       {
    for(j=i;j>=1;j--)
     printf("%d",j);
     printf("\n");
       }
    return n;
    }
    int main()
     {
    int n,j;
      printf("Please enter no. of rows");
      scanf("%d",&n);
       printf("%d",j);
       printf("\n");
     }
    getch();

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    When you want to print something to the screen, you call a function called printf, right? Well what about if you want to do something in your own function? Wouldn't you have to CALL THE FUNCTION?

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    got it thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about the C programming tutorial #1
    By elsheepo in forum C Programming
    Replies: 13
    Last Post: 01-19-2009, 08:59 PM
  2. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  3. Question about Downloading/Uploading Functions
    By TeCNoYoTTa in forum C++ Programming
    Replies: 0
    Last Post: 05-26-2008, 01:36 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. question about functions
    By abyssphobia in forum C++ Programming
    Replies: 6
    Last Post: 08-18-2004, 06:22 AM