Thread: Novice needs help with Functions

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    Novice needs help with Functions

    Hi guys,

    First of all, I'm newto this board, so nice to meet you!

    Well, now that introductions are over:

    I'm a super fresh newbie to C++, and I'm having a hell of a time with fucntions. Basically, I'm trying to write an elementary program that passes variables into a function--the function then adds the variable and sends the results back into the prgram. Here is the code:

    Code:
    #include <iostream>
    
    int myFunc(int x, int y);
    
    int main()
    {
    	int a;
    	int b;
    	int c;
    
    	std :: cout << "Enter a number \n";
    	std :: cin >> a;
    	std :: cout << "Enter another number \n";
    	std :: cin >> b;
    	myFunc();
    	std:: cout << "myFunc is: " << c;
    	return 0;
    
    }
    	
    	int myFunc (a,b)
    	{
    		c = a + b;
    		return c;
    	}
    For some reason, I get nothing but compiler errors when I try to run it. I've declared that the function will accept integers, and have listed that it will return integer values. I have no idea what I'm doing wrong. I know you guys have better things to do--and better problems to work on--but I really need some help. Any advice, or should I just hang my head in shame?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The line

    Code:
      myFunc();
    is just throwing away the returned value, since you don't assign it to anything. You need to assign it to the variable you print:

    Code:
      c = myFunc();
    Edit: Another error is that you should replace

    Code:
      int myFunc (a,b)
    with

    Code:
      int myFunc (int a, int b)

  3. #3
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    you need to pass the variable you want the function to compute, and also the return value has to be passed as well...

    so you need to put
    Code:
     c = myFunc(a,b);
    I know nothing about C++, but that's how you do it in C

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    What he said - my line was wrong (you need the "a, b" inside the parentheses).

  5. #5
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    have you ever heard of the...
    Code:
    using namespace std;
    ...function?

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Teaching a newbie to use namespace std is a good way to avoid getting into prefixing.

    It is better to declare exactly what objects you will be using.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    Is better and looks more effiecent than just putting namespace std. But all this is down to the individual programmer I guess, it is all down to personal taste.

    Pass the variable to the function you need.

    Code:
    myFunc ( a, b );

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Thank you, guys, for the advice. I think I am making headway. I re-read my entire text chapter on functions--for some reason, I am still confused by the concept of passing variables. However, I think I am making headway. I am able to compile now without errors, but the program won't run right--" Overloaded Function Error!" or something like that happens when I try to execute the program. Here is my revised version:

    Code:
    // Listing 5.4 - demonstrates variables
    // scoped within a block
    
    #include <iostream>
    
    int myFunc(int a, int b);
    
    int main()
    {
    	int a;
    	int b;
    	int c;
    
    	std :: cout << "Enter a number \n";
    	std :: cin >> a;
    	std :: cout << "Enter another number \n";
    	std :: cin >> b;
    	c = myFunc(a, b);
    	std:: cout << "myFunc is: " << c;
    	return 0;
    
    }
    	
    	int myFunc (int a,int b)
    	{
    		int c;
    		c = myFunc(a,b);
    		return c;
    	}
    I think I'll be able to figure it out, but I need to read ahead about overloading functions. Damn it! Everything was so easy before this. Damn these functions!

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Has nothing to do with overloading - the 3rd-to-last line is supposed to be
    Code:
      c=a+b;
    not

    Code:
      c = myFunc(a,b);

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Or you could just put

    Code:
    return a + b
    They would both achieve the same result

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Success!!!

    Thank-you for the help! It was such a silly mistake. I kept getting confused the concept of a function prototype, declaring the function, defining the function and calling the function. Essectially, I kept forgetting which one required me to use the exact name of the variables that I declared in main. Nevertheless, what was nebulous is now painfully clear. Here is my correction:

    Code:
    // Listing 5.4 - demonstrates variables
    // scoped within a block
    
    #include <iostream>
    
    double myPay(double precd, double tpay);
    
    using namespace std;
    
    int main()
    {
    	//This program calculates the percentage of taxes
    	// taken from a person' paycheck
    	
    	double precd;
    	double tpay;
    	double modu;
    
    	cout << "Enter The Net amount of your check:\n";
    	cin >> precd;
    	cout << "Enter the gross amount earned on your check\n";
    	cin >> tpay;
    
    	modu= myPay(precd,tpay);
    
    	
    	cout << " You received" << modu << "% of your money!";
    
    	return 0;
    }
    
    	double myPay(double precd, double tpay)
    	{
    	
    		return precd / tpay;
    
    	}

    This is the same as the other one, with different variables and a different operator. For some reason I thought I had to declare the varibles inside the function--ignoring the obvious concept of "passing" the variables from main to the function.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I see you have taken some bad advice concerning namespaces. The way you originally used std:: was the best.

    Usually newbies are advised to use "using namespace std;" if the teacher doesn't want to go into namespaces, because otherwise they would come with questions "How come 'cout' is undeclared?" This is just a lazy "fix" to get your program compiling, and hopefully proper usage of namespaces would be taught at a later time.

    But if you already use std:: properly, starting to use "using namespace std;" is like going back to the stone age. This directive effectively says: I just need the program to compile somehow, and I don't care about the benefits of namespaces and I don't care if this language feature is even there.

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by anon
    I see you have taken some bad advice concerning namespaces. The way you originally used std:: was the best.

    Usually newbies are advised to use "using namespace std;" if the teacher doesn't want to go into namespaces, because otherwise they would come with questions "How come 'cout' is undeclared?" This is just a lazy "fix" to get your program compiling, and hopefully proper usage of namespaces would be taught at a later time.

    But if you already use std:: properly, starting to use "using namespace std;" is like going back to the stone age. This directive effectively says: I just need the program to compile somehow, and I don't care about the benefits of namespaces and I don't care if this language feature is even there.
    I agree. "using namespace std;" is something that should be avoided in any good program. I don't even like individual using statements -- I like to see each place where I use something in the std:: namespace because my programs generally have functions and classes in three, four, five namespaces. It makes it MUCH more clear to prefix everything like your original code.

    There are a few good uses of "using" (and a few times you can't accomplish something without it) but they're special/unusual cases. You shouldn't use it just to get rid of std:: in your code (which make the code more readable).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Thanks for the tip. We were told to use std::, I was just trying something new. I'm going to read a bit more about namespaces, though, just for the hell of it. For some reason we haven't touched much on it yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM