hello

header ads

Function and Comments Line

Comments Line

Comments are portions of the code ignored by the compiler which allow the user to make simple notes in the relevant areas of the source code. Comments come either in block form or as single lines.

·       Single-line comments :- start with // and continue until the end of the line. If the last character in a comment line is a \ the comment will continue in the next line.

·       Multi-line comments :-  start with /* and end with */

 

Function

            A function in C++ is a group of statements that together perform a specific task. Every C/C++ program has at least one function that the name is main. The main function is called by the operating system by which our code is executed. We can make n number of function in a single program but we can make only one main function in a single program. Every program has only one main function.

The main part of functions is return_type, function_name, parameter and functions body.

Syntax :-

1

2

3

4

Return_type Function_name (Parameters)

{

    // Function Body;

}

 

Steps for function

§  Function prototype

§  Function definition

§  Function call

Function Prototype

The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of a function parameter, it also tells about the return type of the function. By this information, the compiler cross-checks the function signatures before calling it

 

Function prototype

i.                Function type

ii.              Function name

iii.             Function parameter/arguments

iv.             Terminator

Syntax : Type function_name(parameter);

Ex. Int add(int,int);

            Function definition

i.                Function type

ii.              Function name

iii.             Function parameter/arguments

iv.             Variables

v.              Statement

Syntax : Type function_name(parameter variables)

{  

Statements;

}

Ex. Int add(intx,inty)

{  

Statements;

}

 

 

            Function call

i.                Function name

ii.              Function parameter/arguments

iii.             Terminator

function_name(parameters);

add(5,8);

 

Post a Comment

0 Comments