Thread: Passing Variables to Functions -- Invalid in C99

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    6

    Passing Variables to Functions -- Invalid in C99

    This latest C book by Greg Perry needs updating, especially in the last chapters. Forgive my noob question but I'm trying to make this program work:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main() {
    	
      int i; 
    
    
      printf("Please enter an integer...\n"); 
      scanf(" %d", &i); 
    
    
      // Now call the half function, passing the value of i
      half(i)
    
    
      // Shows that the function did not alter i's value
      printf("In main(), i is still %d.\n", i); 
    
    
    return(0); 
    
    
    }
    
    
    /*******************/
    /* Sometimes putting dividers like the one above is a nice break between your different functions. */
    
    
      half(int i) { // Receives the value of i
    
    
      i = 1 / 2; 
      printf("Your value halved is %d.\n", i); 
      return; // Returns to main
    
    
    }
    What's the correct way to make the 'half' function work?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void half(int i);   //// need prototype of function
    
    int main() {
      int i; 
    
      printf("Please enter an integer...\n"); 
      scanf(" %d", &i); 
    
      // Now call the half function, passing the value of i
      half(i);   //// need semicolon here
    
      // Shows that the function did not alter i's value
      printf("In main(), i is still %d.\n", i); 
    
      return 0; 
    }
      
    //// If you want your function to have no return value then
    //// you need to explicitly make it void.
    void half(int i) { // Receives the value of i
      i = i / 2;    //// presumably you mean i / 2 here
      printf("Your value halved is %d.\n", i); 
      return; // Returns to main    //// unnecessary, returns at ending brace anyway
    }
    Last edited by algorism; 05-10-2016 at 07:29 PM.

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    6
    Thank you, algorism!

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Is that a new book? It seems like pre-standard 80s C to me...
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by GReaper View Post
    Is that a new book? It seems like pre-standard 80s C to me...
    If it's the 3rd edition of "C Programming Absolute Beginner's Guide" from 2013, then it's supposed to be "updated for C11". The 2nd edition is from 1994, so it should still be C89. However, this free sample chapter from the 3rd edition still doesn't put "int" before main.
    http://ptgmedia.pearsoncmg.com/image...0789751984.pdf
    (see page 14)

  6. #6
    Registered User
    Join Date
    May 2016
    Posts
    6
    Code:
    /* assign value of (i / 2) to i's memory */
    void half(int *i)
    {
    	*i = (*i / 2);
    }
    
    
    int main()
    {
    	int number = 400;
    	half( &number );
    	printf("number=%d\n", number);
    	return 0;
    }
    to change its value you have to pass its address or assign the return value of the function to your variable.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    @wrvc
    to change its value you have to pass its address or assign the return value of the function to your variable.
    Yes, this is true, but please read the code the OP posted:

    // Shows that the function did not alter i's value
    The purpose of the code posted was to illustrate that the variable, 'i' in the two functions are different, and altering the one in half() has no affect on the one in main(). The chapter on pointers would then cover passing the address of 'i' to alter the one in main().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing variables among c functions
    By amaturequestion in forum C Programming
    Replies: 9
    Last Post: 03-31-2015, 05:11 AM
  2. passing variables between functions
    By sababa.sababa in forum C Programming
    Replies: 8
    Last Post: 11-11-2012, 08:52 AM
  3. help with passing variables to different functions
    By Draylath in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2012, 01:22 PM
  4. passing variables between functions
    By owi_just in forum C Programming
    Replies: 6
    Last Post: 05-08-2005, 09:12 AM
  5. Replies: 6
    Last Post: 05-06-2003, 03:08 PM

Tags for this Thread