Functions

This is a discussion on Functions within the C Programming forums, part of the General Programming Boards category; I need main() to pass a string and int num to a function only once then the function must set ...

  1. #1
    Unregistered
    Guest

    Post Functions

    I need main() to pass a string and int num to a function only once then the function must set up a loop that will print that string
    num number of times - want to know if this code is right.

    void james (int,char[]);
    void main (void)
    {
    james(5,"hooman");
    }
    void james (int num, char name[])
    {
    int i;
    for(i=1;i<=num;++i)
    printf("%s\n",name);
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    looks fine except for the void main bit...
    try this:-
    Code:
    void james (int,char[]); 
    int main (void) 
    { 
    james(5,"hooman");
    return 0; 
    } 
    void james (int num, char name[]) 
    { 
    int i; 
    for(i=1;i<=num;++i) 
    printf("%s\n",name); 
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    This notation works technically:
    Code:
    for(i=1;i<=num;++i)
    But is better represented by:
    Code:
    for(i=0;i<num;++i)
    I compile code with:
    Visual Studio.NET beta2

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247

    Lightbulb

    Hi, Only problem I can see with your code is the incrementation in the for loop..... ++i will increment i before the first loop...therefore
    1 becomes 2 and the name will only print 4 times...be better to use i++, which increments i after statements within the loop are processed...
    for(i = 1, i <= num, i++)
    Also if you want to print name 5 times as in your code, it would be better to initialize i to 0 and get rid of the "=" in the conditional......
    for ( i = 0; i < num; i ++)
    Last edited by bigtamscot; 09-02-2001 at 11:57 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > want to know if this code is right.
    Get a compiler and ask it

    > ++i will increment i before the first loop...therefore
    No it won't.
    The increment step is performed at the end of the loop, assuming the loop is executed at all by evaluation of the control expression.

    for ( i = 0 ; i < num ; i++ )
    for ( i = 0 ; i < num ; ++i )
    for ( i = 1 ; i <= num ; i++ )
    for ( i = 1 ; i <= num ; ++i )
    All run the same number of iterations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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