+ 1

Function parameter

if I use some name for a parameter of a function and use same name for same data type for main function will it affect my function for example I used parameter name as x which is int type and use x in main function of type int and call the function and use this x as parameter value of my function then what will happen

9th May 2017, 6:01 PM
Manan Jethwani
Manan Jethwani - avatar
1 ответ
+ 12
No problem, you can use same name for different variables which are in different functions. One won't affect the another. int main(){ int x = 10; // body of main() // This is the scope of first x } int sum(int x, int y){ // body of function sum() // This is the scope of second x } The above mentioned variables are called local variable for each function. BUT if you use any global variable (outside all functions), it'll be accessible from all functions and whatever you'll do with it inside any function, it'll affect the global value. #include <iostream> using namespace std; int p = 20; // Global variable p void increment(){ p++; } void main(){ cout<<p<<endl; // 20 increment(); cout<<p<<endl; // 21 // because global p has been changed in another function }
9th May 2017, 6:47 PM
Shamima Yasmin
Shamima Yasmin - avatar