Chapter3.pptx - PROBLEM SOLVING PROGRAMMING II CHAPTER 3 FUNCTION BASICS PREDEFINED FUNCTIONS THAT RETURN VALUE \u2022 Arguments value that function starts

# Chapter3.pptx - PROBLEM SOLVING PROGRAMMING II CHAPTER 3...

This preview shows page 1 - 9 out of 19 pages.

PROBLEM SOLVING & PROGRAMMING II CHAPTER 3 FUNCTION BASICS PREDEFINED FUNCTIONS THAT RETURN VALUE Arguments: value that function starts with. It can be constant, or variable or even an expression. Function can have more than one argument Value returned: value computed by the function Function call ( function invocation): consists of function name followed by arguments. Function_Name (Argument_1, Argument_2) root= sqrt(4.0); root = (sqrt(num)/10); Library : Each predefined function has a library that should be included. #include<cmath> Example //This program is used to show how to use predefined functions. we will use pow(x,y) #include<iostream> #include<cmath> using namespace std; void main() { cout<<"This is the power function\n"; double result, x , y; cout<<"Please enter the base\n"; cin>>x; cout<<"Please enter the exponent: \n"; cin>>y; //compute the power using pow function result= pow(x,y); cout<<"The result for "<<x<<" Power "<< y <<" is "<< result<<endl; } Output: This is the power function Please enter the base 2 Please enter the exponent: 4 The result for 2 Power 4 is 16 -------------------------------- PREDEFINED VOID FUNCTIONS #include<iostream> #include<cstdlib> using namespace std; void main() { cout<< "This program is used to show how exit(1) is used\n"; exit(1); cout<<"Nothing will be executed after using exit(1)\n"; } Output: This program is used to show how exit(1) is used -------------------------------- PREDEFINED FUNCTIONS abs for integer : abs(-7) library cstdlib. fabs for double : fabs(-7.5) library cmath. rand() for random integer number, its library is cstdlib. srand(seed) void function, its library is cstdlib RANDOM NUMBER GENERATOR rand() returns randomly chosen numbers from 0 to RAND_MAX, where RAND_MAX is the maximum two byte positive integer( at least 32767) for(i =0; i<10; i++) { cout<<rand() <<endl; } To scale to 10: for(i =0; i<10; i++) { // rand()%10 is used for scaling cout<<rand()%10<<endl; } RANDOM NUMBER GENERATOR srand(seed number), seed number is integer number // generate 10 random numbers for(i =0; i<10; i++) { // rand()%10 is used for scaling cout<<rand()%10<<endl; } 9 1 9 7 2 6 1 8 4 5 After running the program for the second time: 9 1 9 7 2 6 1 8 4 5 RANDOM NUMBER GENERATOR  #### You've reached the end of your free preview.

Want to read all 19 pages?

• Fall '18
• Eric Swartz
• • •  