i dont understand this loop and function

This is a discussion on i dont understand this loop and function within the C Programming forums, part of the General Programming Boards category; Why is this code not looping and is not passing the value the function. The output is zero and, i ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    62

    the loop is not passing value to the function

    Why is this code not looping and is not passing the value the function.
    The output is zero and, i would like to pass the number 1, 2, 3, 4, 5, 6, 7, 8, 9,
    to main. Why is this not working


    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>

    float get_value(void);

    int main ( )
    {
    system("cls");
    float a;
    a=get_value();
    printf("\n\n%1.1f",a);

    getch();
    }

    float get_value(void)
    {
    float a;

    for(a=0;a<10;a++)
    {

    return(a);
    }

    getch();
    }
    Last edited by joker_tony; 03-20-2008 at 08:31 PM.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    It wouldn't work because of two reasons:

    1. As soon as the function returns, it terminates its process and the control returns to main(), so it cuts the for loop when getvalue() returns 0.
    2. There must be a loop in main(). A function can only return one value at a time, so "a" will only have one value, which is 0.
    Last edited by samus250; 03-20-2008 at 08:44 PM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    You could do something like this:

    Code:
    #include<stdio.h>
    
    float get_value(void);
    
    int main ( )
    {
    	system("cls");
    	float a;
    	int x;
    	
    	for(x=1;x<10;x++){
    		a=get_value();
    		printf("\n\n&#37;1.1f",a);
    	}
    
    	getch();
    }
    
    float get_value(void)
    {
    	static float x=0;
    	
    	x++;
    	return x;
    }
    Is that what you want it to output?

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    62
    Thank for your help, but this is not what i want beccause i want to create a loop in the function prototype and pass it to main. and not have a loop in the main function

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Code:
    int get_values(int count)
    {
       int x;
       for(x=1;x<count;x++)
       {
          float a=get_value();
          printf("\n\n&#37;1.1f",a);
       }
       return count;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. My first game
    By homeyg in forum Game Programming
    Replies: 20
    Last Post: 12-22-2004, 04:25 PM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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