Programming Language. Functions. Eng. Anis Nazer First Semester

Similar documents
C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Chapter 6 - Notes User-Defined Functions I

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Unit 7. 'while' Loops

C++ Programming: From Problem Analysis to Program Design, Third Edition

Why Is Repetition Needed?

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Introduction to Programming

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

(created by professor Marina Tanasyuk) FUNCTIONS

Functions and Recursion

Chapter 3 - Functions

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

CSCE 110 PROGRAMMING FUNDAMENTALS

WARM UP LESSONS BARE BASICS

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

A SHORT COURSE ON C++

Chapter 7 - Notes User-Defined Functions II

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

Lab Instructor : Jean Lai

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Using Free Functions

CMSC 202 Midterm Exam 1 Fall 2015

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Summary of basic C++-commands

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

C++ basics Getting started with, and Data Types.

Ch 6. Functions. Example: function calls function

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

BITG 1233: Introduction to C++

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

C++ Programming: From Problem Analysis to Program Design, Third Edition

Objectives. In this chapter, you will:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Understanding main() function Input/Output Streams

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Computer Programming : C++

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Increment and the While. Class 15

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Introduction to C++ Systems Programming

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

In this chapter you will learn:

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

Introduction to Scientific Programming with C++

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Chapter 3 - Functions

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

4. C++ functions. 1. Library Function 2. User-defined Function

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

CS2141 Software Development using C/C++ C++ Basics

CS242 COMPUTER PROGRAMMING

Introduction to Programming I COS1511 School of Computing Revision Notes

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

CSCE 121 ENGR 112 List of Topics for Exam 1

CSCE 110 PROGRAMMING FUNDAMENTALS

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Local and Global Variables

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Functions. Lecture 6 COP 3014 Spring February 11, 2018

CSCE 206: Structured Programming in C++

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

Chapter 4 Computer Science with C++ Name: Review Worksheet A Mr. Ferwerda

Transcription:

Programming Language Functions Eng. Anis Nazer First Semester 2016-2017

Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities statements in a function belong to that function Up until now, all our commands are written inside "main()" function In C++, programs are made up of a collection of functions... like building blocks (... LEGO?)

Definitions main( )............ // function call............ function call return func ( )............... return

Definitions A function can be executed through a "function call" When a function call occurs, execution jumps to the "called function" execution then continues in the "called function" After the statements in the "called function" are finished, execution returns back to the "calling function"

Definitions What is the definition of the terms? function call? called function? calling function? return?

functions Predefined functions : functions are already written and ready for you to use thanks to the people who did that... :) ex. math functions: sqrt(), pow(), sin(), cos()... etc ex. rand()... remember the guessing game? User defined functions : you write your own function how fun is that... :)

Math functions There are millions of predefined functions available... a sea of functions Some are published and are available for you for free :) Predefined math functions: sqrt(), sin(), cos(), floor(), ceil(), pow(), log(),... To use the predefined math functions, you have to include the library <cmath> the file contains the definitions of the functions

Math functions Example: What is the output? #include <iostream> #include <cmath> using namespace std; int main() for ( int c=0 ; c <= 10 ; c++ ) cout << c << "\t" << pow(c, 2) << endl; return 0;

User defined functions To to write your function, you need to specify: function name number of parameters that the function receives and their data types (function input ) data type of the returned value (function output ) statements that are executed when the function is called

User defined functions Syntax: writing a user defined function return type function name ( formal ) parameter list statements......

User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; else max = b; return max ;

User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; function name if ( a > b ) max = a; else max = b; return max ;

User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; else max = b; return max ; formal parameter list

User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; formal parameters else max = b; return max ;

User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; type of the if ( a > b ) returned value max = a; else max = b; return max ;

User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; else max = b; return max ; function heading

User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; else max = b; return max ; function body

User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; local variable else max = b; return max ;

User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; else max = b; return value return max ;

User defined functions function heading : the line that specifies: the return data type the function name the formal parameter list function body : the statements that are executed when the function is called local variable : the variable "belongs to the function, visible only to the function formal parameter list : the list of the parameters that the function expects formal parameters : variables declared in the function heading

User defined functions formal parameter list syntax: datatype identifier, datatype identifier,... Notes: list can have any number of parameters list can be empty, i.e. no parameters parentheses are part of the syntax, even if list is empty each parameter must have a data type it is a variable declaration

Example Write a program that reads two numbers and displays the larger number, use the previous function // function definition here //... int main () double x, y, m; cout << "Enter two numbers: \n"; cin >> x >> y; m = larger( x, y ) ; cout << "larger number is " << m; return 0; function call

Example larger( x, y ) function call What happens when a function is called? values of the actual parameters are copied into the formal parameters value of x is copied into variable a value of y is copied into variable b function is executed when finished, execution returns back to the calling function value of max is returned back to main() and replaces the function call

User defined functions Function call syntax: function name ( actual parameter list ) Syntax for actual parameter list value variable expression value, variable,... expression Note: number must match the number in the formal parameter list

functions - Types Value returning function: when the function ends, it returns a value to the "calling function" void functions: when the function ends, it returns to the "calling function" without returning a value

User defined function return statement ends the function and execution goes back to the calling function Syntax: Value returning function: return value/variable/expression ; void function: return ;

Example write a function that takes the length and width of a rectangle and returns the area Question: Input? two numbers: length, width Output? area types? double? how? area = length * width

Example double area ( double length, double width) double a; area = length * width; return area;

Another example Write a function that takes three numbers and returns the largest of the three double larger3 ( double n1, double n2, double n3) double m; m = larger ( n1, n2 ); m = larger ( m, n3 ); return m;

Another example Write a function that takes three numbers and returns the largest of the three or, you can reduce the code: double larger3 ( double n1, double n2, double n3) return larger ( larger ( n1, n2 ), n3 );

Example Write a program that reads 10 numbers and displays the maximum Use the already available function larger() Variables: number, max functions: larger how: read number max = larger of number and max repeat 10 times

Example read number Start max = number c=1 c <= 9 display max End read number increment c max = larger( max, number)

Example int main() double number, max; int c; cout << "Enter 10 numbers:\n"; cin >> number; for ( max = number, c=1; c <= 9 ; c++ ) cin >> number; max = larger( max, number ); cout << "The largest number is: "; cout << max; return 0;

More examples Write a function that takes a grade and returns true if the grade is between 0 and 100, and returns false otherwise Questions: Input? grade Output? true or false types? integer and boolean how? if/else

Example bool is_valid ( int grade ) if ( grade > 100 grade < 0 ) return false; return true;

Example Write a program that reads a grade and displays whether the grade is valid or not int main() int grade; cout << "Enter grade: "; cin >> grade; if ( is_valid ( grade ) ) cout << grade << " is valid"; else cout << grade << " is not valid"; return 0;

function prototype you can define the function after the function call, but you need to write a function prototype syntax: same as the function heading but with a semicolon at the end You can remove the variable name in the function prototype. In fact, the header files that are included (iostream, cmath,...) contain function prototypes

function prototype So, our example can be written as: // function prototype bool is_valid ( int grade ); int main() int grade; cout << "Enter grade: "; cin >> grade; if ( is_valid ( grade ) ) cout << grade << " is valid"; else cout << grade << " is not valid"; return 0; // function definition

Void functions void is a data type that refers to nothing Syntax: void function name ( formal ) parameter list statements......

Void functions void function returns to the calling function without returning a value the return statement in a void function has no value if the programmer does not write a return, the computer returns automatically, after executing the last statement in the function

Example write a function that takes an integer N and displays N stars on the screen write a function that takes an integer N and displays N spaces on the screen Write a program that displays a pyramid of stars, of height 10 Modify program to make the user enter the height

Stars void stars(int N) for ( int c=1; c <= N ; c++) cout << "*"; return ; void spaces(int N) for ( int c=1; c <= N ; c++) cout << " "; return ; int main() int sp, st, lines; cout << "Enter the number of lines: "; cin >> lines; for (int c=1, st=1, sp=lines; c <= lines ; c++) spaces(sp); stars(st); cout << endl; sp = sp 1; st = st + 2; return 0;