FUNC.CPP

• 1: #include <iostream.h>


• 2: int Add (int x, int y) {4:


• 3: cout << "In Add(), received " << x << " and " << y << "\n";


• 4: return (x+y); }


• 5: int main() { cout << "I'm in main()!\n";


• 6: int a, b, c;


• 7: cout << "Enter two numbers: ";


• 8: cin >> a;


• 9: cin >> b;


• 10: cout << "\nCalling Add()\n";


• 11: c=Add(a,b);


• 12: cout << "\nBack in main().\n";


• 13: cout << "c was set to " << c;


• 14: cout << "\nExiting...\n\n";


• 15: return 0; }


Output: 


• I'm in main()!
 
• Enter two numbers: 3 5
 
• Calling Add()
 
• In Add(), received 3 and 5
 
• Back in main().
 
• c was set to 8
 
• Exiting... 

Demonstrating call to a function

1: #include <iostream.h>
 
• 2: // function Demonstration Function
 
• 3: // prints out a useful message
 
• 4: void DemonstrationFunction()
 
• 5: { cout << "In Demonstration Function\n"; }
 
• 6: // function main - prints outa message, then
 
• 7: // calls DemonstrationFunction, then prints
 
• 8: //out a second message.
 
• 9: int main() { cout << "In main\n" ;
 
• 10: DemonstrationFunction();
 
• 11: cout << "Back in main\n";
 
• 12: return 0; } 

Output for Function call : 

• In main
 

• In Demonstration Function
 

• Back in main  
free counters