manish k parmar pgt (cs) k v vvnagar thursday, december 24, 2015 lesson on user defined function in...

35
Manish K Parmar PGT (CS) K V VVNagar Friday, June 17, 2022 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science Kendriya Vidyalaya VVNagar

Upload: adam-marsh

Post on 21-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Lesson on

USER DEFINED FUNCTION IN C++

Presented by

Manish K Parmar

PGT Computer Science

Kendriya Vidyalaya VVNagar

Page 2: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

USE DEFINED FUNCTIONS

C++

Page 3: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

FUNCTIONS

A Function groups a number of program statements into a single unit and gives it a name. It takes arguments and returns a value.

This unit can be called (invoked) from other parts of the program. Functions are the building blocks of C++ programs where all the program activity occurs.

Functions serve two purposes. They allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else', and they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.

Page 4: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Need for a Function

Monolethic program (a large single list of instructions) becomes difficult to understand. For this reason functions are used.

A Function has a clearly-defined objective (purpose) and a clearly-defined interface with other functions in the program.

A group of functions together is called a module.

Reduction in program size is the another reason. Any sequences of statements that is repeated in a program can be combined together to form a function. The function code has only one copy in memory, even though it may be executed as many times as a user needs.

Page 5: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Advantages of using functions in C++

A complex program can be divided into small subtasks and function subprograms can be written for each.

These are easy to write, understand and debug

A function can be utilized in many programs by separately compiling it and loading them together.

C++ has the facility of defining a function in terms of itself i.e. recursion.

Many functions such as cin, cout, sqrt etc. are kept in C++ library and the compiler of C++ is written for calling any such function.

Page 6: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Types of Functions

1. Library Functions / Built in Functions

Many functions are in built with C++ language by default. They are library functions and stored in the header files of C++.

Eg. pow(), getch(), exit(), etc.

2. User Defined Functions

User can create a new functions as per his/her requirement

Page 7: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Function Prototype

Like any variable in a C++ program it is necessary to prototype or declare a function before it’s use.

It informs the compiler that the function would be referenced at a later stage in the program.

Eg.void display_message();

is a function prototype or a declaration. Here void specifies that this function does not return any value.

Page 8: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Defining a Function

A Function must be defined prior to it’s use in the program. It contains the code of the function.

Syntax of a function definition :

Type Name_of_the_function (argument list)

{

// body of the function

}

Page 9: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Defining a Function

Example :

int Sum (int a, int b)

{

int c;

c = a + b;

return c;

}

Page 10: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Function Call / Invoke a Function

A Function can be used by calling it or invoking it in the main program.

Eg.

Total = sum(5, 9);

Page 11: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Example of A Function

#include<iostream.h>

int Cube(int);

void main()

{

int number, cube;

cout << “\n Enter No :”;

cin >> number;

cube = Cube(number);

cout << “\n Cube of the no is : “ << cube;

}

Int Cube(int n)

{

return n * n * n;

}

Page 12: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Arguments

The inputs which are given to the functions are known as Function Arguments.

Constant Arguments

Default Arguments

Page 13: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Arguments

Constant Arguments

void max (const float x, const float y);

Here, the qualifier const informs the compiler that the arguments having const should not be modified by the function max().

Page 14: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Arguments

Default Arguments

C++ allows a function to assign a parameter the default value in case no argument for that parameter is specified in the function call.

void pattern (char M, int B = 2);

to print the character M for B times.

if pattern(‘@’, 4) then it prints @ four times.

if pattern(‘*’) then it printe * two times.

Page 15: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Calling Functions

Function can be called by two ways.

1. Call by Value

2. Call by Reference

Page 16: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Call by Value

Here the function is called by the value of arguments. Here two words are used Actual Parameter and Formal Parameter.

The parameter passed in function call is known as Actual Parameter, while the parameter declared in the definition is called as Formal Parameter.

Page 17: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Call by Value

#include<iostream.h>

int Sum(int, int);

void main()

{

int n1, n2, Total;

cout << “\n Enter No 1 :”; cin >> n1;

cout << “\n Enter No 2 :”; cin >> n2;

Total = Sum(n1, n2); // n1, n2 : Actual Parameters

cout << “\n Total : “ << Total;

}

int Sum (int a, int b) // a, b : Formal Parameters

{

return a + b;

}

Page 18: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Call by Reference

A reference provides an alias – a different name – for the variable, that is, the same variable’s value can be used by two different names : the original name and the alias name.

In call by reference method, a reference to the actual arguments in the calling program is passed (only variables). So the called function does not create its own copy of original value(s) but works with the original values with different name.

Page 19: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Call by Reference

#include<iostream.h>

void main()

{

int n1, n2, Total;

int Sum(int &a, int &b);

cout << “\n Enter No 1 :”; cin >> n1;

cout << “\n Enter No 2 :”; cin >> n2;

Total = Sum(n1, n2);

cout << “\n Total : “ << Total;

}

int Sum (int &a, int &b)

{

return a + b;

}

Page 20: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

How control passed between functions

#include<iostream.h>

int Sum(int, int);

void main()

{

int n1, n2, Total;

cout << “\n Enter No 1 :”;

cin >> n1;

cout << “\n Enter No 2 :”;

cin >> n2;

Total = Sum(n1, n2);

cout << “\n Total : “ << Total;

}

int Sum (int a, int b)

{

return a + b;

}

Page 21: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Returning from Function…

Return Statement is used to return a value from function.int fun(){………return <variable> or <value>;}

A function can return only a single value.

There may be three types of functions :

Computational Functions : Calculate some value and return it.

Manipulative Functions : Manipulate information and return success or failure code.

Procedural Functions : Perform an action and have no explicit return value.

Page 22: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Returning (by) Reference

A fuction may also return a reference.

int & min (int & a, int & b){

if (a < b)return a;

elsereturn b;

}

This function returns reference to a or b rather than returning values.

For eg. min (x, y) = -5;

It assigns -5 to lesser of the two x and y.

Page 23: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Function Classification…

Function can be classified into four categories as per the arguments and return values.

1. No Arguments and No Return Values

2. No Arguments but Return Values

3. Arguments but No Return Values

4. Arguments and Return Values

Page 24: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

No Arguments No Return Values

#include<iostream.h>

void print_name();

void main()

{

print_name();

}

void print_name()

{

cout << “\n Sachin Tendulkar”;

}

Page 25: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

No Arguments But Return Values

#include<iostream.h>

int get_number();

void main()

{

int n;

n = get_number();

}

int get_number()

{

int a;

cout << “\n Enter No : ”; cin >> a;

return a;

}

Page 26: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Arguments but No Return Values

#include<iostream.h>

void print_name(int);

void main()

{

print_name(10);

}

void print_name(int n)

{

for (int i= 1; i<=n; i++)

cout << “\n Sachin Tendulkar”;

}

Page 27: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Both Arguments and Return Values

#include<iostream.h>

int Sum(int, int);

void main()

{

int n1, n2, Total;

cout << “\n Enter No 1 :”; cin >> n1;

cout << “\n Enter No 2 :”; cin >> n2;

Total = Sum(n1, n2); // n1, n2 : Actual Parameters

cout << “\n Total : “ << Total;

}

int Sum (int a, int b) // a, b : Formal Parameters

{

return a + b;

}

Page 28: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Scope Rules

The program part (s) in which a particular piece of

code or a data value (variable) can be accessed is

known as the piece-of-code’s or variable’s scope.

Four kinds of Scope in C++ :

(i) Local Scope (Block)

(ii) Function Scope

(iii) File Scope

(iv) Class Scope

Page 29: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Scope of Variable

#include<iostream.h>

int global_var; // Global Variable

void main()

{

int local_var; // Local Variable/Fun Scope

cout << ::global_var; // :: - scope resolution operator

{

char Block_var; // Block Variable

}

}

void fun()

{

float fun_var; // Function Scope

}

Page 30: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Example of Scope

Find out the output of the following code :

#include<iostream.h>

int a = 20;

void main()

{

int a = 50;

cout << ::a << “\n” << a;

}

Page 31: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Example of Scope

Find out the output of the following code :

#include<iostream.h>

int a = 20;

void main()

{

int a = 50;

cout << ::a << “\n” << a;

}

Output :

20

50

Page 32: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Example of ScopeFind out the Output for the following :

#include<iostream.h>#include<conio.h>int g=10;void func(int &x, int y){

x = x - y;y = x *10;cout<<x<<“ – “<<y<<endl;

}void main( ){

int g=7;func(::g, g);cout<<g<<“ – “<<::g<<endl;func(g, ::g);cout<<g<<“ – “<<::g<<endl;

}

Page 33: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Example of ScopeFind out the Output for the following :

#include<iostream.h>#include<conio.h>int g=10;void func(int &x, int y){

x = x - y;y = x *10;cout<<x<<“ –

“<<y<<endl;}void main( ){

int g=7;func(::g, g);cout<<g<<“ –

“<<::g<<endl;func(g, ::g);cout<<g<<“ –

“<<::g<<endl;}

OUTPUT

3 – 307 – 34 – 404 – 3

Page 34: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Lifetime of a variable

The time interval for which a particular variable or data

value lives in the memory is called Lifetime of the

variable or data value.

Generally a variable’s life time is its parent-block-run i.e.

as long as its parent block is executing, the variable

lives in the memory.

The global variable’s life time is the program-run.

The life time of local variable having function scope is

the function-run.

Page 35: Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science

Manish K ParmarPGT (CS)

K V VVNagar

Friday, April 21, 2023

Thank You…