Presentation is loading. Please wait.

Presentation is loading. Please wait.

National University of Computer & Emerging Sciences

Similar presentations


Presentation on theme: "National University of Computer & Emerging Sciences"— Presentation transcript:

1 National University of Computer & Emerging Sciences
FUNCTIONS National University of Computer & Emerging Sciences CS 101 Introduction to Computing Get rid of bad bugs!! Mehreen Saeed October 2014

2 FUNCTIONS HELP US DO MODULAR PROGRAMMING
"Modular Programming" tends to encourage splitting of functionality into two types: "Manager" functions: they control main program flow and primarily contain calls to "Worker" functions "Worker" functions: they handle low-level details

3 FUNCTIONS SYNTAX type functionName (parameter_list) THINGS TO NOTE
This is a function prototype THINGS TO NOTE If the function does not return anything type is void The functionName is just like a variable name. You decide what the name should be Parameter list is simply the list of parameters and their types. (also called formal parameters) Actual parameter is the value parameter is assigned to EXAMPLES void PrintTheOutput(int output) int SumNumbers(int Number1, int Number2, int Number3) void SwapNumbers(int &Number1, int &Number2)

4 PARAMETER TYPE Pass by value Pass by reference
When only input is required. A copy of variables is made into the parameter variable Pass by reference When multiple outputs are required. The parameter variables refer to the parameters of the caller

5 Changing Number1 and Number2 does not change x and y
Pass by value: A separate copy of the variable is made inside the function. Here x is copied to Number1 and y is copied to Number2 void main() { int x=10,y=20; PrintOutput(x,y); cout << x << y; } void PrintOutput(int Number1,int Number2) { cout << Number1 << Number2; Number1 = Number1+1; Number2 = Number2+3; PrintOutput Number Number2 10 20 11 23 calls main x y 10 20 Changing Number1 and Number2 does not change x and y

6 Pass by reference: the same variable is being referred to
void main() { int x=10,y=20; swap(x,y); cout << x << y; } void Swap(int &Number1,int &Number2) { int temp; temp = Number1; Number1 = Number2; Number2 = temp; Here Number1 refers to x and Number2 referes to y Swap Number Number2 calls main x y 10 20 20 10 Changing Number1 and Number2 changes x and y

7 int SqCube(int x,int &cube)
cube = x*x*x; s = x*x; return s; } void SqCube1(int x,int &square,int &cube) { square = x*x; cube = x*x*x; } NOTE: the two different ways of computing and returning two values to the caller

8 int SquareBAAD() { int x; cin >> x; int sq = x*x; return sq; } int SquareGOOD(int x) { int sq; sq= x*x; return sq; } void SquareALSOGOOD(int x,int &sq) { sq = x*x; return ; } void SquareALSOBAAD(int x) { sq = x*x; cout << sq; } WHY ARE SOME GOOD AND SOME BAAD????? HINT: Write a program to print the square of numbers from 1 to 100 by using the above

9 int Area1(int &height,int &width)
{ int a = height*width; height = height/10; width = width/2; return a; } int Area(int height,int width) { height = 2+width; width = 5+height; int a = height*width; return a; }

10 int function1(int x,int &y)
{ x = x*100; y = x*10; return x*y; } int function2(int &x,int &y,int &z) { x = y*z; y = x*y; z = z*z; return x+y+z; }

11 IDENTIFY THE FUNCTIONS HERE…also identify the parameters to be used
Dr. Weirdo is trying to come up with a formula that will confuse everyone in the world. He will take three numbers and arrange them in ascending order. Next, he will take the sum and product of all three numbers. If the sum is <50, then he will arrange the numbers in descending order and print them out. If the sum is >=50 then he will check their product. If the product is < 100 then he will swap the first two numbers and print them. Dr. Weirdo thinks that nobody can reproduce the above method….He says even if somebody can do the above they cannot do it in the modular style…BUT CAN YOU???

12 IDENTIFY THE FUNCTIONS AND LIST OF PARAMETERS
A student enrollment system has the following description. A new student gets admission in FAST and is assigned a roll number. The system records his name, address, date of birth and domicile city. The system has the facility of being able to modify a student record any time, in case the address of the student changes or some details were entered incorrectly. The system is able to output all details of the student when given the roll number. If the student gets admission somewhere else then the system has the facility to delete the record from its database.

13 IDENTIFY FUNCTIONS AND LIST OF PARAMETERS
Design a game called ‘Speedy the Robot’. The Robot is to be controlled by a user via the keyboard’s up, down, left and right arrow keys. Speedy moves around a maze represented by dots and ‘x’ as shown below. If Speedy bumps into an ‘x’ the user looses five points. If the Speedy moves on a dot the user gains one point and the dot changes to a dash. If Speedy moves on a dash the user loses one point. Speedy’s initial position is the bottom right corner. The aim of the game is to collect a maximum number of points. To end the game the user presses ‘x’ and the computer shows the score to the user. If Speedy has gained 80% or more points the system assigns the user 3 stars. If Speedy gains 50% and less than 80% points then user is assigned two stars. No stars are gained if user scores below 10%, otherwise a one star.

14 Writing prototypes…NO cin and cout
Write the complete function prototypes for the following scenarios: A function that takes Celsius as input temperature and outputs the corresponding temperature in Fahrenheit A function that computes the location and velocity when given the coordinates of an object at two different times. A function that takes as input mass and speed of light and outputs the corresponding amount of energy associated with the mass. A function that computes the slope and intercept of a line when given two points on the line A function that decides whether a circle passes through the x-axis or not when given its center and radius. A function that places a cursor at any point on the screen A function to draw a circle when given the center and radius of the circle A function that computes the grade and percentage marks of a student when given his/her midterm and final result


Download ppt "National University of Computer & Emerging Sciences"

Similar presentations


Ads by Google