OiO.lk Blog C# Why compiler complains on using a reference to a var passed as a parameter?
C#

Why compiler complains on using a reference to a var passed as a parameter?


My code:

#include <stdio.h>

void adding(int a,int b,int& sum);

int main(int argc, char const *argv[])
{
    
    int sum = 0;
    int a = 5;
    int b = 6;
    printf("Sum of %d and %d = ", a, b);
    adding(a,b,sum);
    printf("\n%d", sum);

    return 0;
}

void adding(int a, int b, int& sum) 
{
    sum = a + b; 
    printf("%d", sum);
}

I just want to see that passing reference to var(sum) as parameter to a function, where this var is going to change it’s value -> will change it value in main() as well. But i’m getting this:
error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token

However it’s working correctly in C book i’m reading.

My platform: Ubuntu 22.04
GCC version: 13.2.0
IDE: VS Code



You need to sign in to view this answers

Exit mobile version