0% found this document useful (0 votes)
14 views

LP3 - Unit 4 - C++ Functions

The document discusses C++ functions, defining that a function is a block of code that performs a specific task, and outlines the key components of functions including the return type, function name, parameters, and body. It also explains different types of functions like void functions, main functions, and recursive functions, providing examples to illustrate function syntax and usage.

Uploaded by

Ariel Verzosa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

LP3 - Unit 4 - C++ Functions

The document discusses C++ functions, defining that a function is a block of code that performs a specific task, and outlines the key components of functions including the return type, function name, parameters, and body. It also explains different types of functions like void functions, main functions, and recursive functions, providing examples to illustrate function syntax and usage.

Uploaded by

Ariel Verzosa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

3 | Computer Programming 1

UNIT 4

C++ Functions

4.0 Intended Learning Outcomes


On completion of the module, you should be able to:

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.

4.2 C++ Function

A function is an assignment or a task that must be performed to complement


the other part(s) of a program. There are two kinds of functions: those supplied to you
and those you will be writing. The functions that are supplied to you are usually in
three categories: those built-in the operating system, those written in C++ (they are
part of the C++ language), and those supplied with your programming environment.
The use of these functions is the same regardless of the means you get them; you
should know what a function looks like, how to create one, what functions are already
available, where they are located, and what a particular function does, how and when
to use it.

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

A function is known with various names like a method or a sub-routine or a


procedure etc.

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

return_type function_name(parameter list )


{
body of the function
}

A C++ function definition consists of a function header and a function body.

4.2.1 Types of C++ Functions

Here are all the parts of a function:

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.

3. 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.

4. Function Body: The function body contains a collection of statements that


define what the function does.

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:

// function returning the max between two numbers


int max(int num1, int num2)
{
//local variable declaration int result;
if (num1 > num2)
3 | Computer Programming 1

result = num1;
else
result = num2;
return result;
}

4.2.2 Function in C++

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

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

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

Recursion is a programming technique in which a function calls itself for a number of


times until a particular condition is satisfied. It’s a very important technique to
understand and once understood many long listing of code can be reduced to a few
number of lines.

Recursion basically a word mostly used in mathematics to state a new terms in


previous term such as :
3 | Computer Programming 1

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.

1. The problem must be expressed in recursive manner.


2. There must be a condition which stops the recursion otherwise there would be
a stack overflow.

#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

Hello from main1


Hello from main2
Hello from main3
Hello from main4
Hello from main5
Hello from main6
Hello from main7

EXPLANATION: A static variable persists even when function execution comes to an


end and its default initial value is 0. When main ( ) executes for the first time value of
t is 0. If condition falls and count after if executes. When control reaches at main ( ) the
3 | Computer Programming 1

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

Malini Devi J. (2014). C++ Programming Language for Beginner.


Chapter 1 pp. 1-4

Hari Mohan Pandey (2015). Object-Oriented Programming C++ Simplified;


Computer Engineering Department NMIMS University Mumbai; An ISO 9001:2008
Certified Company; ISBN 978-93-81159-50-7

C++ Programming Language


https://www.tutorialspoint.com/cplusplus/index.htm
https://www.w3schools.com/cpp/
https://cplusplus.com/doc/tutorial/

4.4 Acknowledgment
The syntax, table or images, and information contained in this module were
taken from the references cited above.

You might also like