Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Programming Week # 7 & 8 Functions Lecture # 11& 12

Similar presentations


Presentation on theme: "Intro to Programming Week # 7 & 8 Functions Lecture # 11& 12"— Presentation transcript:

1 Intro to Programming Week # 7 & 8 Functions Lecture # 11& 12
By: Saqib Rasheed

2 What we will study today …
What are functions? How are they defined ? How are they declared ? What values are passed to functions ? What values do functions return ? Saqib Rasheed

3 What is C++ function   A large C++ program is divided into basic building blocks called C++ function. C++ function contains set of instructions enclosed by “{  }” which performs specific operation in a C++ program. Actually, Collection of these functions creates a C++ program. Saqib Rasheed

4 Uses of C++ functions: C++ functions are used to avoid rewriting same logic/code again and again in a program. There is no limit in calling C++ functions to make use of same functionality wherever required. We can call functions any number of times in a program and from any place in a program. A large C++ program can easily be tracked when it is divided into functions. The core concept of C++ functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C++ programs Saqib Rasheed

5 Function Two types of functions: Functions that return a value
Functions that do not return a value Saqib Rasheed

6 Function You've already encountered a few functions: main, cout and cin. The main function is special, in the way that it's called automatically when the program starts. In C++, and other programming languages, you can create your own functions. Saqib Rasheed

7 A Typical Function Functions have 5 main features:
1.  The DATA TYPE is the data type of the RETURN VALUE of the function. 2. The NAME is required as an identifier to the function, so that the computer knows which function is called. Naming of functions follows the same set of rules as the naming of variables. 3. Functions can take ARGUMENTS - a function might need extra information for it to work. Arguments are optional. 4. The function BODY is surrounded by curly brackets and contains the statements of the function. 5. The RETURN VALUE is the value that is passed back to the main program. Functions exit whenever you return a value. Saqib Rasheed

8 Function Syntax Function has three parts
Function Declaration / Prototype Function Call Function Body Saqib Rasheed

9 Function Prototype The function prototype is a single line “summary” of the function Tells the number of inputs and output Tells the type of inputs and output (int, float, double, char). It is normally located at the top of your program listing Usually above the main function and below the #include files definition A function prototype has the form output Parameter functionName(input parameter, input parameter, …); 11/12/2018

10 Function Return Type A function can only return either a single variable OR nothing at all Multiple values are return using pointers and that can be specify in the input parameters. The return type indicates the type of variable (if any) that the function outputs (returns) Return type can be of any type (e.g. int, float, char) OR void (indicating the function does not return anything) 11/12/2018

11 Function Name A function name can be anything but it is a good idea that the name used tells you what the function does For example if the function used for deleting any student data (use name deleteStudentData) We can declare more than one functions with the same name But there input parameters should be different. For example void deleteStudentData (void); // delete all the students data void deleteStudentData (int studentID); // delete the student data with specific ID. 11/12/2018

12 Function Inputs Most functions will use input values
It is possible to write a function that has no inputs (e.g. rand function) Example void deleteStudentData (void); Input values must be separated by commas if there are more than one input value within the brackets () after the function name Example void deleteStudentData (int studentID,float CGPA) Each entry in this list of arguments must identify the input variable type and define a name by which that input can be referred to In the case where there are no inputs to a function the keyword void appears instead of the list of arguments 11/12/2018

13 Example Consider our function to calculate the factorial of an integer
Inputs: A single integer (that we will referred by its name) Function Name: factorial Return value: integer Thus the prototype of this function is: int factorial(int value); 11/12/2018

14 Function Implementation
ALL functions are typically located either before the main function OR after the main function A function implementation is very like the main function itself It starts with the function description (effectively the same as the prototype) Without the ; It is bounded by a pair of chain brackets {} (Local) variables can be defined within (at the start of) the function If the function has an output, return command must be present in the function 11/12/2018

15 Function Implementation
You cannot refer to a variable that is defined in any other function directly why? The return command is used as follows: return (variableName); This return command can : Appear only in a function if it is returning output value in its prototype Causes the function to immediately end and output the value in the single variable being “returned” You can only return a single variable using the return statement 11/12/2018

16 Declaration of Function
//Prototype / Declaration return-value-type function-name( argument--type-list) ; void main ( ) { Statement1; function-name( argument) ; //Call Of Function } Saqib Rasheed

17 Function Body Syntax //Function Body
return-value-type function-name( argument-list ) { statements; } Saqib Rasheed

18 Simple Example 1 #include<iostream.h>
void print(void); Function Declaration int main() { cout<<"\nDisplaying Statement 1"; print(); Function Call cout<<"\nDisplaying Statement 2 "; return 0; } void print(void) Function Body cout<<"\n\n*****Displaying Statement 3 ****** \n"; Saqib Rasheed

19 Simple Example 2 #include < iostream.h >
int i ;  Global Variable void myfun ( void ) ; Function Declaration main ( ) { i = 10 ; cout<< "\n within main i = " << i ; myfun ( ) ; Function Call cout<<endl; } Saqib Rasheed

20 Simple Example 2 void myfun ( void ) -----------Function Body {
cout<< "\n Inside function f , i = " << i ; } Saqib Rasheed

21 Calling the Function If the function returns a value, you may store the result in a suitable variable type using the assignment (=) operator Examples of function calls would be: variableName=functionOne(); variableName=functionTwo(variableA,variableB); functionThree(variableA,variableB); A function can be called from the main function or from any other function When a function is completed the program returns to the next line after where the function was called from 11/12/2018

22 Example Get three integers numbers from user Pass them to function
Add them and find the average in function body Display the Average in function body Saqib Rasheed

23 void printAverage(int x, int y, int z);//Function Prototype
void main() { int a, b, c; cout<<"Enter 3 integers = "; cin>>a>>b>>c; printAverage(a, b, c); //Function Call cout<<“Ending Program”<<endl”; } void printAverage(int x, int y, int z) //Function Body { int res = (x + y + z) / 3; cout<<“Average = ”<<res<<endl;

24 Function Returning value
Saqib Rasheed

25 int printAverage(int , int , int ); //Function Prototype void main() {
int a, b, c; cout<<"Enter 3 integers = "; cin>>a>>b>>c; int res= printAverage(a, b, c); //Function Call cout<<“Average = <<res<<endl”; } int printAverage(int x, int y, int z) //Function Body { return (x + y + z) / 3; Saqib Rasheed

26 int printAverage(int , int , int );//Function Prototype void main() {
int a, b, c; cout<<"Enter 3 integers = "; cin>>a>>b>>c; cout<<“Average = ”<<printAverage(a, b, c); } int printAverage(int x, int y, int z) //Function Body { return (x + y + z) / 3; Saqib Rasheed

27 #include <iostream> using namespace std; int addition(int a, int b); int main () { int z; z = addition (5,3); cout << "The result is " << z; } int addition (int a, int b) int r; r=a+b; return r; Saqib Rasheed

28 #include <iostream.h>
int factorial (int value); // function prototype void main (void) { int scanValue = 0; cout<<“\n Enter the input value:”; cin>>scanValue; int res; res= factorial (scanValue); // function calling cout<<“\nThe Result is = ”<< res; res = factorial (10); // function calling cout<<“\nThe Result is = ”<< res ; } int factorial (int value) // function implementation int counter=0; int result=1; for (counter=1; counter<=value; counter++) result= result * counter; return (result); 11/12/2018

29 Example Consider our example: int factorial (int value) {
void main (void) { int result = factorial (4); cout<<“The result is”<<result; } Example Consider our example: int factorial (int value) { int counter=0; int result=1; for (counter=1; counter<=value; counter++) result= result * counter; } return(result); 11/12/2018

30 Example Consider our example: int factorial (int value) {
counter = 0 Example Consider our example: int factorial (int value) { int counter=0; int result=1; for (counter=1; counter<=value; counter++) result= result * counter; } return(result); 11/12/2018

31 Example Consider our example: int factorial (int value) {
counter = 0 result = 1 Example Consider our example: int factorial (int value) { int counter=0; int result=1; for (counter=1; counter<=value; counter++) result= result * counter; } return(result); 11/12/2018

32 Example Consider our example: int factorial (int value) {
counter = 1 result = 1 Example Consider our example: int factorial (int value) { int counter=0; int result=1; for (counter=1; counter <= value; counter++) result= result * counter; } return(result); 11/12/2018

33 Example Consider our example: int factorial (int value) {
counter = 1 result = 1 * 1 = 1 Example Consider our example: int factorial (int value) { int counter=0; int result=1.0; for (counter=1; counter <= value; counter++) result= result * counter; } return(result); 11/12/2018

34 Example Consider our example: int factorial (int value) {
counter = 2 result = 1 Example Consider our example: int factorial (int value) { int counter=0; int result=1; for (counter=1; counter <= value; counter++) result= result * counter; } return(result); 11/12/2018

35 Example Consider our example: int factorial (int value) {
counter = 2 result = 1* 2 = 2 Example Consider our example: int factorial (int value) { int counter=0; int result=1.0; for (counter=1; counter <= value; counter++) result= result * counter; } return (result); 11/12/2018

36 Example Consider our example: int factorial (int value) {
counter = 3 result = 3 * 2 = 6 Example Consider our example: int factorial (int value) { int counter=0; int result=1; for (counter=1; counter <= value; counter++) result= result * counter; } return (result); 11/12/2018

37 Example Consider our example: int factorial (int value) {
counter = 4 result = 6 * 4 = 24 Example Consider our example: int factorial (int value) { int counter=0; int result=1; for (counter=1; counter <= value; counter++) result= result * counter; } return (result); 11/12/2018

38 Example Consider our example: int factorial (int value) {
counter = 5 result = 24 Example Consider our example: int factorial (int value) { int counter=0; int result=1.0; for (counter=1; counter <= value; counter++) result= result * counter; } return (result); 11/12/2018

39 Calling the Function void main (void) { int result = factorial (4); cout<<“The result is ”<<result; } If the function returns a value, you may store the result in a suitable variable type using the assignment (=) operator Examples of function calls would be: variableName = functionOne(); variableName = functionTwo(variableA, variableB); functionThree(variableA,variableB); A function can be called from the main function or from any other function When a function has completed its execution then the program returns to the next line after where the function was called from. All the local variables of the function are destroyed after function execution. 11/12/2018

40 Advantages of using functions
Code becomes more readable easy to modifiable by second person. Code becomes more compact and small. If code is small then the exe file will be also small, which will enable your executable file to fit in a very small RAM. 11/12/2018

41 What will be the output? void duplicate (int a, int b) { a*=2; b*=2; }
int main () int x=1, y=3; duplicate (x, y); cout << "x=" << x << ", y=" << y; return 0; 11/12/2018

42 Example 2 #include <iostream.h> int Square( int ); // function prototype void main() { for ( int counter = 1; counter <= 10; counter++ ) { cout<<“The square of “<< counter<< “ is”; cout<<Square( counter )”<<endl;// function call } } // end _tmain // function definition int Square( int y ) return y * y; // return square of y } // end function Square 11/12/2018

43 Sharing Data Among User-Defined Functions
There are two ways to share data among different functions Using global variables (very bad practice!) Passing data through function parameters Value parameters Reference parameters Constant reference parameters

44 C++ Variables A variable is a place in memory that has
A name or identifier (e.g. income, taxes, etc.) A data type (e.g. int, double, char, etc.) A size (number of bytes) A scope (the part of the program code that can use it) Global variables – all functions can see it and using it Local variables – only the function that declare local variables see and use these variables A life time (the duration of its existence) Global variables can live as long as the program is executed Local variables are lived only when the functions that define these variables are executed

45 I. Using Global Variables
#include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; }

46 I. Using Global Variables
#include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x

47 I. Using Global Variables
#include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x void main() { f2(); cout << x << endl ; } 1

48 I. Using Global Variables
#include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 4 void f2() { x += 4; f1(); } 2 void main() { f2(); cout << x << endl ; } 1

49 I. Using Global Variables
#include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 5 4 void f1() { x++; } 4 void f2() { x += 4; f1(); } 3 void main() { f2(); cout << x << endl ; } 1

50 I. Using Global Variables
#include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 5 4 void f1() { x++; } 5 void f2() { x += 4; f1(); } 3 void main() { f2(); cout << x << endl; } 1

51 I. Using Global Variables
#include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 5 4 void f2() { x += 4; f1(); } 6 void main() { f2(); cout << x << endl; } 1

52 I. Using Global Variables
#include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 4 5 void main() { f2(); cout << x << endl; } 7

53 I. Using Global Variables
#include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; } x 4 5 void main() { f2(); cout << x << endl; } 8

54 I. Using Global Variables
#include <iostream.h> int x = 0; void f1() { x++; } void f2() { x+=4; f1(); } void main() { f2(); cout << x << endl; }

55 What is Bad About Using Global Vairables?
Not safe! If two or more programmers are working together in a program, one of them may change the value stored in the global variable without telling the others who may depend in their calculation on the old stored value! Against The Principle of Information Hiding! Exposing the global variables to all functions is against the principle of information hiding since this gives all functions the freedom to change the values stored in the global variables at any time (unsafe!)

56 Local Variables Local variables are declared inside the function body and exist as long as the function is running and destroyed when the function exit You have to initialize the local variable before using it If a function defines a local variable and there was a global variable with the same name, the function uses its local variable instead of using the global variable

57 Example of Defining and Using Global and Local Variables
#include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() int x = 10; // Local variable

58 Example of Defining and Using Global and Local Variables
#include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() int x = 10; // Local variable x

59 Example of Defining and Using Global and Local Variables
#include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() int x = 10; // Local variable x void main() { x = 4; fun(); cout << x << endl; } 1

60 Example of Defining and Using Global and Local Variables
#include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() int x = 10; // Local variable x 4 void fun() { int x = 10; cout << x << endl; } x ???? 3 void main() { x = 4; fun(); cout << x << endl; } 2

61 Example of Defining and Using Global and Local Variables
#include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() int x = 10; // Local variable x 4 void fun() { int x = 10; cout << x << endl; } x 10 3 void main() { x = 4; fun(); cout << x << endl; } 2

62 Example of Defining and Using Global and Local Variables
#include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() int x = 10; // Local variable x 4 void fun() { int x = 10; cout << x << endl; } x 10 4 void main() { x = 4; fun(); cout << x << endl; } 2

63 Example of Defining and Using Global and Local Variables
#include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() int x = 10; // Local variable x 4 void fun() { int x = 10; cout << x << endl; } x 10 5 void main() { x = 4; fun(); cout << x << endl; } 2

64 Example of Defining and Using Global and Local Variables
#include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() int x = 10; // Local variable x 4 void main() { x = 4; fun(); cout << x << endl; } 6

65 Example of Defining and Using Global and Local Variables
#include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() int x = 10; // Local variable x 4 void main() { x = 4; fun(); cout << x << endl; } 7

66 II. Using Parameters Function Parameters come in three flavors:
Value parameters – which copy the values of the function arguments Reference parameters – which refer to the function arguments by other local names and have the ability to change the values of the referenced arguments Constant reference parameters – similar to the reference parameters but cannot change the values of the referenced arguments

67 Value Parameters This is what we use to declare in the function signature or function header, e.g. int max (int x, int y); Here, parameters x and y are value parameters When you call the max function as max(4, 7), the values 4 and 7 are copied to x and y respectively When you call the max function as max (a, b), where a=40 and b=10, the values 40 and 10 are copied to x and y respectively When you call the max function as max( a+b, b/2), the values 50 and 5 are copies to x and y respectively Once the value parameters accepted copies of the corresponding arguments data, they act as local variables!

68 Example of Using Value Parameters and Global Variables
#include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() x = 4; fun(x/2+1); x void main() { x = 4; fun(x/2+1); cout << x << endl; } 1

69 Example of Using Value Parameters and Global Variables
#include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() x = 4; fun(x/2+1); x 4 void fun(int x ) { cout << x << endl; x=x+5; } 3 void main() { x = 4; fun(x/2+1); cout << x << endl; } 3 2

70 Example of Using Value Parameters and Global Variables
#include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() x = 4; fun(x/2+1); x 4 void fun(int x ) { cout << x << endl; x=x+5; } 3 8 4 void main() { x = 4; fun(x/2+1); cout << x << endl; } 2

71 Example of Using Value Parameters and Global Variables
#include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() x = 4; fun(x/2+1); x 4 void fun(int x ) { cout << x << endl; x=x+5; } 8 3 5 void main() { x = 4; fun(x/2+1); cout << x << endl; } 2

72 Example of Using Value Parameters and Global Variables
#include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() x = 4; fun(x/2+1); x 4 void main() { x = 4; fun(x/2+1); cout << x << endl; } 6

73 Example of Using Value Parameters and Global Variables
#include <iostream.h> int x; // Global variable void fun(int x) { cout << x << endl; x=x+5; } void main() x = 4; fun(x/2+1); x 4 void main() { x = 4; fun(x/2+1); cout << x << endl; } 7

74 Practice Questions

75 Question 1 Write a program in C++ that have a function of
square(). The user should enter the number and the code should display the square of the given number. Saqib Rasheed

76 Question 1(code) void square(double); Function Prototype void main() { double x; cout <<"\n main(), before calling "; cout<<"\nEnter the number to find square="; cin>>x; square(x); Function Call } void square(double x) Function Body x=x*x; cout <<"\n After calling in square(), x = " << x<<endl; Saqib Rasheed

77 Question 3 Write a code that take numbers from user and
displays its cube. The Code should reads integers and prints their cubes until the user inputs the sentinel value 0. Each integer read should be passed to the cube() function by the call cube(n). The value returned by the function should replaces the expression cube(n) and then should be passed to the output object cout. Saqib Rasheed

78 cout<<"\nEnter the Number to Find the Cube = ";
int cube(int x); Function Prototype void main() { int n=1; cout<<"\nFor Termination Enter Zero" <<"\nEnter the Number to Find the Cube = "; while (cin>>n && n != 0) //cin>> n; It can be written in side also. cout<<"\tcube(" << n << ") = " << cube(n) << endl; Function Call cout<<"\nEnter the Number to Find the Cube = "; } int cube(int x) Function Body return x*x*x; Saqib Rasheed

79 Question 4 Write a function that finds the area of the
rectangle on providing length and width. Get Length & width from user in main() Call the function area() Calculate the length and return the area Display the result in main() Saqib Rasheed

80 int area(int,int); ----------Function Prototype int main() {
int length, width; cout << "Enter the length: "; cin >> length; cout << "Enter the width: "; cin >> width; cout << "The area of a " << length << "x" << width; cout << " rectangle is " << area(length, width)<<endl; return 0; } int area(int l, int w) int number; number = l * w; return number; Saqib Rasheed

81 Question 5 Get month ,day & year from user in main()
Call the function printDate(int, int, int) Put a check in printDate() using if statement (m >= 1 || m <= 12 && d >= 1 || d <= 31 && y > 0) & if it vilotates the rule display “Must Enter a Valid Date” Using switch Statement get the month Day and year are displayed normally Termination should be on entering 0 in months Saqib Rasheed

82 Out Put Enter Month = 12 Enter Day = 27 Enter Year = 1986
December 27,1986 Enter Month = 25 Enter Day = 0 Enter Year = 1999 Must Enter a Valid Date. Enter Month = Saqib Rasheed

83 Question 5(code) void printDate(int,int,int); int main() {
int month,day,year ; do cout<<"\nEnter Month = "; cin >>month; cout<<"\nEnter Day = "; cin>> day; cout<<"\nEnter Year = "; cin>> year; printDate(month,day,year); } while (month != 0 ); return 0; Saqib Rasheed

84 void printDate(int m,int d, int y) //Funcation Body {
if (m < 1 || m > 12 || d < 1 || d > 31 || y < 0) cout<< "\nMust Enter a Valid Date.\n"; return; } switch (m) case 1: cout << "January "; break; case 2: cout << "February "; break; case 3: cout << "March "; break; case 4: cout << "April "; break; case 5: cout << "May "; break; case 6: cout << "June "; break; case 7: cout << "July "; break; case 8: cout << "August "; break; case 9: cout << "September "; break; case 10: cout << "October "; break; case 11: cout << "November "; break; case 12: cout << "December "; break; cout << d << "," << y << endl; Saqib Rasheed

85 Question 6 A leap year is a year in which one extra day (February 29) is added to the regular calendar. Most of us know that the leap years are the years that are divisible by 4. For example, 1992 and 1996 are leap years. Most people, however, do not know that there is an exception to this rule: centennial years are not leap years. For example, 1800 and 1900 are not leap years. Furthermore, there is an exception to the exception: centennial years which are divisible by 400 are leap years. Thus, the year 2000 is a leap year. Make a program that full fills the above criteria using functions The program should terminate on entering 0 The return type should be Boolean i.e bool isLeapYear(int); Saqib Rasheed

86 Out Put Enter in the Format (2020)===2010 2010 is not a leap year.
2000 is a leap year. Enter in the Format (2020)===2012 2012 is a leap year. Enter in the Format (2020)===0 0 is a leap year. Press any key to continue Saqib Rasheed

87 bool isLeapYear(int); void main() { int n; do
{ cout<<"\n\nEnter in the Format (2020)==="; cin >> n; if (isLeapYear(n)) cout << n << " is a leap year.\n"; else cout << n << " is not a leap year.\n"; } while (n > 1); bool isLeapYear(int y) return y % 4 == 0 && y % 100 != 0 || y % 400 == 0; Saqib Rasheed

88 Question 7 Write a program in C++ that take two numbers from
user and find the largest among two using function. The Conditions are if (a==b) "A, B are the same“ a = b, (values) else if (a < b) "A & B are not same “ a != b (values) " A is less than B “ a < b (values) else "A is Greater than B “ a > b (values) Saqib Rasheed

89 Question 7 (code) #include<iostream.h>
void maximum (int, int); //Funcation Defination int main () { int a,b; cout<<"Enter Two Numbers "<<endl; cin>>a>>b; maximum(a,b); // Call Of Funcation cout<<endl; return 0; } Saqib Rasheed

90 Question 7 (code) void maximum (int a,int b) //Funcation Defination {
if (a==b) cout<<"\n\n A, B are the same\t "<<a<<"="<<b; else if (a < b) cout<<"\n\n A & B are not same \t"<<a<<" != "<<b; cout<<"\n\n A is less than B \t"<<a<<" < "<<b; } else cout<<"\n\n A is Greater than B\t"<<a <<" > "<<b; cout<<endl; Saqib Rasheed

91 Question 8 Develop a program in C++ that has function
printTempOpinion() which prints "Cold" on if the temperature is below 10, "OK" if the temperature is in the range 20 -> 30, "Hot" if the temperature is above 30. Saqib Rasheed

92 Question 8 (code) void printTempOpinion (int t); void main () {
cout<<"Enter the Temperature="; cin>>temp; printTempOpinion(temp); } void printTempOpinion (int t) if (t < 10) cout<<"\n\t\tIts COLD\n"; else if (t>= 20 && t <= 30) cout<<"\n\t\tThe Weather is Ok\n"; else cout<<"\n\t\tThe Weather is Hot\n"; Saqib Rasheed


Download ppt "Intro to Programming Week # 7 & 8 Functions Lecture # 11& 12"

Similar presentations


Ads by Google