CSE202-Lec#4. CSE202 C++ Programming

Similar documents
Chapter-13 USER DEFINED FUNCTIONS

C++ 8. Constructors and Destructors

UNIT 3 FUNCTIONS AND ARRAYS

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

Downloaded from

Darshan Institute of Engineering & Technology for Diploma Studies

SUHAIL T A

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Downloaded from

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

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

Functions. Introduction :

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

cout<< \n Enter values for a and b... ; cin>>a>>b;

Sample Paper Class XI Subject Computer Sience UNIT TEST II

Lecture 5: Methods CS2301

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

Kapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8

Subject: Fundamental of Computer Programming 2068

Methods. CSE 114, Computer Science 1 Stony Brook University

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

FUNCTIONS OMPAL SINGH

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Introduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department

Bangalore South Campus

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

Chapter 4 Functions By C.K. Liang

User Defined Functions

True or False (12 Points)

C Programming Lecture V

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

Object Oriented Design

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

SECTION A (15 MARKS) Answer ALL Questions. Each Question carries ONE Mark. 1 (a) Choose the correct answer: (10 Marks)

Functions BCA-105. Few Facts About Functions:

COMPUTER SCIENCE (083)

6. C++ Subprograms David Keil CS I slides 7/03 1

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

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

OOP THROUGH C++(R16) int *x; float *f; char *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.

Solution: A pointer is a variable that holds the address of another object (data item) rather than a value.

include<iostream.h> #define PI=3.14 void main( ) { float r;a; cout<< enter any radius ; cin>>r; a=pi*pow(r,2); cout<< Area= <<a }

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

OBJECTIVE QUESTIONS: Choose the correct alternative:

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

UNIT- 3 Introduction to C++

CLASS-XI COMPUTER SCIENCE

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

DE122/DC106 Object Oriented Programming with C++ DEC 2014

UNIT POLYMORPHISM

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Lecture 2:- Functions. Introduction

DELHI PUBLIC SCHOOL TAPI

CS1150 Principles of Computer Science Methods

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Creating a C++ Program

Abstraction in C++ Abstraction is the concept of exposing only the required essential characteristics and behavior

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

UNDERSTANDING THE COMPUTER S MEMORY

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

CSE 230 Intermediate Programming in C and C++ Functions

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

#include<iostream.h> { for(int L=1;L<=N;L++) cout<<m*l; cout<<endl; } void main()

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

IT 1003 Introduction to Programming (New)

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

Pointer Data Type and Pointer Variables

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

True or False (15 Points)

BRAIN INTERNATIONAL SCHOOL. Term-I Class XI Sub: Computer Science Revision Worksheet

Module Operator Overloading and Type Conversion. Table of Contents

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Implementing Abstract Data Types (ADT) using Classes


CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

CSE202- Lec#6. (Operations on CSE202 C++ Programming

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

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

CS1150 Principles of Computer Science Methods

KE DRIYA VIDYALAYA SA GATHA CHE AI REGIO COMMO PREBOARD EXAMI ATIO COMPUTER SCIE CE- CLASS- XII. Marking scheme

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

b) Give the output of the following program: 6,70,70 2,70 210,282,59290

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Downloaded from

Name SECTION: 12:45 2:20. True or False (12 Points)

Chapter 4 Defining Classes I


UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

C++_ MARKS 40 MIN

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Transcription:

CSE202-Lec#4 Functions and input/output streams @LPU CSE202 C++ Programming

Outline Creating User Defined Functions Functions With Default Arguments Inline Functions @LPU CSE202 C++ Programming

What is function???? Function is a self contained block of statements that perform a coherent task of some kind. Every C++ program can be a thought of the collection of functions. main()isalsoafunction.

Types of Functions. Library functions Thesearethein--builtfunctionsof C++ library. These are already defined in header files. User defined functions. Programmer can create their own function in C++ to perform specific task

Why use function? Writing functions avoids rewriting of the same codeagainandagainintheprogram. Using function large programs can be reduced to smaller ones. It is easy to debug and find outtheerrorsinit. Using a function it becomes easier to write programtokeeptrackofwhattheyaredoing.

//Function Declaration retn_type func_name(data_type 1,data_type par2); //Function Defination rent_type func_name(data_typepar1,data_type par2) // body of the function //Function Call func_name(par1,par2);

Function prototype A prototype statement helps the compiler to check the return type and arguments type of the function. A prototype function consist of the functions return type, name and argument list. Example intsum(int x,int y);

Example #include<iostream.h> #include<conio.h> void main() clrscr(); void sum(int, int); // function declaration int a, b, c; cout<< enter the two no ; cin>>a>>b; sum(a,b); // function calling getch(); void sum( int x, int y) // function definition c=x+y; cout<< sum is <<c;

#include<conio.h> void main() clrscr(); int a=10,b=20; int sum(int, int); int c=sum(a,b); Cout<< sum is << c; getch(); int sum(int x, int y) int s; s=x+y; return(s); /*actual arguments /*return value /*formal arguments

Categories of functions A function with no parameter and no return value A function with parameter and no return value A function with parameter and return value A function without parameter and return value

A function with no parameter and no return value #include<conio.h> void main() clrscr(); void print(); /*func declaration Cout<< no parameter and no return value ; print(); /*func calling getch(); void print() /*func defination For(int i=1;i<=30;i++) cout<< * ; Cout<< \n ;

A function with no parameter and no return value There is no data transfer between calling and called function The function is only executed and nothing is obtained Such functions may be used to print some messages, draw stars etc

A function with parameter and no return value #include<conio.h> void main() clrscr(); int a=10,b=20; void mul(int,int); mul(a,b); getch(); void mul(int x, int y) int s; s=x*y; Cout<< mul is << s; /*actual arguments /*formal arguments

A function with parameter and return value #include<conio.h> void main() clrscr(); int a=10,b=20,c; int max(int,int); c=max(a,b); Cout<< greatest no is <<c; getch(); int max(int x, int y) if(x>y) return(x); else return(y);

A function without parameter and return value #include<conio.h> void main() clrscr(); int a=10,b=20; int sum(); int c=sum(); /*actual arguments Cout<< sum is << c; getch(); int sum() /*formal arguments int x=10,y=20; return(x+y); /*return value

Default arguments In the function prototype (declaration), the default values are given. Whenever a call is made to function without specifying an argument, the program will automatically assign values to the parameters from the default function prototype. Default arguments facilitate easy development and maintenance of programs.

#include<iostream.h> void sum(int x=10, int y=20); void main() sum(); void sum(int a1,int a2) Int temp; temp= a1+a2; Cout<< Sum is <<temp;

Inline functions One of the objectives of using functions is to save some memory space, which becomes appreciable when a function is likely to be called many times. Everytimeafunctioniscalled,ittakesalotof extra time in executing a series of instructions. Toeliminatethecostofcallstosmallfunctions, C++ proposes inline function.

An inline function is a function that expanded in line when it is invoked. The compiler replaces the function call with corresponding function code. inline function header function body

Prefix the keyword inlineto the function definition. #include<iostream.h> inline float mul(float x, float y) return(x*y); Inline double div(double p, double q) return(p/q); void main() float a = 12.345; float b = 9.82 cout<< mul(a,b) << \n ; cout<<div( a,b)<< \n

Situation where inline expansion do not work For functions returning values, if loop, a switch or a gotoexists. If function contain static variable. If inline functions are recursive