Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link
Posts

7.2 Declaration, call & argument

2 min read
Image result for Declaration, call or argument C++ 
Function Declaration
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.

A function declaration has the following parts:
return_type function_name( parameter list );

For our defined function add() in previous chapter, following is the function declaration:
int add(int a, int b);

Parameter names are not important in function declaration only their type is required, so following is also valid declaration:
int add(int, int);

Function declaration is required when you define a function in one source file 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.

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.

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.

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:
int main ()
{
int a = 100;
int b = 200;
int ret;
// calling a function to get addition.
ret = add(a, b);
cout << "addition is : " << ret << endl;
return 0;
}

Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.
The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.

While calling a function, there are two ways that arguments can be passed to a function:
Call by value
This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

Call by pointer
This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

Call by reference
This method copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

                      7.3 Inline function          NEXT >>

You may like these posts

  • Pointer is a variable that stores the address of another variable. They can make some things much easier, help improve your program's efficiency, and even allow you to handle u…
  • On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. cout is an instance of iostream class …
  • In Array we can store data of one type only, but structure is a variable that gives facility of storing data of different data type in one variable. Structures are variab…
  • In most program environments, the standard input by default is the keyboard, and the C++ stream object defined to access it is cin. cin is an instance of iostream class For f…
  • References are another variable type of C++ that act as an alias or short name to another variable. A reference variable acts just like the original variable it is referencin…
  • C++ allows us to create own own data types. enumerated type is the simplest method for doing so. An enumerated type is a data type where every possible value is defined as a s…

Post a Comment