Thread: Calling init function through main in C

  1. #16
    Registered User
    Join Date
    Nov 2015
    Posts
    86

    question new

    Quote Originally Posted by mike65535 View Post
    Goodness, where did this code come from?

    You don't need to identify or call functions with "func" (as it seems perhaps you believe?)

    If you are having trouble understanding function calls, you really need to learn about it, and then try a simple example. Leave out the pointers and all that. If you can't get that to work, post it!

    Code:
    #include <iostream>
    using namespace std;
    
    
    #define maxinstant 1
    
    #define this_error_code 2
    void func1(input1)
    {
        return 1;
        
    }
    void func2(input1)
    
    
    {
        return 0;
    }
    
    
    bool_t func( Dip* req )
    {
        bool_t response = true;
     
        if ( req->inst > maxinstant )
        {
            req->errorCode = this_error_code;
        }
        else if ( req->inst == 0 )
        {
          response = func1( req );
        }
        else
        {
            Response = func2( req );
        }
     
        return ( response);
    }
    
    
     int main()
     {
      func(1);
     return 0;
     }
    this is another questions i created this as a test for myself to learn function call
    i was wondering if you can help me call function func in main?
    Last edited by amahmoo; 11-23-2015 at 11:23 AM.

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    You call a function with its name.

    If you have a function called "func" (as you do), you call it in main() by the line

    func();

    (In your case you need to pass it a parameter)

  3. #18
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    I know but i dont know how to pass variable through it

  4. #19
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    C'mon, this is the most basic stuff.

    If you want to pass "func" a parameter you put the parameter in the parens

    func(45);
    func(myvariable);
    etc.

  5. #20
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    i know but i want to set a variable for req a different one everytime i run it
    but i am getting a initaialization from incompatible pointer type

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    I know but i dont know how to pass variable through it
    I have already shown you several times in this thread. Maybe you should work with a simpler example:
    Code:
    #include <stdio.h>
    
    void foo(int n)
    {
        printf("%d\n", n);
    }
    
    int main(void)
    {
        int x = 123;
        /* insert function call here */
        return 0;
    }
    Replace the comment with a call to foo with x as the argument.
    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

  7. #22
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    i know this i just want to be able to know how pass varaible that would change value for req
    so i can run it three time with three different returns

  8. #23
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Until you can get a simple example working why muddle the learning experience with more stuff?

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    i know this i just want to be able to know how pass varaible that would change value for req
    so i can run it three time with three different returns
    Then you need to figure out what does the function do, and thereby deduce what the Di (or Dip, or whatever) object should be. I mean, you cannot just throw some arbitrary function, provide insufficient information, and then expect to be spoonfed the answer. If you are unable to understand the function, simplify.

    EDIT:
    Code:
    #include <iostream>
    using namespace std;
    This is C++. Stop. Stop getting random code snippets from the Internet and then try to understand them. You have no clue. No !@#$ing clue. Go get yourself a proper introductory book and learn C programming in a structured way. I don't care if you claim to know how to do what I challenged you to do: until I have further proof, you know nothing.
    Last edited by laserlight; 11-23-2015 at 11:33 AM.
    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

  10. #25
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Code:
    #include <stdio.h>
     
    void foo(int n)
    {
        printf("%d\n", n);
    }
     
    int main(void)
    {
        int x = 123;
        foo(x);
        // i know this
        return 0;
    }

  11. #26
    Registered User
    Join Date
    Nov 2015
    Posts
    86


    Code:
    bool_t func( Dip* req )
    Code:
    {
        bool_t response = true;
    
         if ( req->inst == 0 )
        {
          response = 1;
        }
        else
        {
            Response = 0;
        }
      return response;
    }
    

    ok i spoofed it down i want to be able to call this function in main
    and call it twice to return each value do i have to initialze struct first ?

  12. #27
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    i mean to do it in C
    thank you for ur help i just kind of desperatei need to get this done soon

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What does the function do? Give a summary and also describe line by line.
    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

  14. #29
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    function passes a pointer to a structure
    if the pointer has a value for req then it return 1 if not return 0

  15. #30
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    I jusdt dont know how to pass a value for a specifc variable in the struct through the pointer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  2. Calling this function in my main.
    By filadon in forum C Programming
    Replies: 3
    Last Post: 02-15-2011, 02:58 PM
  3. having some trouble calling a function in the main program
    By CMakesMeSad :( in forum C Programming
    Replies: 19
    Last Post: 06-26-2009, 09:33 PM
  4. calling a function inside main()
    By mero24 in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2005, 01:22 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

Tags for this Thread