National University of Computer & Emerging Sciences

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

Functions Prototypes, parameter passing, return values, activation frams.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Chapter 7 Functions.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
CPS120: Introduction to Computer Science Functions.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Arrays Chapter 7.
Chapter 8 Functions.
User-Written Functions
Lecture 3: Method Parameters
Scratch for Interactivity
Introduction to Programming
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Dr. Shady Yehia Elmashad
Python Programming Module 3 Functions Python Programming, 2/e.
Intermediate Programming using C++ Functions part 2
CS1010 Programming Methodology
COMP 170 – Introduction to Object Oriented Programming
CO1401 Programming Design and Implementation
CS1010 Programming Methodology
Dr. Shady Yehia Elmashad
Chapter 4: Subprograms Functions for Problem Solving
Dr. Shady Yehia Elmashad
Reference parameters (6.2.3)
Returning Structures Lesson xx
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pass by Reference.
FUNCTION CSC128.
Topics discussed in this section:
Computing Fundamentals
Lecture 3: Method Parameters
Simulating Reference Parameters in C
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Introduction to Problem Solving and Programming
CS150 Introduction to Computer Science 1
Chapter 8 Functions.
CS 1111 Introduction to Programming Spring 2019
ICS103: Programming in C 6: Pointers and Modular Programming
Presentation transcript:

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

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

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)

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

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 Number1 Number2 10 20 11 23 calls main x y 10 20 Changing Number1 and Number2 does not change x and y

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 Number1 Number2 calls main x y 10 20 20 10 Changing Number1 and Number2 changes x and y

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

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

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; }

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; }

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???

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.

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.

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