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

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

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

CS1150 Principles of Computer Science Methods

CS1150 Principles of Computer Science Methods

Lecture 2:- Functions. Introduction

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

Lecture 5: Methods CS2301

What Is a Function? Illustration of Program Flow

by Pearson Education, Inc. All Rights Reserved. 2

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

Object oriented programming C++

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

Topic 2: C++ Programming fundamentals

Methods. CSE 114, Computer Science 1 Stony Brook University

Object Oriented Design

C Programming for Engineers Functions

( &% class MyClass { }

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

Fundamentals of Programming Session 13

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

EECS168 Exam 3 Review

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

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

Multiple-Subscripted Arrays

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

Lab Manual. ISC 115 Computing Foundations. Information Technology Solutions

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

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

Chapter-13 USER DEFINED FUNCTIONS

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

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

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

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

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

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

CS242 COMPUTER PROGRAMMING

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Unit 7. Functions. Need of User Defined Functions

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.

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

CT 229 Methods Continued

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

Methods CSC 121 Fall 2014 Howard Rosenthal

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

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

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

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

UNIT 3 FUNCTIONS AND ARRAYS

Chapter 3 Function Overloading

COMPUTER APPLICATION

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.

Introduction to Programming (Java) 4/12

Fundamentals of Programming Session 14

Functions and Recursion

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

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

Chapter 3 - Functions

Lab Instructor : Jean Lai

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

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

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:

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

Lecture 3 Tao Wang 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Anatomy of a Class Encapsulation Anatomy of a Method

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

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

CS-201 Introduction to Programming with Java

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

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

Chapter 4 Defining Classes I

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

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

CS-201 Introduction to Programming with Java

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.

Computer Science II Lecture 1 Introduction and Background

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

CS111: PROGRAMMING LANGUAGE II

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

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Introduction to Programming

CSCE 206: Structured Programming in C++

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

Computer Programming: C++

Lecture 5 Tao Wang 1

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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


CSE 230 Intermediate Programming in C and C++ Functions

Lecture 9 - C Functions

Implementing Abstract Data Types (ADT) using Classes

Function Call Stack and Activation Records

Loops / Repetition Statements

Fundamentals of Programming Data Types & Methods

C: How to Program. Week /Apr/16

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:

Transcription:

Lab 4 Functions 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 Invoke a funciton return value type method name formal parameters 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. 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: 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. Exercise 3A:

Exercise 3B: 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:

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:

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) The Fibonacci series 0,1,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.