The document discusses passing variables to functions in C++. It provides examples of passing integer and character variables to functions. The key points are:
- Passing variables to functions is similar to passing constants, but variables are passed instead of constants
- Variables from the main function can be passed to other functions to use their values
- Examples show passing integer and character variables to functions and the functions using the passed values
The document discusses passing variables to functions in C++. It provides examples of passing integer and character variables to functions. The key points are:
- Passing variables to functions is similar to passing constants, but variables are passed instead of constants
- Variables from the main function can be passed to other functions to use their values
- Examples show passing integer and character variables to functions and the functions using the passed values
The document discusses passing variables to functions in C++. It provides examples of passing integer and character variables to functions. The key points are:
- Passing variables to functions is similar to passing constants, but variables are passed instead of constants
- Variables from the main function can be passed to other functions to use their values
- Examples show passing integer and character variables to functions and the functions using the passed values
Passing variables to functions Passing variables to function is same as passing constants to function. Only the difference is that in passing variables to functions we will only pass Variables instead of Constants. Example void foo(int y) { cout << "y = " << y << “\n”; } int main() { int x = 6; 1 foo(x); //passing variable to a function 2 foo(x+1); //passing variable to a function return 0; } void foo(int y)// foo(datatype value) { cout << "y = " << y << “\n”; } int main() { int x ; cout<<“ enter the value of X”; cin>>x; // x is variable foo(x); // foo(value) return 0; } #include <iostream> using namespace std; void fun(int a) { cout<<"Value of A: "<<a; //value of a from main is passed from main to fun(10)} int main() { int a =10;// variable fun(a); //passing variable to function return 0; } #include <iostream> using namespace std; int cubeByValue( int n ) { int result; result= n * n * n; cout << "\nThe new value of number is " << result << endl; } int main() { int number = 5; cout << "The original value of number is " << number; cubeByValue( number ); return 0; // indicates successful termination } Passing Variables to functions #include<iostream.h> #include<conio.h> void chline(char, int); void chline(char ch, int n) { for(int a=1;a<=n;a++) cout<<ch; cout<<endl; } void main(void) { clrscr(); char ch; int n; cout<<"Enter a character "; cin>>ch; cout<<"Enter a value "; Output cin>>n; Enter a character + chline(ch, n); //Character and Integer variables passed Enter a value 10 cout<<"Hello"<<endl; ++++++++++ chline(ch, n); Hello cout<<"We are studying functions"<<endl; ++++++++++ chline(ch, n); getch(); We are studying } functions ++++++++++ Practice Practice tasks 1. Write a value returning function that receives three integers and returns the largest of the three. Assume the integers are not equal to one another. 2. Write a value returning function that receives two floating point numbers and returns true if the first formal parameter is greater than the second. 3. Write a value returning function that receives a character and returns true if the character is a vowel and false otherwise. For this example, vowels include the characters 'a', 'e', 'i', 'o', and 'u'. 1. Write a function that receives three integers and returns the largest of the three. Assume the integers are not equal to one another. 2. Write a function that receives two floating point numbers and returns true if the first formal parameter is greater than the second. 3. Write a function that receives a character and returns true if the character is a vowel and false otherwise. For this example, vowels include the characters 'a', 'e', 'i', 'o', and 'u'.