Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.

Slides:



Advertisements
Similar presentations
Chapter Five Functions
Advertisements

Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Programming Functions: Passing Parameters by Reference.
If Statements & Relational Operators Programming.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Chapter 5 Functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Problem Solving and Program Design Programming. COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Writing and Testing Programs Drivers and Stubs Supplement to text.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
Functions:Passing Parameters by Value Programming.
Structured Programming Programming. COMP102 Prog Fundamentals I: Structured Programming /Slide 2 Structured Programming l Structured programing is the.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
Function Part II: Some ‘advanced’ concepts on functions.
Programming Functions: Passing Parameters by Reference.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Functions—Part I. Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
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.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
Dr. Shady Yehia Elmashad
Function Topic 4.
Fundamentals of structural programming
Dr. Shady Yehia Elmashad
Programming fundamentals 2 Chapter 2:Function
Dr. Shady Yehia Elmashad
CS1201: Programming Language 2
Functions.
Functions.
CS150 Introduction to Computer Science 1
Starting Out with C++: From Control Structures through Objects
More ‘concepts’ on Function
Summary Two basic concepts: variables and assignments Basic types:
Let’s all Repeat Together
Chapter 6: User-Defined Functions I
Functions Divide and Conquer
CS1201: Programming Language 2
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Functions
Functions Divide and Conquer
More ‘concepts’ on Function
Presentation transcript:

Introduction to Functions Programming

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself. l This is called structured programming. l These parts are sometimes made into functions in C++. main() then uses these functions to solve the original problem.

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 3 Advantages of Functions l Functions separate the concept (what is done) from the implementation (how it is done). l Functions make programs easier to understand. l Functions can be called several times in the same program, allowing the code to be reused.

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 4 C++ Functions l C++ allows the use of both internal (user- defined) and external functions. External functions (e.g., abs, ceil, rand, sqrt, etc.) are usually grouped into specialized libraries (e.g., iostream, stdlib, math, etc.)

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 5 User-Defined Functions l C++ programs usually have the following form: // include statements // function prototypes // main() function // function definitions

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 6 Function Input and Output

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 7 Function Definition A function definition has the following syntax: ( ){ } For example: Definition of a function that computes the absolute value of an integer: int absolute(int x){ if (x >= 0)return x; else return -x; }

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 8 Function Call l A function call has the following syntax: ( ) Example: int distance = absolute(-5); n The result of a function call is a value of type

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 9 Arguments/Parameters l one-to-one correspondence between the arguments in a function call and the parameters in the function definition. int argument1; double argument2; // function call (in another function, such as main) result = thefunctionname(argument1, argument2); // function definition int thefunctionname(int parameter1, double parameter2){ // Now the function can use the two parameters // parameter1 = argument 1, parameter2 = argument2

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 10 Absolute Value #include using namespace std; int absolute (int);// function prototype for absolute() int main(){ int num, answer; cout << "Enter an integer (0 to stop): "; cin >> num; while (num!=0){ answer = absolute(num); cout << "The absolute value of " << num << " is: " << answer << endl; cin >> num; } return 0; } // Define a function to take absolute value of an integer int absolute(int x){ if (x >= 0)return x; else return -x; }

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 11 Function Prototype l The function prototype declares the input and output parameters of the function. l The function prototype has the following syntax: ( ); Example: A function that returns the absolute value of an integer is: int absolute(int);

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 12 Function Definition l The function definition can be placed anywhere in the program after the function prototypes. If a function definition is placed in front of main(), there is no need to include its function prototype.

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 13 Absolute Value (alternative) Note that it is possible to omit the function prototype if the function is placed before it is called. #include using namespace std; int absolute(int x){ if (x >= 0)return x; else return -x; } int main(){ int num, answer; cout << "Enter an integer (0 to stop): "; cin >> num; while (num!=0){ answer = absolute(num); cout << "The absolute value of " << num << " is: " << answer << endl; cin >> num; } return 0; }

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 14 Function of three parameters #include using namespace std; double total_second(int, double,double ); int main(){ cout << total_second(1,1.5, 2) << endl; return 0; } double total_second( int hour, double minutes, double second) { return hour* minutes * 60 + second; }

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 15 Printing the Diamond Pattern as a Function void diamond(int size) { int row, space, star; for(row=1; row<=size; row++){ //top half for(space=1; space<=size-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; } for(row=size -1; row>=1; row--){ //bottom half for(space=1; space<=size-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; }

COMP102 Prog Fundamentals I: Introduction to Functions /Slide 16 Calculating the Area of a Circle with a Function