(1 Define a function A function is a group of statements...

This preview shows page 1 - 3 out of 5 pages.

(1) Define a function? A function is a group of statements that perform a task. In C++ every program has at least one function which is main and all the most trivial programs 0can define additional functions. In C++, functions exists basically in two types which are :those that do not return a value and those that return a value .The main function in C++ programs is typically non value returning functions (Void function ) of which this one dos not return a value to its caller. (2)Write a programme to generate a factorial from a number using the recursion. 1. #include<iostream> 2. using namespace std; 3. int main() 4. { 5. int factorial( int ); 6. int fact,value; 7. cout<< "Enter any number: " ; 8. cin>>value; 9. fact=factorial(value); 10. cout<< "Factorial of a number is: " <<fact<<endl; 11. return 0; 12. } 13. int factorial( int n) 14. { 15. if (n<0) 16. return (-1); /*Wrong value*/ 17. if (n==0) 18. return (1); /*Terminating condition*/ 19. else 20. { 21. return (n*factorial(n-1)); 22. } 23. }
Image of page 1

Subscribe to view the full document.

Output of the program.
Image of page 2
Image of page 3
  • Summer '16
  • professor Anyago
  • Prime number, Void type, Laurence Fishburne

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern