passing functions with variable

This is a discussion on passing functions with variable within the C++ Programming forums, part of the General Programming Boards category; Please take a look at the included code and help me out with a couple of things. when executed as ...

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    passing functions with variable

    Please take a look at the included code and help me out with a couple of things.
    when executed as it stands i must enter a value for "int c" twice to get through the third function and four times to get through the fourth function.
    what would the method be to enter the "int c" value once and use it in the following functions.
    i suspect it may be in the way i call the functions in the iostream???




    //---------------------------------------------------------------------------

    #include <vcl.h>
    #include <iostream.h>
    #pragma hdrstop

    //---------------------------------------------------------------------------

    #pragma argsused

    int main(int argc, char* argv[])
    {
    int first(int);
    int second(int b, int a);
    int third(int b, int a);
    int fourth(int a,int b);
    int a;
    int b;

    //send this input to other functions

    cout<< "Enter a number: ";
    cin >>a;
    cout<< "a = " <<a<<"\n";
    cout << "Enter another number: ";
    cin >> b;
    cout<< "b = " <<b<<"\n";

    //get this back from other functions

    cout<< "\nFirst returns a*c= "<< first(a)<<"\n";

    cout<< "\nSecond returns a*b= "<< second(a,b)<<"\n";

    cout<< "\nThird returns "<< second(a,b)<<"*"<< first(a)<<"= "<<third(b,a)<<"\n";

    cout<< "\nFourth returns "<< first(a)<<"*"<< second(a,b)<<"*"<< third(b,a)<<"= "<< fourth (a,b);
    getchar();
    return 0;
    }
    //Functions

    //simple a times c
    int first (int a)
    {
    int c;
    cout<< "\ninput value for C: ";
    cin >>c;
    cout<< "In first C = " <<c;
    return a*c;
    }
    //simple a times b
    int second (int a, int b)
    {
    return a*b;
    }
    //first times second
    //requires value for "int c"
    //to be entered twice???
    int third(int b,int a)
    {
    return (first(a) * second(a,b));
    }
    //first times second times third
    //requires value for "int c" to be entered
    //four times???
    int fourth(int a, int b)
    {
    return ((first(a)* second(a,b))*third(b,a));
    }
    //---------------------------------------------------------------------------


    Thanks
    itld

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    You can enter c in main then make it available to all the functions exept second and that will take care of it. see below:
    Code:
    //--------------------------------------------------------------------------- 
    
    //#include <vcl.h> 
    #include <iostream.h> 
    #pragma hdrstop 
    
    //--------------------------------------------------------------------------- 
    
    //#pragma argsused 
    
    int first( int a, int c ); 
    int second( int b, int a ); 
    int third( int b, int a, int c ); 
    int fourth( int a, int b, int c ); 
    
    int main(int argc, char* argv[]) 
    { 
    	int a; 
    	int b; 
    	int c;
    	//send this input to other functions 
    
    	cout<< "Enter a number (a): "; 
    	cin >>a; 
    
    	cout<< "a = " << a << endl; 
    
    	cout << "Enter another number (b): "; 
    	cin >> b; 
    	cout<< "b = " << b << endl; 
    
    	cout << "Enter another number (b): "; 
    	cin >> c; 
    	cout<< "c = " << c << endl; 
    
    	//get this back from other functions 
    
    	cout<< "\nFirst returns a*c= "<< first( a, c ) << endl; 
    
    	cout<< "\nSecond returns a*b= "<< second( a, b ) << endl; 
    
    	cout<< "\nThird returns " << second( a, b ) << "*" << 
    		first( a, c ) <<"= " << third( b, a, c ) << endl; 
    
    	cout<< "\nFourth returns " << first( a, c ) << "*" << second( a, b ) 
    		<< "*" << third( b, a, c ) << "= " << fourth ( a, b, c ) << endl; 
    	
    	
    	return 0; 
    } 
    //Functions 
    
    //simple a times c 
    int first (int a, int c) 
    {  
    	
    	return a*c; 
    } 
    
    //simple a times b 
    int second (int a, int b) 
    { 
    	return a*b; 
    } 
    
    //first times second 
    //requires value for "int c" 
    //to be entered twice??? 
    int third(int b,int a, int c ) 
    { 
    	return ( first( a, c ) * second( a, b ) ); 
    } 
    
    //first times second times third 
    //requires value for "int c" to be entered 
    //four times??? 
    int fourth(int a, int b, int c ) 
    { 
    	return ( ( first( a, c ) * second( a, b ) ) * third( b, a, c) ); 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Passing Through A Variable Argument List
    By SMurf in forum C Programming
    Replies: 6
    Last Post: 04-14-2007, 11:12 AM
  3. Replies: 1
    Last Post: 01-20-2002, 10:50 AM
  4. Passing a function name as a variable to execute it?
    By Zuul in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 11:42 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21