0

Can I use a return command instead of printf in the tables1 function, and still get the same output as it is giving me currently? The tables1 function is defined at the starting of the following code which presently executes without warnings or errors:-

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

float tables1(float a,float b)
{
    int i=0;
    for(i=0;i<=10;i++)
    {
        printf(" %.2f\n", a*i);
        printf(" %.2f\n ", b*i);
    }
    printf(" if all tasks not executed plz check the code \n");
    printf("just to check how the computer treated your number input as, lets print it again\n %.2f \n", a);
    return 0;
}


int main()
{
    float a,b;
    printf("enter the numbers whose first 10 multiples u want to see\n");
    scanf("%f", &a);
    scanf("%f", &b);
    printf("following below are the multiples \n\n ");

    tables1(a,b);
    return 0;
}

The output image is attached.

Also, when i wrote the above code initially, i did not write any return statement in the tables1 function definition, hence i got a warning during build, so just to see if it works, i wrote a return 0; compiled it, and the warning was gone yet, leaving me in dissatisfaction from a second query which is:- Doesnt the float function have a different kind of return statement, or is return 0 rightly used? i feel there may be couple of such instances(like mistakes) in my program that may have gone unnoticed by me.

Output of the code i have written above

8
  • please explain why you want to do this. Commented May 30, 2015 at 10:42
  • 2
    if you make tables1 a void function it doesn't need to return anything Commented May 30, 2015 at 10:45
  • because the reference materials i use, have a lot of simpler examples of int functions which use a return inside their definition to give outputs.......these are simple examples like ' return a+1; }' and it gives the output fine. But i couldnt come accross a float example of such type not even in some normal search i did on the net Commented May 30, 2015 at 10:45
  • 3
    I think you need to reiterate over your understanding of functions (or "procedures") in general programming... Commented May 30, 2015 at 10:46
  • 2
    To answer your first question (literally): no. The printf does something visible, return a; does not. It's also a nonsensical return value: it's the same a that you fed the function with, and it doesn't get modified. Commented May 30, 2015 at 10:49

1 Answer 1

1

You aren't using the return value of tables anywhere. Furthermore, tables already takes a float as input, hence you cannot really check inside tables whether that number matches the user's input, since the input (a string) is unknown to tables.

You might as well change the function to void tables and not return any value. The part of the code that deals with the user's input is in main, and there is no need to pass such details onto a function dealing with floats.

Meanwhile scanf does return a value which you are also ignoring – do check the documentation of each library function you are using, and determine whether the return value might be useful... (There are also other functions to parse numbers from strings, e.g., strtod.)

Sign up to request clarification or add additional context in comments.

5 Comments

yes i tried doing void, yes it works perfectly as u said, dint have to return anything, well that is a good alter solution, just that i wanted to concentrate on float type more extensively hence i wanted to know explicitly what should a float function return, which is not described properly anywhere that i tried to search, or atleast not in simple terms.
re. “what should a float function return“, depends entirely on the function. If you're using it only for its side-effects, such as printing tables, perhaps it should return nothing, or it can return a value to indicate success/failure. If it is a “true” function used to compute a value (e.g., the square root function), it should obviously return that value…
so i believe that means, "return 0;" would work for both int and float type functions, coz not returning anything for a float function is an obvious error/warning. Well this helped me know more, thanks.
A function that always returns 0 is quite pointless, and returning nothing (i.e., having a void function) is not an indication of error/warning, quite the opposite.
yes yes i get it, just that i meant not returnig anything in a float type function was giving me warnings coz it was not a void function but a float function......newz u got the point and i got what u said.....i guess most of my work would require only int and void type functions and may be one or two others (while i am learning C programming).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.