Function to return user input in C

This is a discussion on Function to return user input in C within the C Programming forums, part of the General Programming Boards category; Firstly, I'd like to say that I'm relatively new to functions and my course standards state that I'm now supposed ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    10

    Function to return user input in C

    Firstly, I'd like to say that I'm relatively new to functions and my course standards state that I'm now supposed to relay information from the user to the program via functions, this means that I'm no longer able to use 'printf/scanf' combos in the main function.

    Is it possible to do it via functions? I've tried the following method for example, but to no avail. It's confusing me.

    Code:
    #include <stdio.h>
    
    int getlowR();
    
    
    
    
    int main(void)
    {
    
      getlowR();
    
    
      printf("Low range: %d, High range: %d", lowRange);
    
    
      return(0);
    }
    
    int getlowR()
    {
      int lowRange;
    
      printf("Enter the low range value:");
      scanf("%d", &lowRange);
      return(lowRange);
    }

    I think the following works but then lowRange is native to the function, isn't it? How can I use it outside the function? For example, how would I go about actually printing the value acquired by the function in main?

    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Essentially, getlowR() returns an integer that you can assign to a variable in main(). Ie.
    Code:
    int a = getlowR();
    The longer explanation is twofold:
    1. The variables declared in a function (e.g. lowRange) are local to that function - which means lowRange doesn't exist in main(), nor can main see it.
    2. To understand any expression, you can evaluate all the operands in the expression by substitution. For non-void functions, this means substitute the function call with the value that the function returns.
    So as an example, let's say getlowR() returns the integer 5. Then in main() we substitute the function call with 5. Then you have the statement
    Code:
      5;
    which as you can see is meaningless. So the gist of it is, that functions that return values can be used in any expression that is valid for that return type.

    It should be clear that void functions don't return values and therefore shouldn't be used in expressions.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Thanks, I kind of get it, but I have another quick question.

    In
    Code:
    int getlowR()
    {
      int lowRange;
     
      printf("Enter the low range value:");
      scanf("%d", &lowRange);
      return(lowRange);
    }
    for example, do I still have to declare the variable in the function or can it directly send it to a variable in main (assuming I set it to one)?

    Edit: I just got it to print the results via another function! I'm getting the hang of functions now
    Last edited by tuxdeep; 02-26-2013 at 10:16 PM.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    That's a good question. Sometimes you don't need local storage for the value you're returning. In your case, scanf requires someplace to store the value read, so there's no getting around that.

    But assume a and b are integers and foo() is a another function. We can return expressions directly using a, b, and foo().
    Code:
    return a + b  ;
    ////////////////
    return 2*a + 3*b ;
    ///////////////
    return a < b;            //This takes advantage that the expression a < b evaluates to 1 if true, 0 if false.
    //////////////
    return a + foo( a, b );     //Here we call foo with a and b as arguments, and we will return the sum of a and the value that foo returns
    Hope that gives you some ideas.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Thanks a lot, you've been a great help. I'll practice some more tomorrow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-09-2012, 11:23 AM
  2. Compare user input and function with array and bubblesort
    By Gil Carvalho in forum C Programming
    Replies: 15
    Last Post: 06-15-2012, 01:32 PM
  3. Replies: 4
    Last Post: 08-31-2009, 11:04 AM
  4. Replies: 4
    Last Post: 06-04-2009, 01:36 AM
  5. How do I allow a user to input a function?
    By bananasinpajama in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2005, 11:11 AM

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