Download presentation
Presentation is loading. Please wait.
Published byAdam Marsh Modified over 9 years ago
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 Kendriya Vidyalaya VVNagar
2
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 USE DEFINED FUNCTIONS C++
3
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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.
4
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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.
5
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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.
6
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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
7
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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.
8
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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 }
9
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Defining a Function Example : int Sum (int a, int b) { int c; c = a + b; return c; }
10
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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);
11
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Example of A Function #include 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; }
12
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Arguments The inputs which are given to the functions are known as Function Arguments. Constant Arguments Default Arguments
13
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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().
14
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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.
15
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Calling Functions Function can be called by two ways. 1. Call by Value 2. Call by Reference
16
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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.
17
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Call by Value #include int Sum(int, int); void main() { int n1, n2, Total; cout > n1; cout > 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; }
18
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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.
19
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Call by Reference #include void main() { int n1, n2, Total; int Sum(int &a, int &b); cout > n1; cout > n2; Total = Sum(n1, n2); cout << “\n Total : “ << Total; } int Sum (int &a, int &b) { return a + b; }
20
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 How control passed between functions #include 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; }
21
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Returning from Function… Return Statement is used to return a value from function. int fun() { … return or ; } 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.
22
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Returning (by) Reference A fuction may also return a reference. int & min (int & a, int & b) { if (a < b) return a; else return 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.
23
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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
24
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 No Arguments No Return Values #include void print_name(); void main() { print_name(); } void print_name() { cout << “\n Sachin Tendulkar”; }
25
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 No Arguments But Return Values #include int get_number(); void main() { int n; n = get_number(); } int get_number() { int a; cout > a; return a; }
26
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Arguments but No Return Values #include 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”; }
27
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Both Arguments and Return Values #include int Sum(int, int); void main() { int n1, n2, Total; cout > n1; cout > 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; }
28
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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
29
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Scope of Variable #include 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 }
30
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Example of Scope Find out the output of the following code : #include int a = 20; void main() { int a = 50; cout << ::a << “\n” << a; }
31
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Example of Scope Find out the output of the following code : #include int a = 20; void main() { int a = 50; cout << ::a << “\n” << a; } Output : 20 50
32
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Example of Scope Find out the Output for the following : #include 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; }
33
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Example of Scope Find out the Output for the following : #include 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 – 30 7 – 3 4 – 40 4 – 3
34
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 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.
35
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Thank You…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.