Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Functions with c

  • 24-10-2016 10:07pm
    #1
    Registered Users, Registered Users 2 Posts: 35


    Hi,

    Wondering if anyone could explain this code for me, i have a test tomorrow and im looking at a fail right now, not for the lack of effort, i mean its open book i just really cant understand this i just got to grips with for loops and the test is on functions -_-

    Question 2
    1. What is the value of the variable c in the main function after the following code is executed?
    /* function prototypes
    (prototypes let the compiler know what functions your program is going to use)
    */
    int myfunction(int var1, int var2);
    void my_other_function(int var1);
    int main()
    {
    int c, b;
    b = 7;
    c = myfunction(4, b);
    my_other_function(2);
    return 0;
    }
    int myfunction(int var1, int var2)
    {
    int a;
    a = var1 + var2;
    return(a);
    }
    void my_other_function(int var2)
    {
    int c;
    c = var2;
    }




    Thanks,
    Mark:confused:


Comments

  • Registered Users, Registered Users 2 Posts: 6,285 ✭✭✭bonzodog2


    I'm not going through it line by line, but c in main will have the value 11 after the 1st function is called; the second function has no effect as it only changes its own local variable c, which is then discarded.


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭PrzemoF


    /* function prototypes
       (prototypes let the compiler know what functions your program is going to use)
     */
    
    int myfunction(int var1, int var2);
    void my_other_function(int var1);
    
    int main()
    {
    	int c, b;
    	b = 7;
    	c = myfunction(4, b);
    	my_other_function(2);
    	return 0;
    }
    
    int myfunction(int var1, int var2)
    {
    	int a;
    	a = var1 + var2;
    	return(a);
    }
    
    void my_other_function(int var2)
    {
    	int c;
    	c = var2;
    }
    
    Add
    printf("%i", c);
    
    before return (0);
    Go to http://www.learn-c.org/ and test the code at the bottom of the page.
    Search for c for beginners and read anything about c - that will explain you what's happening here (main, void, int, return is all you need to understand)
    11


  • Registered Users, Registered Users 2 Posts: 20,195 ✭✭✭✭jimgoose


    Have a read of this:

    https://en.wikipedia.org/wiki/Local_variable

    The key to the thing is these lines:
    c = myfunction(4, b); 
    ..
    ..
    return(a);
    

    And tell the lecturer that wrote that that (s)he's looking for a kick up the hole with that layout and indentation. :D


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭PrzemoF


    Naming convention is below any standard as well :D


Advertisement