Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Similar documents
Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Computer Programming: C++

Lecture 2:- Functions. Introduction

Object oriented programming C++

CS1150 Principles of Computer Science Methods

CS1150 Principles of Computer Science Methods

A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions.

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CT 229 Methods Continued

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

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

What Is a Function? Illustration of Program Flow

and function calls (including call-by-reference parameters and global variables), but no decision or repetition instructions.

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

Lecture 5: Methods CS2301

by Pearson Education, Inc. All Rights Reserved. 2

C Programming for Engineers Functions

Topic 2: C++ Programming fundamentals

UNIT III (PART-II) & UNIT IV(PART-I)

Fundamentals of Programming Session 13

Chapter 3 - Functions

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

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.

Lab Manual. ISC 115 Computing Foundations. Information Technology Solutions

CHAPTER 4 FUNCTIONS. 4.1 Introduction

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Methods. Eng. Mohammed Abdualal

Methods. CSE 114, Computer Science 1 Stony Brook University

Fundamentals of Programming & Procedural Programming

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

FORM 2 (Please put your name and form # on the scantron!!!!)

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Object Oriented Design

Unit 7. Functions. Need of User Defined Functions

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

C: How to Program. Week /Apr/16

Introduction to Programming (Java) 4/12

( &% class MyClass { }

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Python lab session 1

EECS168 Exam 3 Review

Methods CSC 121 Fall 2014 Howard Rosenthal

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Multiple-Subscripted Arrays

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Computer Programming : C++

Functions. Using Bloodshed Dev-C++ Heejin Park. Hanyang University

Pace University. Fundamental Concepts of CS121 1

UNIT 3 FUNCTIONS AND ARRAYS

Chapter-13 USER DEFINED FUNCTIONS

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to.

LAB 13: ARRAYS (ONE DIMINSION)

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

TOPICS TO COVER:-- Array declaration and use.

Introduction to Computer Science Unit 2. Notes

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

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

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Lecture 04 FUNCTIONS AND ARRAYS

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Procedures, Parameters, Values and Variables. Steven R. Bagley

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

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:

AP CS Unit 3: Control Structures Notes

Loops / Repetition Statements

CS242 COMPUTER PROGRAMMING

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:

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

M1-R4: Programing and Problem Solving using C (JAN 2019)

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


CSE 230 Intermediate Programming in C and C++ Functions

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Procedural programming with C

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Computer Programming: C++

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

Standard Version of Starting Out with C++, 4th Edition. Chapter 6 Functions. Copyright 2003 Scott/Jones Publishing

STUDENT LESSON A12 Iterations

Chapter 5 C Functions

Introduction to Programming

Chapter 3 Function Overloading

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

COMPUTER APPLICATION

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Programming for Engineers Arrays

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Fundamentals of Programming Session 14

Transcription:

Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2013 C++ Programming Language Lab # 6 Functions

C++ Programming Language Lab # 6 Functions Objective: To be familiar with C++ Programming Language. Introduction: A function : is a collection of statements that are grouped together to perform an operation. The following is its format: type name ( parameter1, parameter2,...) { statements } where: type is the data type specifier of the data returned by the function. name is the identifier by which it will be possible to call the function. parameters (as many as needed): Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas. statements is the function's body. It is a block of statements surrounded by braces { }. Define a function return value type method name formal parameters Invoke a funciton function header function body int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; parameter list return value int z = max(x, y); actual parameters (arguments) } return result; Notes: Function signature >> (function name,parameter list). The variables defined in the function header are known as formal parameters. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument Void function does not return a value. 2

Scope of variables: The scope of variables declared within a function or any other inner block is only their own function or their own block and cannot be used outside of them. Therefore, the scope of local variables is limited to the same block level in which they are declared. Nevertheless, we also have the possibility to declare global variables; These are visible from any point of the code, inside and outside all functions. In order to declare global variables you simply have to declare the variable outside any function or block; that means, directly in the body of the program. Calling Functions: Exercise 1: 3

Void Functions: We do not need to return any value. In this case we should use the void type specifier for the function. This is a special specifier that indicates absence of type. Exercise 2: Overloaded functions: In C++ two different functions can have the same name if their parameter types or number are different. That means that you can give the same name to more than one function if they have either a different number of parameters or different types in their parameters. 4

Exercise 3A: Exercise 3B: 5

Pass by Value &Pass By Reference: Pass by value: In pass be value mechanism copies of the arguments are created and which are stored in the temporary locations of the memory. The parameters are mapped to the copies of the arguments created. The changes made to the parameter do not affect the arguments. Pass by value mechanism provides security to the calling program. Exercise 4: Pass by reference: Pass by reference is the second way of passing parameters to the function. The address of the argument is copied into the parameter. The changes made to the parameter affect the arguments. The address of the argument is passed to the function and function modifies the values of the arguments in the calling function. Exercise 5: 6

Declaring functions: There is an alternative way to avoid writing the whole code of a function before it can be used in main or in some other function. This can be achieved by declaring just a prototype of the function before it is used, instead of the entire definition. This declaration is shorter than the entire definition, but significant enough for the compiler to determine its return type and the types of its parameters. Its form is: type name ( argument_type1, argument_type2,...); It is identical to a function definition, except that it does not include the body of the function itself (i.e., the function statements that in normal definitions are enclosed in braces { }) and instead of that we end the prototype declaration with a mandatory semicolon (;). The parameter enumeration does not need to include the identifiers, but only the type specifiers. Exercise 6: Recursivity: Recursivity is the property that functions have to be called by themselves. It is useful for many tasks, like sorting or calculate the factorial of numbers. 7

Exercise 7: to obtain the factorial of a number (n!) the mathematical formula would be: n! = n * (n-1) * (n-2) * (n-3)... * 1 more concretely, 5! (factorial of 5) would be: 5! = 5 * 4 * 3 * 2 * 1 = 120 Lab work: Apply the programming exercises practically on DEV C++ Program, and show the results to your instructor. Enter a number of students to get the average of them,, by a function that receive the grades by a while loop and calculate the average of the grades. Home work: Implement the following integer function : a) Function Celsius return the Celsius equivalent of a Fahrenheit temperature. b) Function Fahrenheit return the Fahrenheit equivalent of a Celsius temperature. c) Use these functions to write a program that prints charts showing the Fahrenheit equivalents of all Celsius temperatures from 0 to 10 degrees, and the Fahrenheit temperatures from 32 to 42 degrees. Print the outputs in a neat tabular format. celsius = (5.0 / 9.0) * (Fahrenheit - 32.0) fahrenheit = (9.0/5.0)*( Celsius +32.0) 8

The Fibonacci series 0,1,2,3,5,8,13,21,. Begins with the term 0 and 1 and has the property that each succeeding term is sum of the two preceding terms. Write a recursive function Fibonacci(n)that calculates the nth Fibonacci number. Write a function distance that calculates the distance between two points (x1,y1) and (x2,y2). all numbers and return values should be of type float. Then call the function in the main, receive the values from keyboard to calculate the distance. 9