#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Help Starting C - Returning values from function


    Hi guys, new to the forum. Guess it means i should give a brief introduction. I took a computer science in highschool and dabbled in c++/visual basic back in '04, took a semister and dabbled in java in '10, and now im trying to dabble in C before dabbling back into C++, im more of a hobbiest when it comes to programming but i do enjoy it and and hopefully one day get more then just a 'dabble' into a language lol. Also wouldnt mind havin a study/hobby partner if anyone interested.

    Now that the introduction is over, back to programming :P, so im just starting off in C as i mentioned and starting out having some trouble understanding returning say an int value from another function. Heres what i have and i dumbed down the code to just figure out whats going on

    Code:
    #include <stdio.h>
     
    int main()
    {
        int num;
        get_number(num);
    	
    	printf("You chose option: %d", num);
    }
    
    int get_number(int num){
    	printf("Enter any number \n");
        scanf("%d", &num);
        printf("Entered character is %d \n", num);
    	
    	return num;
    }


    Now i can get the code to work this way which brings me to assume i might just be throwing a little bit of c++ language into my code? Not sure but heres what works:

    Code:
    #include <stdio.h>
     
    int main()
    {
        int num;
        	
    	printf("You chose option: %d", get_number(num));
    }
    
    int get_number(int num){
    	printf("Enter any number \n");
        scanf("%d", &num);
        printf("Entered character is %d \n", num);
    	
    	return num;
    }
    So can you not just call a function to return a value to change the variable? Help me understand the difference and whats going on in the C language at this point please. Thanks in advance
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    also just tested my waters with something out to see what else is going on

    #include <stdio.h>

    Code:
    int main()
    {
        int num;
        	
    	printf("You chose option: %d \n", get_number(num));
    	printf("Number after the printf callin the function: %d", num);
    }
    
    int get_number(int num){
    	printf("Enter any number \n");
        scanf("%d", &num);
        printf("Entered character is %d \n", num);
    	
    	return num;
    }
    this one just added a line to print the value of num after the printf statement that calls the function and the ouput is:

    Code:
    You chose option: 2
    Number after the printf callin the function: 0
    So i def just think i may be doing to much for the C or ??
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > printf("You chose option: %d \n", get_number(num));
    > printf("Number after the printf callin the function: %d", num);
    The thing to remember here is that C uses call by value.

    Calling get_number does NOT change num.
    So in the first instance, you're printing the return result of the function, whereas in the second you're printing the original (uninitialised) value of num.


    Also, passing num as a parameter to immediately overwrite it is a waste of effort.
    Consider
    Code:
    int main()
    {
        printf("You chose option: %d \n", get_number());
    }
    
    int get_number(){
        int num;
        printf("Enter any number \n");
        scanf("%d", &num);
        printf("Entered character is %d \n", num);
        return num;
    }
    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

IMN logo majestic logo threadwatch logo seochat tools logo