Thread: Passing value of parameters in main funtion

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    Passing value of parameters in main funtion

    I wrote my own program to understand the basic of function calling and passing parameters

    Code:
    #include<stdio.h>void loop (unsigned int i)
    {
     for (i = 0; i < 10; i++)
     {
     }
    }
    int main(void)
    {   
        loop (6);
        return 0;
    }
    I think loop run 10 times in function but if I pass value 6 in main function then it will run only 6 times

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your function ignores the parameter, and just loops 10 times anyway.

    Give your parameter a decent name.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by Salem View Post
    Your function ignores the parameter, and just loops 10 times anyway.

    Give your parameter a decent name.
    I didn't get your point. Why does it ignores the parameter as per my knowledge when main function call to delay function then loop will run only 6 times. I think if I pass the value 2 in main function then loop will run 2 times. What's wrong in program ?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What's wrong in program ?
    Look at your function closely. Do you ever use the value you passed into your function?

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by jimblumberg View Post
    Look at your function closely. Do you ever use the value you passed into your function?
    what is meaning of this > Do you ever use the value you passed into your function?

    if possible can you give example

    I wrote this program for understanding calling function

    Code:
    #include<stdio.h>
    void loop (void)
    {
     unsigned int count;
     for (count = 0; count < 10; count++)
     {
      printf("Number :  %d \n", count);
     }
    }
    int main(void)
    {   
        loop();
        return 0;
    }
    Number : 0
    Number : 1
    Number : 2
    Number : 3
    Number : 4
    Number : 5
    Number : 6
    Number : 7
    Number : 8
    Number : 9

  6. #6
    Guest
    Guest
    And now imagine if you changed the function to:
    Code:
    void loop (unsigned int count)
    {
        // unsigned int count;   <-- no longer used
        for (count = 0; count < 10; count++) {
            ...
        }
    }
    What do you think happens? Consider that your function knew how to loop without being passed an argument.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Does it make any sense

    Code:
    #include<stdio.h>
    void loop (unsigned int number)
    {
     unsigned int count;
     for (count = 0; count < number; count++)
     {
      printf(" count : %d \n",count);
      
     }
    }
    int main (void)
    {
       loop(6);
       
       return 0;
    }
    count : 0
    count : 1
    count : 2
    count : 3
    count : 4
    count : 5

    I think I can pass any value
    Code:
    #include<stdio.h>
    void loop (unsigned int number)
    {
     unsigned int count;
     for (count = 0; count < number; count++)
     {
      printf(" count : %d \n",count);
      
     }
    }
    int main (void)
    {
       loop(6);
       loop(4);
       loop(2);
       return 0;
    }
    count : 0
    count : 1
    count : 2
    count : 3
    count : 4
    count : 5

    count : 0
    count : 1
    count : 2
    count : 3

    count : 0
    count : 1
    Last edited by vead; 01-19-2018 at 02:11 PM.

  8. #8
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    I'm please you got it right eventually.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. int main parameters
    By alyeska in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2009, 12:12 PM
  2. Changing how 'main' parameters are interpreted
    By Canadian0469 in forum C Programming
    Replies: 3
    Last Post: 10-07-2007, 01:00 AM
  3. HELP!!! parameters of funtion???????
    By WildLyxn in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2002, 11:19 PM
  4. parameters in main()
    By pnxi in forum C Programming
    Replies: 1
    Last Post: 10-09-2002, 02:52 AM
  5. passing parameters to main
    By jpchand in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2002, 02:27 PM

Tags for this Thread