LP3 - Unit 4 - C++ Functions
LP3 - Unit 4 - C++ Functions
UNIT 4
C++ Functions
1. Recognize the use of functions and that structured programs are built
using functions.
2. Construct the general format/ syntax of functions in writing C++
programs based on applicable program design.
4.1 Introduction
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 such that each function performs a specific task.
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.
3 | Computer Programming 1
Defining a Function
The general form of a C++ function definition is as follows:
1. 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.
2. Function Name: This is the actual name of the function. The function name
and the parameter list together constitute the function signature.
Example #1:
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:
result = num1;
else
result = num2;
return result;
}
The main function in C++ must return a value. In C++ we can define function
one in two manner as :
int main( )
{
function body;
}
OR
Even you do not define return type of main it does not give any error but compiler
simply flashes a warning. To suppress a warning define return type of main and
return value from it. You can also write void before main but experts community says
that it is a good programming practice to return value from main.
4.2.3 Recursion
Then we can calculate X2 in terms of X1, X 3 in terms of X 2 and so on. When solving
a problem through recursion two conditions must be satisfied.
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
void main( )
{
static int t;
clrscr( );
if(t==7)
{
cout<<“Quit\n “;
exit(0);
}
cout<<endl<<“Hello from main”<<++t;
main( );
}
Output
recursion starts as we are calling main from main. The main starts again for this call
with value of t =1.
Note: static int t; statement is executed only once. cout after if gets executed and main ( ) is
called again, this time with value of t=2. This continues until t does not become 5. When t
becomes 5, if condition satisfies and recursion stops.
If we do not take t as static in the above program our program will be put into
an infinite loop as there will be no condition which stops recursion. As each time main
is called a new t will be initialized with a new value, so there will be no effect of
incrementing t in the cout statement.
3 | Computer Programming 1
4.3 References
4.4 Acknowledgment
The syntax, table or images, and information contained in this module were
taken from the references cited above.