Thread: functions

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    functions

    i am having trouble with the divide function in the main. any help would be great.


    Code:
    #include <iostream>
    #include "fun.h"
    using namespace std;
    double divide (double num1, double num2);
    int main()
    
    {
    	double a, b, c;									//declares variables
    	cout<<"Enter two numbers:";							//displays output	
    	cin>>a>>b;									//gets input
    	cout<<endl;									//ends line
    
    	/**************************************
    	*  Adds the value of a and b          *
    	*  @param a  a double                 *
    	*  @param b  a double 	              *
    	*  @return   the sum of the two inputs*
    	**************************************/
    	c=add(a,b);
    	cout<<"The sum of the two numbers is: " <<c;
    	cout<<endl;
    
    	
    	/***************************************
    	*  Subtracts the value of a and b      *
    	*  @param a  a double                  *
    	*  @param b  a double 	               *
    	*  @return   the diff of the two inputs*
    	***************************************/
    	c=subtract(a,b);
    	cout<<"The difference of the two numbers is: " <<c;
    	cout<<endl;
    	
    	
    	/******************************************
    	*  Mutiplies the value of a and b         *
    	*  @param a  a double                     *
    	*  @param b  a double 	                  *
    	*  @return   the product of the two inputs*
    	******************************************/
    	c=multiply(a,b);
    	cout<<"The product of the two numbers is: " <<c;
    	cout<<endl;
    
    	
    
    	/***************************************
    	*  Cubes the value of a and b          *
    	*  @param a  a double                  *
    	*  @param b  a double 	               *
    	*  @return   the cube of one input     *
    	***************************************/
    	c=cube(a);
    	cout<<"The cube of the first number is: " <<c;
    	cout<<endl;
    
    	
    	/***************************************
    	*  Cubes the value of a and b          *
    	*  @param a  a double                  *
    	*  @param b  a double 	               *
    	*  @return   the cube of one  input    *
    	***************************************/
    	c=cube(b);
    	cout<<"The cube of the second number is: " <<c;
    	cout<<endl;
    
    
    	
    
    
    	double divide (double num1, double num2)
    	{
    		double total;  
    		total = num1 / num2;
    		return total;
    	}
    		
    
    
    
    return 0;
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Define that function outside of your main() function.
    Ex:
    Code:
    #include <iostream>
    using namespace std;
    
    void foo();
    
    int main()
    {
      //
      //
      return 0;
    }
    
    void foo()
    {
      cout << "foo" << endl;
    }
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    is there any way to get this to work inside the main.

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    That would be non-function version
    Code:
    int main() 
    {
       // the rest of the code
    
       double total;  
       total = num1 / num2;
    		
       return 0;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    is it possible to do this as a function in the main?

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by bj31t
    is it possible to do this as a function in the main?
    Nop, can't declare a function inside a function, including main
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    thanks for the help

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You can write the function outside of main and call it from inside main like you called multiply, cube, etc. That is how it is normally done.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Thumbs up Right!

    ...That is how it is normally done.
    Right! In fact, with most large programs, main() won't do anything except call other functions. You might have some "branching" logic (like if-statements and switch-statements), but all of your I/O, calculations, data manipulation, etc., would be done in functions.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Keyword: large programs Just to clarify.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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