Chapter-13 USER DEFINED FUNCTIONS

Similar documents
CSE202-Lec#4. CSE202 C++ Programming

Downloaded from

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Functions. Introduction :

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

CHAPTER 4 FUNCTIONS. 4.1 Introduction

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

Darshan Institute of Engineering & Technology for Diploma Studies

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

C++ 8. Constructors and Destructors

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

UNIT 3 FUNCTIONS AND ARRAYS

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

Chapter-14 STRUCTURES

Subject: Fundamental of Computer Programming 2068

DELHI PUBLIC SCHOOL TAPI

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

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

Part II Answer to all the questions (2 Marks):

Unit 7. Functions. Need of User Defined Functions

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

Lab Instructor : Jean Lai

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

UNIT- 3 Introduction to C++

Downloaded from

Sample Paper Class XI Subject Computer Sience UNIT TEST II

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

Lecture 5: Methods CS2301

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI


Creating a C++ Program

What Is a Function? Illustration of Program Flow

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

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

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

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

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

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

C++ For Science and Engineering Lecture 15

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Unit 8. Structures and Unions. School of Science and Technology INTRODUCTION

Introduction to Programming (Java) 4/12

Functions. (transfer of parameters, returned values, recursion, function pointers).

Understand Execution of a Program

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

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

6.096 Introduction to C++

SUHAIL T A

CHAPTER 9 FLOW OF CONTROL

Padasalai.Net s Model Question Paper

Lecture 9 - C Functions

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

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

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

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

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

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

Chapter 6: Functions

CSc Introduction to Computing

Functions BCA-105. Few Facts About Functions:

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

Chapter 3 Function Overloading

Variable x is created and assigned a value 35. Variable is y created as a copy of x. Variables x and y are two separate memory locations.

BLUE PRINT SUBJECT: - COMPUTER SCIENCE(083) CLASS-XI. Unit Wise Marks

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

C Programming Lecture V

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

Lecture 2 Tao Wang 1

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

15 FUNCTIONS IN C 15.1 INTRODUCTION

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES

Bangalore South Campus

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

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

EL6483: Brief Overview of C Programming Language

LECTURE 06 FUNCTIONS

A Shotgun Introduction to C

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

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Lecture #6-7 Methods

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Pointer Data Type and Pointer Variables

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

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

Module 07: Programming in C++

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

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 }

PROGRAMMING IN C++ COURSE CONTENT

by Pearson Education, Inc. All Rights Reserved. 2

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

M.CS201 Programming language

EECS402 Lecture 02. Functions. Function Prototype


A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Transcription:

Chapter-13 USER DEFINED FUNCTIONS Definition: User-defined function is a function defined by the user to solve his/her problem. Such a function can be called (or invoked) from anywhere and any number of times in the program. The purpose of using a function is to make the program design process easy, understandable and thereby avoiding ambiguity. Advantages of user-defined functions: If there is set of statements to be repeated several times in the program, these statements can be replaced as a function and called whenever and whenever required. If a task in needed in more than one program or to a group of programmers, a function can be developed and made available to other programs or other programmers. Programs can be modularized into functions. Each function can be developed independently, debugged and tested. If a problem is divided into sub-problems, a team of programmers can be formed to solve each sub-problem, rather solving by a single person. If functions are developed in a program, the number of line decreases. Function definition or structure of user-defined function: Return type-specifiers: is the data type of the value return by the function to another function when it is called. It can be char, int, float or void. Data type void is used when the function return no value to calling function. Function-name: is the name of the function. It is an identifier to identify the particular function in 1 P a g e Keerthi Kumar H.M

a program. Argument-list with declaration: is the list of arguments or parameters or variables with their declaration. Each argument should be declared separately. Each declaration should be separated by comma. The list should be enclosed within parenthesis. The complete line is called the function header. Note that there is no semicolon at the end. Local-variable declaration: is the declaration of the variables that are used within the function. Since these variables are local to the function, these variables are called as local variables. Executable statements: are the statements that perform the necessary operations to solve the problem. If the function is returning a value, a return statement should be included. Otherwise return statement is not necessary. Local declaration and executable statements are together called as body of the function. The body of the function should be enclosed within the curled braces. Sample Program: To find the greatest of three numbers. #include<iostream.h> #include<conio.h> int biggest (int, int, int); int a, b, c, big; clrscr( ); cout<< Enter the three numbers ; cin>>a>>b>>c; big = biggest(a, b, c); cout<< Biggest= <<big<<endl; getch( ); int biggest (int x, int y, int z) int big; big = x>y?(x>z?x:z):(y>z?y:z); return (big); //Function prototype // Function call-statement Calling Function Called Function Calling a function: The definition of the program can come either before the main( ) function or after the main( ) function. Calling function is a function that transfer control from it to another function by specifying the name of that function and passing arguments. Called function is a function that receives the call and arguments from another calling function. 2 P a g e Keerthi Kumar H.M

Function call is the statement that is used to call or make another function execute. When a function call is made, the control jumps calling function to the called function. The general form of function call is: variable = function_name (arugument list); OR variable = function_name( ); big = biggest(a, b, c); main ( ) function: The main ( ) function returns a value of type int to the operating system. If the program runs successfully, 0 is returned. Otherwise, a non-zero value is returned to the operating system, indicating that the program contains errors. If the main ( ) function is not returning a value, the data type void can be used as return type specifiers. The general form of main ( ) function is: int main ( ) Executable-Statements; return 0; Returning a value: Executable-Statements; When a function is called, the statements in the called function are executed. After executing the statements, the function returns value to the calling function. The return statement is used to return a value. A function can return only one value or none to the calling function, for every function call. The general form of return statements is: Function Prototype: return (expression); OR return 0; A function prototype is a declaration of the function that tells the program about the type of the value return by the function and the number and type of arguments. The general form of function prototype is: return-type-specifier function-name ( type arg1, type arg2.) OR return-type-specifier function-name ( type, type.) int biggest(int x, int y, int z); 3 P a g e Keerthi Kumar H.M OR int biggest( int, int, int);

Types of Arguments: A variable in a function header is called an argument. The arguments are used to pass information from the calling function to the called function. Actual Arguments: The function call statement contains name of the function and the list of arguments to be passed. These arguments or parameters are called as actual arguments. The arguments can be constants, variables, or expressions. : big = biggest (a, b, c); Formal Arguments: The function header contains return-type-specifier, function name and list of arguments with their declaration. These arguments are called as formal arguments or dummy arguments. Formal arguments get their values from the actual arguments. : int biggest (int x, int y, int z) Local Variables: The variables declared inside function or block is said belong to that block. These variables are called as Local variables. Values of local variables are accessible only in that block. The function s formal arguments are also considered as local variables. : Global Variables: function-name (int a, int b) int x, y; ; //x and y are local variables; The variables declared outside the function are called as global variables. These variables are referred by the same data type and same name throughout the program in both calling function and called function. Whenever if some variables are to treated as same value in both main( ) and in other functions, it is advisable to use global variables. : 4 P a g e Keerthi Kumar H.M

int a, b; int p, q;. // a and b are global variables // p and q are local variables Program: To find the LCM of two numbers: #include<iostream.h> #include<conio.h> int a,b; // a and b are global variables int gcd(int x, int y) int r; // r is local variable while(b!=0) r = a%b; a=b; b=r; return a; int gcd(int x, int y) //Function Prototype int product, lcm, g; clrscr( ); cout<< enter two numbers: ; cin>>a>>b; produt= a *b; g = gcd(a, b); lcm = product/g; cout<< GCD= <<g<<endl; cout<< LCM= <<lcm<<endl; getch(); Scope of Variables: Scope of variables refers to the part of the program where the value of the variable can be used. The scope of the variables begins from where the variable declared. If the declaration is inside a function, the scope extends to the end of the innermost block that contains the declaration. : To illustrate scope of variables #include<iostream.h> 5 P a g e Keerthi Kumar H.M

#include<conio.h> void f( ); void g( ); int x=10; int x = 20; int x = 30; cout<< In block inside main( ): x= <<x<<endl; cout<< In main(): x= <<x<<endl; cout<< In main(): ::x= <<::x<<endl; f( ); g( ); void f() cout<< In f(): x= <<x<<endl; void g() int x=40; cout<< In g(): x= <<x<<endl; Types of Function: There are different types of functions: a. Function with no arguments and no return values. b. Function with arguments and with no return values. c. Function with no arguments and with return values. d. Function with arguments and with return values. e. Recursive function. Function with no arguments and no return values: In this method, the simply performs independent task. The function does not receive or send any arguments. void function-name( ) Statements; void natural( ) for(int i=1; i<=10; i++) cout<< \t <<I; 6 P a g e Keerthi Kumar H.M

Function with arguments and with no return values: In this method, the function receives some arguments and does not return any value. void function-name(argument list) Statements; void sum(int x, int y, int z ) int sum; sum= x + y+ z; cout<< Sum = <<sum; Function with no arguments and with return values: In this method, the function receives no arguments but return a value. return-type function-name() Statements; return(value) int greatest( ) if(a>b); return(a); else return(b); Function with arguments and with return values: In this method, the function receives some arguments and returns a value. return-type function-name(argument-list) Statements; return(value) Recursive function: float interest(float p, float t, float r) si = (p * t * r)/100; return(si); Recursive function is a function that calls itself. The process of calling function by itself is called as recursion. return-type function-name(argument-list) Statements; return(function-name(argument-list)) Passing default arguments to functions: In C++, to call a function we need not pass all the values for the arguments to a function from the calling function. 7 P a g e Keerthi Kumar H.M

It allows us to assign default values to the formal arguments. Default values can be assigned to the argument which does not have a matching argument int the function call. Default values are assigned in the function prototype. The default values are assigned to the arguments as we initialize variables. : float interest(float amt, int time, float rate =0.15) si = interest(5000,5) //third argument is missing si=interest(5000, 5, 0.2) //No missing arguments Uses of default values: Default arguments are useful in situations where some arguments always have the same value. Default arguments provide flexibility to the programmer. Default arguments are used to add new arguments to the existing functions. Default arguments are used to combine similar functions into one. Passing Constant arguments: In C++, we can declare some arguments as constants. The compiler cannot modify the arguments marked as constants. : int strlen(const char *p); int total(const int x, const int y); Pass by value or Call by Value: A function can be called by passing arguments from the calling function into the called function. Thus data is transferred through argument list. The calling function sends the data to the called function through actual parameters. The called function receives the data into its corresponding formal parameters. In pass by value, a copy of data sent by calling function stored in temporary locations, The called function uses these values as the initial values of the formal parameters. The formal parameters are processed or changed to generate the required result. However these changes are affected to the original values stored in the actual parameters. : 8 P a g e Keerthi Kumar H.M

void swap(int, int); //Function prototype int a, b; clrscr( ); cout<< Enter two numbers ; cin>>a>>b; cout<< Before calling the function: a= <<a<< and b= <<b<<endl; swap(a, b); //Actual Arguments cout<< After calling the function: a= <<a<< and b= <<b<<endl; getch(); void swap(int x, int y) //Formal Arguments int temp; temp = x; Output: Enter two numbers 10 20 x = y; y= temp; Before calling function: a=10 and b=20 After calling function: a=10 and b=20 Pass by reference or call by reference: Sometimes we need to change the values of the actual arguments in the calling function. This is not possible in the passing by value method. C++ allows us to pass arguments to the function by using reference variables. When we pass arguments by reference, the formal arguments int the called function become the aliases to the actual arguments of the calling function i.e. the called function is actually uses the original data with a different name. : void swap(int &, int &); //Function prototype int a, b; clrscr( ); cout<< Enter two numbers ; cin>>a>>b; 9 P a g e Keerthi Kumar H.M

cout<< Before calling the function: a= <<a<< and b= <<b<<endl; swap(a, b); //Actual Arguments cout<< After calling the function: a= <<a<< and b= <<b<<endl; getch(); void swap(int &x, int &y) //Formal Arguments int temp; temp = x; Output: Enter two numbers 10 20 x = y; y= temp; Before calling function: a=10 and b=20 After calling function: a=20 and b=10 CHAPTER 13 USER DEFINED FUNCTIONS BLUE PRINT VSA (1 marks) SA (2 marks) LA (3 Marks) Essay (5 Marks) Total 01 Question - - 01 Question 06 Marks ************** 10 P a g e Keerthi Kumar H.M