Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:

Similar presentations


Presentation on theme: "Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:"— Presentation transcript:

1 Chapter 6: Function

2 Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared: – Inside a function or a block which is called local variables, – In the definition of function parameters which is called formal parameters. – Outside of all functions which is called global variables. We will learn what is a function and it's parameter in subsequent chapters. Here let us explain what are local and global variables.

3 Local Variables Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own.

4 Example of Local Variable

5 Global Variables Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program. A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables:

6 Example of Global Variable

7 What new of variable ? A program can have same name for local and global variables but value of local variable inside a function will take preference.

8 Initializing Local and Global Variables When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them as follows:

9 Example It is a good programming practice to initialize variables properly, otherwise sometimes program would produce unexpected result.

10 FUCTION A function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate 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 specific task.

11 FUNCTION (cont.) 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 The C++ standard library provides numerous built-in functions that your program can call. For example, function strcat() to concatenate two strings, function memcpy() to copy one memory location to another location and many more functions.. A function is knows as with various names like a method or a sub-routine or a procedure etc.

12 Defining a Function The general form of a C++ function definition is as follows

13 Defining a Function (cont.) A C++ function definition consists of a function header and a function body. 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 returns. Some functions perform the desired 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. – 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 define what the function does.

14 Example of Function Following is the source code for a function called max(). This function takes two parameters num1 and num2 and returns the maximum between the two:

15 Functions with no type. The use of void Requires the declaration to begin with a type. This is the type of the value returned by the function. But what if the function does not need to return a value? In this case, the type to be used is void, which is a special type to represent the absence of value.

16 Example of void Function

17


Download ppt "Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:"

Similar presentations


Ads by Google