Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Functions with c

  • 24-10-2016 11: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,335 ✭✭✭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,929 ✭✭✭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,208 ✭✭✭✭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,929 ✭✭✭PrzemoF


    Naming convention is below any standard as well :D


Advertisement