Thread: functions

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    functions

    I am having difficulty understanding functions and how to write a program using one. Foe example, I need to write a program that takes a number from the user and prints a diamond.
    I need to use functions to tell the program hoe many spaces to print and how many * to print. Can any one hellp me please?
    Thanks

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    for functions read some tuts. It will click soon. They are not hard to understand especially when you see them working. As for the other its been done about a zillion times now in all sorts of fancy ways ( well by prelude anyway ). Im certain a board search will be all you need to do your homework.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    Here is an example program which calls a function:
    Code:
    #include <iostream>
    using namespace std;
    
    void DoubleThenPrint(int a)
    {
    	cout<<2 * a<<endl;
    }
    
    int main()
    {
    	int a = 4;
    	DoubleThenPrint(a);
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2
    how do I call the function to print both spaces and stars using the exact same function?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    A function is just like a miniature program--you're not limited to one line of code. You can make the function do anything you want. It can be 10,000 lines long, and print every character possible in a representation of the Mona Lisa. If you want the function to print a space, then do this:

    cout<<" ";

    If you want your function to print an asterisk next to the space, then do this:

    cout<<"*";

    Like magic, your function will have printed a space and an asterisk
    Last edited by 7stud; 04-02-2003 at 10:51 PM.

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