Unit One Functions PDF

Summary

This document provides an introduction to C++ functions, covering concepts such as function declarations and definitions. It includes explanations and examples of function calls, parameters (arguments), and return types. The use of functions to organize and modularize code is emphasized.

Full Transcript

Unit One FUNCTIONS 1.1. Introduction 2  A function is a group of statements that together perform a task.  Functions are building blocks of the programs. They make the programs more modular and easy to read and manage.  Creating an application us...

Unit One FUNCTIONS 1.1. Introduction 2  A function is a group of statements that together perform a task.  Functions are building blocks of the programs. They make the programs more modular and easy to read and manage.  Creating an application using function makes it easier to understand, edit, check errors etc.  Every C++ program has at least one function, which is main(). 1.1. Introduction 3  The execution of the program starts from the function main( ).  Yo u c an d i v i d e up yo ur c o d e i nto se p arate functions.  How you divide up your code among different functions is up to you, but logically the division usually is so each function performs a specif ic task.  A C+ + p rog ram c an c ontai n any num b e r of functions according to the needs. 1.2. Function Declarations 4  Function declaration, is done to tell the compiler about the existence of the function.  A function declaration tells the compiler about a function name, return type, parameters and how to call the function.  The actual body of the function can be def ined separately.  A function declaration has the following parts: return_type function_name( parameter list );  The following is the function declaration example: int max(int num1, int num2); 1.2. Function Declarations 5  Parameter names are not important in function declaration only their type is required, so following is also valid declaration: int max(int, int);  Function declaration is required when you def ine a function in one source f il e and you call that function in another file.  In such case, you should declare the function at the top of the file calling the function. 1.2. Function Declarations 6  For example function declaration can be: int add (int x, int y); int factorial (int n1,float j1); int factorial (int , float); 1.3. Defining a Function 7  The general form of a C++ function definition is as follows: return_type function_name( parameter list ) { body of the function }  A C++ function definition consists of a function header and a function body. 1.3. Defining a Function 8 Here are all the parts of a function:  Return Type: A function may return a value. The return_type is the data type of the value the function ret urns. Some func t ions p er form t he d esired operations without returning a value. In this case, the return_type is the keyword void.  Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature. 1.3. Defining a Function 9  Here are all the parts of a function:  Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.  Function Body: The function body contains a collection of statements that def ine what the function does. 1.3. Defining a Function 10  Example: int add (int x, int y) { int z; z = x + y; return z; } 1.4. Calling a Function 11  While creating a C++ function, you give a def inition of what the function has to do.  To use a function, you will have to call or invoke that function.  When a program calls a function, program control is transferred to the called function.  A called function performs defined task and when its return statement is executed or when its function ending closing brace is reached, it returns program control back to the main program. 1.4. Calling a Function 12  To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value. For example: 1.5. Function Arguments 13  The information is transferred to the function by the means of arguments when a call to a function is made.  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. 1.4. Function Arguments 14  The data types of the arguments should correspond with the data types of the parameters.  When a function call is made arguments replace the parameters of the function.  Example: sum = add (12, 56); fact = factorial ( z); 1.5. Return Statement and Return values 15  A return statement is used to exit /terminate from the function where it is.  It returns the execution of the program to the point where the function call was made.  It returns a value to the calling code.  The general form of the return statement is:- return expression;  The expression evaluates to a value which has type same as the return type specified in the function declaration. 1.5. Return Statement and Return values 16  For example the statement, return n;  is the return statement of the factorial function.  The type of variable n should be integer as specified in the declaration of the factorial function.  If a function has return type as void then return statement does not contain any expression.  It is written as: return; 1.6. Parameter passing mechanism 17  There are two parameter passing mechanisms for passing arguments to functions such as:  pass by value and  pass by reference. 1.6. Parameter passing mechanism 18  Pass by Value  Inpass be value mechanism copies of the arguments are created and which are stored in the temporary locations of the memory.  The parameters are mapped to the copies of the arguments created.  The changes made to the parameter do not affect the arguments.  Pass by value mechanism provides security to the calling program. 1.6. Parameter passing mechanism 19  Pass 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.  The address of the argument is passed to the function and function modif ie s the values of the arguments in the calling function. 1.6. Parameter passing mechanism 20  Default Values for Parameters  When you define a function, you can specify a default value for each of the last parameters.  This value will be used if the corresponding argument is left blank when calling to the function.  This is done by using the assignment operator and assigning values for the arguments in the function definition.  If a value for that parameter is not passed when the function is called, the default given value is used, but if a value is specif ied, this default value is ignored and the passed value is used instead. 1.7. Function overloading in C++ 21  C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function over loading and operator over loading respectively.  An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation). 1.7. Function overloading in C++ 22  When you call overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator.  You can have multiple definitions for the same function name in the same scope.  The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.  You cannot overload function declarations that differ only by return type. 1.7. Function overloading in C++ 23 #include void print(int i) { cout

Use Quizgecko on...
Browser
Browser