C++ Functions Lecture Notes PDF
Document Details
Uploaded by Deleted User
Prikshat Kumar Angra
Tags
Summary
This document is a set of lecture notes on C++ functions. It covers different aspects of functions, including their declaration, definition, different types of functions calls, and examples with programs.
Full Transcript
ECAP44 ORIENTED PROGRAMMING 4 USING C++ Prikshat Kumar Angra Assistant Professor Learning Outcomes After this lecture, you will be able to understand C++ functions. understand C++ function arguments. C++ Functions The function in C++ l...
ECAP44 ORIENTED PROGRAMMING 4 USING C++ Prikshat Kumar Angra Assistant Professor Learning Outcomes After this lecture, you will be able to understand C++ functions. understand C++ function arguments. C++ Functions The function in C++ language is also known as procedure or subroutine in other programming languages. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. Why We Need Functions in C++ Debugging of the code would be easier if you use functions, as errors are easy to be traced. Use of functions reduce size of code. Duplicate set of statements are replaced by function calls. Improve reusability of code, same function can be used in any program. Use of function improves readability of code. C++ Functions Types C++ Functions Library Functions User-define Functions Library Functions It is already present inside the header file which we always include at the beginning of a program. You just have to type the name of a function and use it along with the proper syntax. User-defined Function User-defined function is a type of function in which we have to write a body of a function and call the function whenever its require. Function Activities Function Declaration Function Definition Function Call Function Declaration A function declaration is also called "Function prototype”. we just specify the name of a function that we are going to use in our program like a variable declaration Function Declaration return_data_type function_name (data_type arguments); Function Definition Function definition means just writing the body of a function. A body of a function consists of statements which are going to perform a specific task. Function Definition int sum(int a,int b) { int c; c=a+b; return c; } Function Call While creating a C++ function, you give a definition of what the function has to do. To use a function, you will have to call or invoke that function. Function Call Types Call by value Call by reference Call by value This method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. By default, C++ uses call by value to pass arguments. Program #include couta>>b; { cout