Thread: How do you make calls?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Question How do you make calls?

    It asked to write a function displayDigit() that will ask the user for an integer. The function will then print out the 1-digit, 10-digit, and 100-digit. The function will also print the average
    of the 1-digit, 10-digit, and 100-digit.

    I got this part and the code is:
    Code:
    #include <stdio.h>
    
    int main () {
      int iVar;
    
      printf( "Enter an integer: " );
      scanf( "%d", &iVar );
      printf( "The 1-digit is %d\n", (int) (iVar) % 10 );
      printf( "The 10-digit is %d\n", (int) (iVar / 10) % 10 );
      printf( "The 100-digit is %d\n", (int) (iVar / 100) % 10 );
      printf( "The average of the three digits is %d\n",
              ( ((int) (iVar) % 10) + ((int) (iVar / 10) % 10) +
              ((int) (iVar / 100) % 100) )/3 );
      return 0;
    }
    It asked me to write a program in main() that will make two (2) calls to displayDigit().
    I'm not sure how to make the function called displayDigit() and make 2 calls.

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    You already know how to make function calls, what you need to know is how to define prototypes and function definitions. There is a C tutorial on this site which I'm sure explains it.

    As far as your code goes, I would recommend NOT casting your calculations to an int. Why? Because an int % int returns an int, there is absolutely no need to cast an int to an int and it makes the code hard to read.

    I would imagine your prototype to be something like:
    Code:
    void displayDigits(int num);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a function make calls to 2 other functions?
    By JFonseka in forum C Programming
    Replies: 12
    Last Post: 08-19-2007, 08:40 AM
  2. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  3. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  4. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM