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.

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.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Chapter 5 Functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Writing and Testing Programs Drivers and Stubs Supplement to text.
1 Lab Session-IV CSIT121 Fall 2000 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercises Lab Exercise for Demo.
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 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
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.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Programming Functions: Passing Parameters by Reference.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions—Part I. Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
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.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
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
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.
CO1401 Programming Design and Implementation
Fundamentals of structural programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
Programming fundamentals 2 Chapter 2:Function
Dr. Shady Yehia Elmashad
Introduction to Functions
CS1201: Programming Language 2
Name: Rubaisha Rajpoot
Functions.
Functions.
CS150 Introduction to Computer Science 1
Counting Loops.
Starting Out with C++: From Control Structures through Objects
More ‘concepts’ on Function
CS150 Introduction to Computer Science 1
Summary Two basic concepts: variables and assignments Basic types:
Let’s all Repeat Together
Chapter 6: User-Defined Functions I
Arrays of Two-Dimensions
Functions Divide and Conquer
CS1201: Programming Language 2
CS150 Introduction to Computer Science 1
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Functions
Functions Divide and Conquer
Programming Strings.
More ‘concepts’ on Function
Presentation transcript:

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.  This is called structured programming.  These parts are sometimes made into functions in C++.  main() then uses these functions to solve the original problem.

 Functions separate the concept (what is done) from the implementation (how it is done).  Functions make programs easier to understand.  Functions can be called several times in the same program, allowing the code to be reused.

 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.)

 C++ programs usually have the following form: // include statements // function prototypes // main() function // function definitions

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

 A function call has the following syntax: ( ) Example: int distance = absolute(-5); ◦ The result of a function call is a value of type

 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

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

 The function prototype declares the input and output parameters of the function.  The function prototype has the following syntax: ( );  Example: A function that returns the absolute value of an integer is: int absolute(int);

 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.

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

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

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