Thread: How do u call a function?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    How do u call a function?

    I dont know how to call a function! The books just explains but it doesnt show an example. Can someone show me a example or examples?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by jturner38 View Post
    I dont know how to call a function! The books just explains but it doesnt show an example. Can someone show me a example or examples?
    printf("Hello, this is a call to function printf");
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    How would that call a function? I just told u what im tryin to learn to do. How do u call a function? Like say u have a problem. U do a program. After u have return 0; then u have another function after it. Then u want those results in your main function. Well u have to call it in your main function. How do u do that?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jturner38
    How would that call a function?
    That is an example of a call to the printf function.

    Quote Originally Posted by jturner38
    Like say u have a problem. U do a program. After u have return 0; then u have another function after it. Then u want those results in your main function. Well u have to call it in your main function. How do u do that?
    If your function is named f and takes no arguments, then you would write:
    Code:
    f();
    If it returns some value that you wish to assign to a variable x, then you would write:
    Code:
    x = f();
    Also, I am assuming that you mean to say that you defined a function after your main function. If so, you should declare a prototype for your function before the point where you define your main function. If you mean to say that you want to call a function after returning 0 from the main function, then that would be wrong, since your function call would be unreachable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by jturner38 View Post
    I dont know how to call a function! The books just explains but it doesnt show an example. Can someone show me a example or examples?
    Example:

    Code:
    #include <stdio.h> /* Standard I/O header */
    
    
    int AddNums( int , int  ); 
    /* Function Declaration, AddNums is called in main, which is before the AddNums function so function is declared here */
    
    
    int main(void)
    {
             int answer1; /* Variable to hold an answer */
             int answer2; /* Same as above */
    
             answer1 = AddNums( 123, 321 ); /* Call to AddNums, will add 123 and 321 (just random numbers I thought of) and put result in answer1*/
    
             answer2 = AddNums( 789, 987 ); /* Another call to AddNums, will add 789 and 987 */
    
             printf("Answer1 = %d\nAnswer2 = %d", answer1, answer2); /* Will print values of answer1 and answer 2 */
    
             return 0;
    }
    
    
    /* AddNums Function, takes in two numbers, adds them together and returns result*/
    int AddNums( int a, int b )
    {
            return a + b; /* Return the sum of a and b */
    }

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    "How do you add two numbers together?"

    Asking a series of infinite questions within the same class as that, is no way to learn C. Does your book not explain these things?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM