Subtopics - Functions Function Declaration Function Arguments Return Statements and values. By Hardeep Singh

Similar documents
13 Classes & Objects with Constructors/Destructors

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES CONSTRUCTORS AND DESTRUCTORS

Chapter 5 Functions. Introducing Functions

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Chapter One Introduction to Programming

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

6. Control Structures

Appendix K Introduction to Microsoft Visual C++ 6.0

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Passing 1D arrays to functions.

Basics of I/O Streams and File I/O

C++FA 5.1 PRACTICE MID-TERM EXAM

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Data Structures using OOP C++ Lecture 1

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

C++ DATA STRUCTURES. Defining a Structure: Accessing Structure Members:

Member Functions of the istream Class

Comp151. Definitions & Declarations

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

While Loop. 6. Iteration

Common Beginner C++ Programming Mistakes

Ch 7-1. Object-Oriented Programming and Classes

Lecture 2 Notes: Flow of Control

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Computer Programming C++ Classes and Objects 15 th Lecture

Lesson Name: Introduction of OOP

Chapter 8 Selection 8-1

CS201-introduction to programing Latest Solved subjective from Midterm Papers Lectures 1-22 Latest subjectives MIDTERM EXAMINATION Spring 2012

J a v a Quiz (Unit 3, Test 0 Practice)

Answers to Review Questions Chapter 7

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

COSC 181 Foundations of Computer Programming. Class 6

Conditions & Boolean Expressions

Introduction to Java

Introduction to Object-Oriented Programming

Compiler Construction

Schedule. Structures and Classes in C++ Outline. Goals for This Topic. Another Example of a Structure. What is a Structure? Classes May 12-17, 2005

Example of a Java program

More C++ Concepts. Operator overloading Friend Function This Operator Inline Function

1. The First Visual C++ Program

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

COMPUTER SCIENCE 1999 (Delhi Board)

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Moving from C++ to VBA

Syllabus OBJECT ORIENTED PROGRAMMING C++

C++ Overloading, Constructors, Assignment operator

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

Moving from CS 61A Scheme to CS 61B Java

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Sources: On the Web: Slides will be available on:

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

JAVA ARRAY EXAMPLE PDF

arrays C Programming Language - Arrays

Subject Name: Object Oriented Programming in C++ Subject Code:

Chapter 2: Elements of Java

Chapter 9 Text Files User Defined Data Types User Defined Header Files

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Constructor, Destructor, Accessibility and Virtual Functions

An Incomplete C++ Primer. University of Wyoming MA 5310

Course notes Standard C++ programming

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

Solving Problems Recursively

7.7 Case Study: Calculating Depreciation

C++ program structure Variable Declarations

C++ INTERVIEW QUESTIONS

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Function Overloading I

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Basics of C++ and object orientation in OpenFOAM

CISC 181 Project 3 Designing Classes for Bank Accounts

Functions. If the inline function is called frequently, the size of the executable file increases and requires more memory.

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

COMPUTER SCIENCE. Paper 1 (THEORY)

Class 16: Function Parameters and Polymorphism

EP241 Computer Programming

Transcription:

Subtopics - Functions Function Declaration Function Arguments Return Statements and values

FUNCTIONS Functions are building blocks of the programs. They make the programs more modular and easy to read and manage. All C++ programs must contain the function main( ). The execution of the program starts from the function main( ).

Function is divided into three sections Function Declaration Function Call Function Definition

Form of Function Syntax: Function Declaration return_type function_name(parameter list) ; Syntax : Function Call int main() Function_name(parameter list); Syntax: Function Definition

Function Declaration A function declaration is made by declaring the return type of the function, name of the function and the data types of the parameters of the function. Always terminated by semicolon. The general form of function declaration :- return_type function_name(parameter list);

The return_type specifies the type of the data the function returns. The parameter list could be empty. The parameter list should contain both data type and name of the variable. For example, int factorial(int n, float j)

Function Arguments Arguments contain the actual value which is to be passed to the function when it is called. The sequence of the arguments in the call of the function should be same as the sequence of the parameters in the parameter list of the declaration of the function. When a function call is made arguments replace the parameters of the function.

The Return Statement and Return values A return statement is used to exit from the function where it is. It returns a value to the calling code. The general form of the return statement is:- return expression;

Example #include<iostream.h> #include<conio.h> int factorial(int n); int main () int n1,fact; cout <<"Enter the number whose factorial has to be calculated" << endl; cin >> n1; fact=factorial(n1); cout << "The factorial of " << n1 << " is : " << fact << endl; getch(); return(0);

int factorial(int n) int i=0,fact=1; if(n<=1) return(1); else for(i=1;i<=n;i++) fact=fact*i; return(fact);

Subtopics- Parameters Pass/Call by Value Parameters Pass/Call by Reference Return by Reference

Pass/Call by value Copies of the arguments are created. The parameters are mapped to the copies of the arguments created. The changes made to the parameter do not affect the arguments.

Example #include<iostream.h> #include<conio.h> int add(int n); int main() int number,result; number=5; cout << " The initial value of number : " << number << endl; result=add(number);

cout << " The final value of number : " << number << endl; cout << " The result is : " << result << endl; getch(); return(0); int add(int number) number=number+100; return(number);

Pass/Call 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.

Example- #include<iostream.h> #include<conio.h> void swap(int &a,int &b) int t=a; a=b; b=t; int main() int m=1,n=2;

cout<<"value of m before swaping\t"<<m<<endl; cout<<"value of n before swaping\t"<<n<<endl; swap(m,n); cout<<"value of m after swaping\t"<<m<<endl; cout<<"value of n after swaping\t"<<n<<endl; getch();

Return by reference A function can also return a reference. Example: #include<iostream.h> #include<conio.h> int &max(int &x,int &y) if(x>y) return x; else return y;

int main() int m=1,n=2; max(m,n)=4; cout<<"value of m"<<m<<endl; cout<<"value of n"<<n<<endl; getch(); return 0;

Subtopics- Inline functions Default arguments Function overloading

Inline Functions An inline function is a function that expanded in line when it is invoked. That is the compiler replaces the function call with the corresponding function code. Syntax: inline function-header Function body

Example: #include <iostream.h> #include<conio.h> int multiply(int); int main( ) int x; cout<< "\n Enter the Input Value: "; cin>>x;

cout<<"\n The Output is: " << multiply(x); getch(); inline int multiply(int x1) return 5*x1;

Default Arguments Default values are specified when the function is declared. Compier looks at the prototype to see how many arguments function uses. Default arguments are useful in situations where some arguments always have the same value.

Example #include<iostream.h> #include<conio.h> int main() float amount; float value(float p,int n,float r=0.15); //prototype void printline(char ch='*',int len=40); //prototype printline(); //uses default values for argumennts amount = value(5000.00,5); //default for 3rd argument

cout<<"\n Final value = "<<amount<<"\n\n"; printline('='); //use default value for 2 nd argument return 0; float value(float p, int n, float r) int year =1; float sum = p; while(year <= n) sum = sum*(1+r); year = year+1;

getch(); return(sum); void printline(char ch, int len) for(int i=1;i<=len;i++) printf("%ch",ch); printf("\n");

Function Overloading A function is overloaded when same name is given to different function. The two functions with the same name will differ at least in one of the following. a) The number of parameters b) The data type of parameters c) The order of appearance

Example #include <iostream.h> #include<conio.h> class arithmetic public: void calc(int num1) cout<<"square of a given number: " <<num1*num1 <<endl; void calc(int num1, int num2 )

cout<<"product of two whole numbers: " <<num1*num2 <<endl; ; int main() //begin of main function arithmetic a; a.calc(4); a.calc(6,7); getch();