Function Helo

This is a discussion on Function Helo within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { int n, i, sum; printf("Enter a value for n!: "); scanf("%d", &n); sum = ...

  1. #1
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72

    Function Help

    Code:
    #include <stdio.h>
    int main(void)
    {
    	int n, i, sum;
    	printf("Enter a value for n!: ");
    	scanf("%d", &n);
        sum = n;
    
        for( i = 1; i < n; i++ )
            sum = sum * ( n - i );
    
            printf("n! is %d\n", sum);
    }
    In main() you will still prompt the user for a positive integer, n, but then you will pass it to a function that calculates n! and then returns the value to main() for printing.

    How do I change this into a function?
    Last edited by BB89; 10-18-2009 at 03:50 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Have you looked at functions at all? In your book? In tutorials?

  3. #3
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    A little bit, why?

    But what is a good tutorials site?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why? Because you said you wanted to make some of your code into a function. So we check that you even know what a function is. You can look at this very site for some tutorials. Essentially whatever code calculates your value (in this case, n!) you put in a separate function to be called from whatever piece of code needs it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21