Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.

Slides:



Advertisements
Similar presentations
Functions Prototypes, parameter passing, return values, activation frams.
Advertisements

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
 Review structures  Program to demonstrate a structure containing a pointer.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
Instructor - C. BoyleFall Semester
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
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 Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
Introduction to Programming
Popping Items Off a Stack Using a Function Lesson xx
Two-Dimensional Arrays Lesson xx
CO1401 Programming Design and Implementation
FUNCTIONS IN C++.
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
Global & Local Identifiers
void Pointers Lesson xx
توابع در C++ قسمت اول اصول كامپيوتر 1.
Returning Structures Lesson xx
CSC1201: Programming Language 2
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
One-Dimensional Array Introduction Lesson xx
Passing Structures Lesson xx
Chapter 5 Function Basics
Popping Items Off a Stack Lesson xx
Value returning Functions
Creation and Use of Namespaces
Lec 14 Oct 23, 02.
Functions Pass By Value Pass by Reference
Dynamic Memory A whole heap of fun….
5.1 Introduction Pointers Powerful, but difficult to master
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Fundamental Programming
CSC1201: Programming Language 2
The Stack.
CPS125.
Presentation transcript:

Lesson xx

Why use functions Program that needs a function Function header Function body Program rewritten using a function

1. Functions allow you to repeat a body of code without having to physically rewrite it many times. 2. Functions allow you to modularize code. This means that you can break down your problems into small blocks and each block can be put into a function. This makes a program easier to work with and debug. 3. Functions allow many people to work on one program at the same time. Each person writes a different function.

Write a program that: 1. Prints a row of 45 ‘*’s 2. Prints the contents of the variable a 3. Prints a row of 45 ‘*’s 4. Prints the contents of the variable b 5. Prints a row of 45 ‘*’s 6. Prints the contents of the variable c 7. Prints 2 rows of 45 ‘*’s

#include "stdafx.h" #include using std::cout; using std::endl; int main() { int a = 5, i; float b = ; char c = 'q'; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " a = " << a; /************************************************/ cout << endl ; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " b = " << b; /************************************************/ cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " c = " << c; /************************************************/ cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; system ("pause"); return 0; }

cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " a = " << a; (1) (2) (3) (4)

cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " b = " << b;

cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl;

void funName ( ) ///function header { Body of code }

void funName ( ) Return type Function name Argument list

#include "stdafx.h" #include using std::cout; using std::endl; void prtStar ( ); ///function prototype int main() { int a = 5,; float b = ; char c = 'q'; prtStar ( ); cout << " a = " << a; /************************************************/ prtStar (); cout << " b = " << b; /************************************************/ prtStar (); cout << " c = " << c; /************************************************/ prtStar (); return 0; } void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }

void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }

void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }

void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }

#include "stdafx.h" #include using std::cout; using std::endl; void prtStar ( ); ///function prototype int main() { int a = 5,; float b = ; char c = 'q'; prtStar ( ); cout << " a = " << a; /************************************************/ prtStar (); cout << " b = " << b; /************************************************/ prtStar (); cout << " c = " << c; /************************************************/ prtStar (); return 0; }

#include "stdafx.h" #include using std::cout; using std::endl; void prtStar ( ); ///function prototype int main() { int a = 5,; float b = ; char c = 'q'; prtStar ( ); cout << " a = " << a; /************************************************/ prtStar (); cout << " b = " << b; /************************************************/ prtStar (); cout << " c = " << c; /************************************************/ prtStar (); return 0; }

How to write a function function header { // body } void fun ( ) { //code goes here } How to call a function 1. function prototype / declaration 2. function call / invocation void fun ( ) ; ///function declaration void main ( ) {... fun ( ) ; ///function call... return 0; }

Why use functions Program that needs a function Function header Function body Program rewritten using a function