C++: Functions -1

The main advantage of using function is that it is possible to reduce the size of a program by calling and using them at different places in the program.

When the function is called,control is transferred to the first statement in the function body. The other statements in the function body are then executed and control returns to the main program when the closing brace is encountered.

The Main Function:

In C++,the main() returns a value of type int to the operating system.

     int main();
     int main(int argc, char * argv[]);

The function that have a return value should use the return statement for termination.

The normal convention is that a return value of zero means the program ran successfully, while a nonzero value means there was a problem.

The use of return(0) statement will indicate that the program was successfully executed.
__________________________________________

Function Prototyping:

The prototype describes the function interface to the compiler by diving details such as the number and the type of arguments and the type of return values.

With function prototyping, a template is always used when declaring and defining a function.When a function is called, the compiler uses the template to ensure that proper arguments are passed, and the return value is treated correctly.

     type function-name(argument-list);
eg
        float volume(int x,float y,float z);
or
        float volume(int , float, float);

since the name of arguments are dummy variables.

In the functioin definition, names are required because the arguments must be referenced inside the function.

       float volume(int a,float b,float c){ 
       float v=a*b*c*;
       ......
       .....
       }

The function volume() can be invoked in a program as:
    
     float cube=volume(b1,w1,h1); //Function call

The variable b1,w1,h1 are knows as actual parameters.

Reading only this blog is not helpful. You have to choose a good book. This blog just provides short notes on the topic.

                 < Happy Coding >

Reference:
OOP by BALAGURUSWAMY

Comments

Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily

C++: Huffman Coding using STL

What is "namespace" and why do we use it?