When you have a function and need to scan for 2 different numbers, should the scanf be in the function or in the main() and why?

it depends on what you're trying to do:

when declaring variables in the main() procedure, is because you intend to use them throughout the whole program, though you can't use them in a function (unless you pass them as parameters by reference, not by value); when you declare them in a function, these variables are only useful as long as the function is running (meaning, they "die" at the end of the function); and if you declare the variables outside of any function or main() , they are Global variables, which can be used throughout all the program (meaning, main or functions).

I'm trying to use the numbers to do calculations in my function. I want user input to get those 2 numbers.

When you have a function and need to scan for 2 different numbers, should the scanf be in the function or in the main() and why?

question is flawed. you shouldnt use scanf for user input. use fgets() in conjunction with sscanf() or atol() or atof() or something similar.

see also: http://www.daniweb.com/tutorials/tutorial45806.html

and yes, you can get user input whereever you want to. depends on how you plan to use it what the most sensible location for it. if this is confusing to you, then just stick it in main() until you learn more about how subroutines work.

.

Edited 6 Years Ago by jephthah: n/a

When you have a function and need to scan for 2 different numbers, should the scanf be in the function or in the main() and why?

No, you should not put the input in the function. The function's purpose is to do calculations, so keep it simple. Maybe another function for input. But keep the different processes (input, calculation, output) separate. The more you do in the function, the harder it is to debug.

I'm trying to use the numbers to do calculations in my function. I want user input to get those 2 numbers.

Then you want the input before you call the calculate function.

This article has been dead for over six months. Start a new discussion instead.