Thread: working with functions

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    working with functions

    Hello everyone,

    I am working with functions now and tryin to understand them.

    I have been practicing different ways for using them.

    such as below:
    Code:
    int greetings();                       // this is a prototype 
     
    int age();
    
    int main()
    {
    
    	int greetings();                   // this is a function header
    	{                                  // start of function body
    
            char name[20];
    		cout << "Please enter your name?  ";
    		cin >> name;
    		cout << "Hello "<< name <<"\n";
    
    	
    	}                                  //end of function body
    
    
     cout <<"Enter your age? ";
    
    
    	    int age();            
    		
    		{                    
        	int age;
    
    	    cin >> age;
    		cout << "You are  " << age << " years old\n\n";
    
    		} 
    /* Next I would like to use my greetins function again in        
        my program: */
    
      int greetings();    // :here                    
    
    
    	return 0;
    }

    What I would like to learn next is how to use the same functions over in the program.

    I would normally use a loop to repeat this code but I want to know how to reuse the functions in the program.
    Last edited by robasc; 12-26-2005 at 10:53 PM.

  2. #2
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    I've done some rearranging you seem to be used to pascal or something...

    Code:
    int greetings();                       // this is a prototype 
    void age();								//another
    
    int main()
    {
    	cout <<"Enter your age? ";
    	age(); //function call to age (age found outside of main ;) )
    	
    	/* Next I would like to use my greetins function again in        
    		my program: */
    
    	greetings();    // :here                    
    
    	return 0;
    }
    
    void age();            
    {                    
    	int age;
    
    	cin >> age;
    	cout << "You are  " << age << " years old\n\n";
    
    } 
    
    int greetings();                   // this is a function header
    {                                  // start of function body
    
        char name[20];
    	cout << "Please enter your name?  ";
    	cin >> name;
    	cout << "Hello "<< name <<"\n";
    
    
    }         //end of function body
    hope that helps

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    well not pascal but visual c++ 6.0 it came with my book.

    I noticed you used void, I forgot about that since age() does not return anything you use void.

    I also noticed you added the statement block to the functions after you actually used the

    functions. Is that correct?

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    You define functions in their own block, so you define them outside of the main() function like Boomba did. You can define the functions before or after you used them,
    just so long as you have the prototypes before you use them its fine either way.
    The cost of software maintenance increases with the square of the programmer's creativity.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    Code:
    void  greetings();                       // change return value to void
    void age();
    
    int main()
    {
                    ...
    	return 0;
    }
    
    void age();                        // remove the semicolon      
    {                    
    	...
    } 
    
    void greetings();               // remove the semicolon and change return type to void
    {                                 
                    ...
    }
    That should do the trick

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    ok, I believe this is better, not perfect but better. I still need to understand the return and void statement better.

    Code:
    #include <iostream.h>
    
    int age();
    char greetings();
    
    int main()
    {
    	{
    	 greetings();
    	}
    
    
    	{
    		age();
    	}
    
    
       return 0;
    }
    
    int age()
    {
    	int age;
    	 cout << "enter your age ";
         cin >> age;
    	 cout <<"you are " << age << " years old...\n\n\n";
    	return 0;                   
    }
    
    char greetings()
    {
    	char name[20];
         cout << "enter name ";
    	 cin >> name;
    	 cout << "\nHello " << name << "\n\n";
    	return 0;
    }
    Last edited by robasc; 12-27-2005 at 03:07 AM.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    yeah you setup prototypes before and outside of the main function. You can define them after, or you can define them in the prototype like this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void age()
    {
       ....whatever age does...
    };          <---Just remember that ; in the prototype!
    
    int main()
    {
        age();
        return 0;
    }
    or you could do it like this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void age();
    
    int main()
    {
        age();
        return 0;
    }
    
    void age()
    {
       ...whatever age does...
    }                 <------No semicolon when you define a function after but always put one in the prototype
    Good luck!

    EDIT: You dont need the
    Code:
    ...
    {
             age();
    }
    ...
    stuff. Just:

    Code:
    ...
    age();
    ...
    will do. And always remember to return a value for main.

    Code:
    int main()
    {
        age();
        greetings();
        return 0;
    }
    --or you can do--
    Code:
    int main()
    {
         age();
        greetings();
        cin.get()
    }
    Last edited by Junior89; 12-27-2005 at 03:03 AM.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    How's this?
    Code:
    #include <iostream>
    
    using namespace std;
    
    void age();
    void greetings();
    
    int main()
    {
    	
       greetings();
       age();
    return 0;
    }
    
    void age()              
    {
    	int age;
    	 cout << "enter your age ";
         cin >> age;
    	 cout <<"you are " << age << " years old...\n\n\n";
    	//return 0;                   
    }
    
    void greetings()
    {
    	char name[20];
         cout << "enter name ";
    	 cin >> name;
    	 cout << "\nHello " << name << "\n\n";
    	                     
    }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you set up your code text editor to only use spaces (not TABS) for indenting code, then everyone else will see the same as you see in your editor, and the same as you see on the forum.
    TABS and HTML on forums do not mix.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    13
    Quote Originally Posted by robasc
    well not pascal but visual c++ 6.0 it came with my book.

    I noticed you used void, I forgot about that since age() does not return anything you use void.

    I also noticed you added the statement block to the functions after you actually used the

    functions. Is that correct?
    I had also a visual C++ 6.0 book, it's really anoying what the differents are right now! I had this problem and off course one with the header files and one with the statements with if, else is else... ect!

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here is the format you should use when first learning functions:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void greeting(string str)
    {
        cout<<str<<endl;
    }
    
    int main()
    {
        greeting("hello");
        greeting("goodbye");
        
        system("PAUSE");
        return 0;
    }

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    I see you used your function header and your prototype together?

    could you break your code down and explain what (#include <string>) does?

    I haven't seen this one before

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I see you used your function header and your prototype together?
    You don't need a function prototype if the function is declared before its first use. I like to put them in anyway, though.

    The <string> header file was included to use the string class, which is used for the argument of greeting(). A string is sort of like an array of chars, but it resizes itself as required. You don't have buffer overruns with strings.
    Code:
    char s1[] = "hello ";
    strcat(s1, "world!");  // buffer overrun
    
    string s2 = "hello ";
    s2 += "world!";  // fine
    strings are good for other things too. I'm sure the tutorials have something about them.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's the tutorial for strings: http://www.cprogramming.com/tutorial/string.html
    And here's the c-string one: http://www.cprogramming.com/tutorial/lesson9.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  3. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  4. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  5. Functions are not working
    By founder247184 in forum C++ Programming
    Replies: 0
    Last Post: 11-29-2002, 04:00 PM