From the course: C Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Retaining values in a function

Retaining values in a function - C Tutorial

From the course: C Essential Training

Start my 1-month free trial

Retaining values in a function

- [Instructor] Variables are private to their functions. In this code, both the main and funct functions use integer variable a, the variable is assigned a value in each function, and this value is independent despite the variable's name and despite it being used in both functions. Build and run, and you can see that the value is independent of what goes on in the other functions. A variable name is just a name. In this source code file, however, I desire to have the same value used in both functions. In this code, however, I desire to have the value of variable a in function funct be returned, not only that, to have its value retained between function calls. So at line seven, the variable's value is increased by 16. This variable is output, its value is output here, and then it's returned to the main function. Now in the main function, this value is saved in its variable a and output but then the function calls again, and…

Contents