Thread: New to functions

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    San Diego
    Posts
    10

    New to functions

    We just learned briefly about functions today and have a list of programs to write, but I am stuck with this particular problem:

    Write a function void myInfo( ) that prints your name and email address on two lines, both indented.

    I have been writing basic functions with int, but I have really no idea how to incorporate this into my program. The assigned chapter to read doesn't mention anything about using void anywhere so I'm not sure what to do. My teacher sucks and has never taught this before so I have been basically teaching myself the entire semester. This is what I have written, but I know it is garbage.

    Code:
    #include <stdio.h>
    
    void myInfo();
    
    main()
    {
       char string [];
    
       printf("My name and email address are as follows:\n");   
    
       printf("\t%s\n\t%s\n", myInfo(char string[]);
    }
    
    void myInfo(char string[])
    {
       char string[] = "Jason";
       
       return string;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    void simply means the function does not return a value.

    I'm thinking you'll get a lot further if you think about setting up your name and email as strings in main, then passing them to the function for printing...

    in pseudocode...
    Code:
    void printstring( pointer String)
      { printf("%string", String); }
    
    int main (void)
     { string myname[] is "freddy"
        string myemail[] is "[email protected]"
        printstring(myname)
        printstring(myemail)
        return 0;
       }
    Does that make more sense to you?

  3. #3
    Registered User
    Join Date
    Feb 2011
    Location
    San Diego
    Posts
    10
    cool, thanks again...and probably again. I'll give it a shot with the pseudocode. This class is stressing me out big time. We've finally gotten to functions, yet my teacher doesn't feel the need to teach us about strings or pointers or god knows what else.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    First thing, read about void data type.
    Scond, think logically.
    Finally, why do you need to make a function that will return string to the main and you could print there.
    Why don't you try to print in that function?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jbone0881 View Post
    cool, thanks again...and probably again. I'll give it a shot with the pseudocode. This class is stressing me out big time. We've finally gotten to functions, yet my teacher doesn't feel the need to teach us about strings or pointers or god knows what else.
    I did this in pseudocode because I want you to learn how to do it in real code... Think of it as a little nudge in the right direction...

    Back when I was in school (probably before you were born) I used to grab the text books and read ahead. Often what I got in the classroom was nothing but a good reiview... I've never been one to wait for that 2 hours a week when I can give it 2 hours a day.

    So I'd say just go for it... grab your text books and do some online searching and let your teacher try to keep up to you...

    IOW... Don't let the teacher hold you back.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. Need help with functions
    By jusfry01 in forum C Programming
    Replies: 2
    Last Post: 05-22-2010, 06:25 PM
  3. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  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. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM