-5

I am trying to pass the parameters in c through a function. The version with a string argument is working fine but both versions with integer arguments are returning 1 as a result.

#include<stdio.h>

void main() 
{
    char s1[10];
    int a,b;
    clrscr();

    printf("name=%s\n",getname(s1));
    printf("mobile=%d\n",getmobile(a));
    printf("mobile=%d\n",getrno(b));

    getch();
}

getname(char s[10])
{
    printf("enter the name\n");
    gets(s);
    return ;
}

getmobile(int a)
{
    printf("enter the mobile number\n");
    scanf("%d",&a);
}

getrno(int b)
{
    printf("enter the rno\n");
    scanf("%d",&b);
} 
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Shreenivas
  • 155
  • 2
  • 6
  • 19
  • 4
    Look up pass by value and pass by reference... And return values as well... Start [here](http://stackoverflow.com/questions/2229498/passing-by-reference-in-c) – fvu Aug 14 '14 at 13:09
  • turn on all warnings? :) – Karoly Horvath Aug 14 '14 at 13:10
  • i tried all the ways but not working – Shreenivas Aug 14 '14 at 13:11
  • 3
    How about writing `return` statements in your functions, have you tried that? I guess no. – Erich Kitzmueller Aug 14 '14 at 13:12
  • 2
    of course it's not working, you failed to understand some basic concepts about the language. it's like driving a car without knowing what the pedals do. – Karoly Horvath Aug 14 '14 at 13:12
  • 2
    you may have tried all ways, but forgot at least one, the right one. Seriously now, *read* the question I linked to and *try* to understand why your code doesn't work, this stuff is quite crucial if you ever want to do something useful in C. – fvu Aug 14 '14 at 13:12
  • yes i got it i missed the return function .thank you very much – Shreenivas Aug 14 '14 at 13:14
  • There are a little over 300 million 6 letter words (all lowercase). I guess you gave up a bit too soon when "trying all the myriad ways". – Jongware Aug 14 '14 at 13:14
  • Your `get` functions don't return the value read as a return value as your `printf` expects them. Rather, they *attempt* to return them as arguments (although your integer cases won't work since you are reading them into local parameters). Have you been through any C tutorials or classes? – lurker Aug 14 '14 at 13:16

2 Answers2

3

The reason why your getname works is but getrno doesn't is because of pass-by-reference vs. pass-by-value semantics and because arrays, like s1 decay to pointers. These are important concepts to understand if you want to program in C.

Think of it like this: When you call getname it accepts a local copy of the address of a buffer. The function then writes into the buffer itself. But when you call your getrno the function accepts a local copy of an integer and reads the value into that local copy, so that nothing changes in the program outside.

@askmish has proposed a good solution, but I would strongly advise something like this instead:

// getrno will prompt the user to enter the rno and will store it into the variable
// pointed to by b. If the function returns 1 then a value was successfully read. 
int getrno(int* b)
{
    // make sure that the pointer looks valid
    if (b == NULL)
        return 1;

    // prompt the user to enter the text
    puts ("enter the rno: ");

    // Note the use of a single space at the beginning of the format string
    // which is used to consume any whitespace (including return characters
    // that might be present)
    if (scanf (" %d", b) == 1)
        return 0;

    // We couldn't read one integer. Return an error.
    return 1;
}

int main() 
{
    int x;

    if (!getrno (&x))
        printf ("rno = %d\n", x);
    else
        printf ("failed to get rno!");

    return 0;
}

You ask how to go about doing floating-point numbers. The solution is to write a function which accepts, as necessary, either a float or a double pointer and which then calls scanf with the correct format specifier, to read the value into that pointer. This function would look very much like the getrno I showed you above.

Community
  • 1
  • 1
Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
-1

Functions should be written like this, for example:

        int getrno(int b)
        {
                printf("enter the rno\n");
                scanf("%d",&b);
                return b;
        } 

to get a return value aka, it must have a return-type and a return statement returning value of the specified return-type. Your code had both missing.

I would also suggest to read a good book in C or atleast this page in wikibooks, to understand better and writing better code.

askmish
  • 6,464
  • 23
  • 42
  • ya right answer and I tried to print with float data type but i am getting error type mismatch in redeclaration . how can achieve with float – Shreenivas Aug 14 '14 at 13:21
  • 1
    @user2503585: Note that before `getrno` he declared the return type. Additionally, you should declare the function __before__ you use it. Otherwise, if your compiler allows code to compile using the C standard from 1989, it will assume that the return type is an `int`. – Bill Lynch Aug 14 '14 at 13:22
  • 3
    In addition to what @sharth said do not that what you describe is *one* way to get a return value. In this case, it's not a particularly good idea, because you do no error detection, and even if you did, there's no way to *sensibly* return an error. The better approach here, is to use pass-by-reference (that is, make `getrno` accept an `int*` and the `&b` can then go away - just use `b`) and to check the return of `scanf` and return a success or failure status code as appropriate. – Nik Bougalis Aug 14 '14 at 13:22
  • this will pass the value of an uninitialized variable to a function. That's not good. It is also not necessary. You can return the value of a local variable. – mch Aug 14 '14 at 13:28
  • @NikBougalis A good suggestion,but I would start with simple solution(like a function with pass by value) and then understand pointers and then use `pass by reference`. The OP is supposedly a beginner, keeping that in mind. – askmish Aug 14 '14 at 13:34
  • 1
    AT downvoters: Please read http://stackoverflow.com/help/why-vote, to know when to downvote. I believe the answer is sufficient and relevant, to the question posted. If you think otherwise, please leave a comment. – askmish Aug 14 '14 at 13:45
  • @askmish I'd be curious to know why the downotes as well. – Nik Bougalis Aug 14 '14 at 13:58