Question about functions :)

I was just wondering why at the end of functions there is a blank (), what is supposed to go in here.
these brackets contain the parameters of the function, ie values that the function requires in order to operate. The brackets are only blank is the function takes no parameters.
Hope this explains a little :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

//prototype
int x(int y);

int main()
{
	int z = 10;

	cout << x(z);// sends z to the function and then prints the output of the function

	cin.get();//pausing program
	return 0;
}

int x(int y){//y = z (=10)
	return y/2;//returns the received variable divided by 2
}


output:
5
Thanks, can you give me a simple example of when a function would have values inside its paremeters, i didnt quite understand the above example, thanks tho
Basically if you need to use something from main (or another function) in your function, you use parameters :)
Otherwise you don't.
@TpOreilly : Can my example be any more simplistic ? :p

I suppose you should look up how to call a function ^^

int x (int y); = new function

x() function is being called and given a variable z(which = 10) as int y in the cout << x(z);

when the x() is called ,it executes it's code (which is dividing the received value by 2) and then sends back the result , which is being cout'ed in main() function again.

This is the best I can explain it...
Faff. I can make it easier (in terms of self-explanatoriness):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
void foo() // void indicates that this function does not return anything
{
   cout << "No input" << endl;
}

void bar(int a)
{
   cout << "Input was: " << a << endl;
}

int main()
{
   foo(); // Call the first function
   bar(3); // Call the second function with a parameter
   cin.get();
   // Trying to call bar without a parameter will cause a compiler error
   // Doing the inverse with foo, adding parameters, generates an error too
   return 0;
}
Erm, ok

What is the point of declaring something inside the functions parameters, is it not the same as doing it inside the function itself?
Here is a simplified answer.

What the main program is doing is hidden from the function, and what the function is doing is hidden from the main program. Because of this the two need a way to communicate.

Usually it is safe to think that parameters are how the main program gives data to a function, and that return values are how the function gives data back to the main program.

The link firix posted will explain how functions work much more precisely.

Think of a function like an bank ATM and a credit card.
1
2
3
4
5
6
7
/* This is not compilable*/

int ATM(long Bank_Card_Number) // Like inserting a bank card
{
      cout << Bank_Card_Number; // Prints number on bank card to the console
      return Money_In_Savings_Account; // Gives you lunch money
}

This code won't compile for a number of reasons I won't go into unles you ask me too. Long is a type of integer. You are passing in an integer called Bank_Card_Number. The function does things with it. Then returns another. But Functions aren't just limited to integers. You can pass a number of things into a function and return just as wide a variety of things from it as well.
@Kyon: Kinda tried giving a example which has some use in some way :p
Which is in this case dividing a variable in main by 2 :D

@TpOreilly: Why using a function? :
1)Assume you want to make a prog which solves a bigger formula/action, and this formula/action has to be repeated multiple times, for each time you want to do this action you just gta link it to the function, this would save you some time ( you could also type over this action each time over)

2)It keeps your main() 'clean' and easier to understand.

3)And I think it can make your prog faster aswell (not rly sure about that, but I think i read it somewhere)

..) and there are probebly a lot more reasons which don't come up at me at this moment :)
erm, im abit tired atm, but this if what i got from it, if im wrong, then ill re-read it in the morning lol :)

So if i put a variable in a parameter, not only would the function its in would be able to use it, but also othersss? whereas if i just put it INSIDE the function, only that function could use that variable???

am i getting closer?
No. Giving a function parameters just gives it temporary access to variables outside of the function, giving it parameters though does not let other functions use those variables too unless they take their own parameters or the said variables are declared in the same scope as the other functions.

Also note that when I say it gives it access to the variables outside the function, if you pass a variable in by value (which is in the normal way as below)

 
void func(int num);


the function makes a copy of num to use inside the function body, so any changes you make to num inside the function will not change the original which you passed in.
It would be better said that functions allow you to reuse code. Here are some examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int functionOne(int a, int b){//a,b are copies of the variables from main
   int result;
   //100 lines of annoying code
   return result;//A copy of result is stored in the main program
}

int functionTwo(int a, int b){
   int result;
   //A different function with vars named a,b, and result is ok since it is hidden from the other function
   //a,b, and result can have different values in this function than in functionOne
   return result;
}

int main(){
int x=2;
int y=4;
int z = functionOne(x,y);//Vars x,y are copied inside the function to a,b -and z is a copy of result
//This next function can also get copies of x,y
int a = functionTwo(x,y);//main can also have a variable named 'a' since it can't see the other functions
x = functionOne(a,z);//Here we can use different values without having to rewrite 100 lines of annoying code

return EXIT_SUCCESS;
}

I should probably say that just because you can give variables the same name doesn't mean you should. You should always try to write your code so it is as clear as possible, and reusing names too much can make it confusing. If it starts to get confusing, start using more names.

EDIT: Clarification
Last edited on
Here is a really simple function. Though it would be just as easy to write:
1
2
int sum;
sum = 1 + 1;

you could also do this:
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
#include <iostream>//has to be used to use cout and other types of things to print to the //screen.
using namespace std;//useful for beginning programers so they don't have to use std:: in front
// of things in the std namespace. You shouldn't worry about namespaces for now. 
int add(int first_number, int second_number)
{
      int sum;//declares an int called sum for this functions scope only. local scope

      sum = first_number + second_number;// same as above code

      return sum;
}

int main()// starting point for all programs
{
      int sum;// global scope. usable in entire program

      sum = add(1, 1);// this function can be used anywhere in the program again.

      cout << "Answer is: " << sum << endl;//prints answer to console.
      char waits_for_user_input_before_exiting_program;//really long variable name :o)
      //^^ type 1 character and press enter
      cin << waits_for_user_input_before_exiting_program;

      return 0;
}

Each function uses variables that can only be used in that function. A function is like a tiny little machine that crunches data and spits out a solution of some kind. You decide what that solution will be. Although sum is used in both the add() function and main() function, they are two different variables. When you type sum = add(1, 1) the program uses the function add(), does the data crunching, and then spits out the answer. Then the function and all it's variables no longer exist until the function is used again. At that point the program uses a different instance of the same function add(). Kind of like a program within a program.
Last edited on
Topic archived. No new replies allowed.