Having trouble understanding logic of Functions

I'm using a book to teach myself, and one chapter is dedicated to understanding functions. However, when I look at the example it has given me, I can manipulate the code, but cannot understand how the user entered number gets passed to the prime function.

Here is the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <math.h>
using namespace std;

// Function must be declared before being used.


int prime(int n);

int main()
{
	int i;

	// Set up an infinite loop; break if user enters 0. Otherwise, evaluate n from prime-ness.

	while (1)
	{
		cout << "Enter a number (0 to exit) and press ENTER: ";
		cin >> i;
		if (i == 0)   // If user entered 0,
			break;   // EXIT
		if (prime(i))  // Call prime(i)
			cout << i << " is prime" << endl;
		else
			cout << i << " is not prime" << endl;
	}
	return 0;
}

// Prime number function. Test divisors from 2 to sqrt of n. Return false if a divisor found; otherwise, return true.

int prime(int n)
{
	int i;
	for (i = 2; i <= sqrt((double) n); i++)
	{ 
		if (n % i == 0) // If i divides n evenly, 
			return false; //n is not prime.
	}
	return true;  // If no divisor found, n is prime.
}



What I don't understand is when the user enters the number as the variable "i" in the main function, how does that value get passed on to the variable "n" in the prime function when I never declared an "n"? Also, the book keeps calling whatever is in the prototype (declaring function) parenthesis the argument list, but I'm not quite sure what exactly that means. Any help is appreciated.
Last edited on
You need not to declare the variable "n" in external function.

You need only declare the variable "n" in declaration of external function
int prime(int n);

And when you call the prime function, the compiler will understand that you use local variable "n".
closed account (z05DSL3A)
When your call a function, say prime(i), it is kind-of-like saying prime(n=i); you are assigning the value of i to the local variable n.

Edit:
orbit316 wrote:
Also, the book keeps calling whatever is in the prototype (declaring function) parenthesis the argument list, but I'm not quite sure what exactly that means. Any help is appreciated.
Out of interest what book are you reading? What it describes as an argument list I would say is a Parameter list.

Anyway, line 8 is the function prototype (function declaration). The parameter (argument) list is a single parameter in this case of type int [NB. the name for parameters is not needed in a prototype, i.e. int prime(int); is still valid]. You can declare multiple parameters in a list by using comma separation i.e. int func(int a, int b,int c); where int a, int b,int c is the parameter (argument) list.
Last edited on
So I could've named int "n" anything I wanted? What happens if you just put int within the declaration of the function without putting a name for the int? Would the user entered number still get passed off into the external function?

Also, I'm using the 2005 edition C++ without fear, and it seems very good so far. I'm just having trouble understanding this specific part, because I didn't understand how the number was getting passed off, and why they decide to just name int n and why it was that specific one. Thanks!
Yes.

I'm not sure what you mean by that...can you give an example?
When you declare the function prime at the top of the code, "int prime(int n);". What happens if I were to just do int prime(int); without naming the int? Will it still be able to pass off the user input number to the next function?
Well, when you make the prototype (that is what it is called), you do not have to name the parameters, since the compiler just needs to know the types.

bool prime(int); //ok, compiler doesn't need to know the names yet

However, when you actually define it (like at the bottom), you will generally want to provide names so that you can actually use the variables in your function. You don't have to provide names, but if you don't, then you can't use the variables.

1
2
3
bool primt(int) {
   //can't use the int here though because we haven't named it
}
Okay now I'm starting to understand. What if you leave int at the top without naming it, but then you name it int "n" at the bottom? Whats done to the prototype up top, must be done to the defined function at the bottom?
Whats done to the prototype up top, must be done to the defined function at the bottom?
Short answer: no. prime(int); //<- see ; means that the function is available to your program from here on in, and will be defined at a later time (and place.) It has nothing to do with what it "does." It only tells the compiler that you can use that function.
Last edited on
^e.g.

1
2
3
4
5
6
bool prime(int);

//later
bool prime(int x) { //defines the above function, names are unimportant
    //...
}
Topic archived. No new replies allowed.