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

C++ Functions

Uploaded by

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

C++ Functions

Uploaded by

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

C++ Functions

The function in C++ language is also known as procedure or subroutine in other programming
languages.
To perform any task, we can create function. A function can be called many times. It provides
modularity and code reusability.
Advantage of functions

- Code Reusability
By creating functions in C++, you can call it many times. So we don't need to write the
same code again and again.

- Code optimization
It makes the code optimized, we don't need to write much code.
Suppose, you have to check 3 numbers (531, 883 and 781) whether it is prime number
or not. Without using function, you need to write the prime number logic 3 times. So,
there is repetition of code.
But if you use functions, you need to write the logic only once and you can reuse it
several times.

Types of Functions
There are two types of functions in C programming:

- Library Functions
These are the functions which are declared in the C++ header files such as ceil(x),
cos(x), exp(x), etc.

- User-defined functions
These are the functions which are created by the C++ programmer, so that he/she can
use it many times. It reduces complexity of a big program and optimizes the code.
Examples, greeting(), summation(int a, int b) and other that you will create.

Page 1 of 5
Parts of a function

✓ 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 general form of a C++ function is as follows −


return_type function_name( parameters or arguments) {
// body of the function
}
A C++ function definition consists of a function header and a function body. Here are all the
parts of a function −

• 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.
• Function Name − This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
• Parameters or Arguments − A parameter or argument is like a placeholder. When a
function is invoked/called, 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.

Page 2 of 5
• Function Body − The function body contains a collection of statements that define
what the function does

Function Declarations
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 the above defined function max(), following is the function declaration −
int max(int num1, int num2);
Parameter names are not important in function declaration only their type is required, so
following is also valid declaration −
int max(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.

Example 1
A program to create a user defined function called greeting function that print a “Hello
there” when called in the main function.

Page 3 of 5
Observations:

- The function has void return type,


- Calling a function using function name and parentheses ()

Example 2
A program with a function will accepts an integer number and print whether the number is
even or not even.

Observations

- A function isEvenNumber has one argument, int a. i.e isEvenNumber(int a) The


argument means the function accept on integer value and assigned to variable named
a.
- In the main function, the function is called with a value 10 is passed in the parentheses

Page 4 of 5
Example 3
A program with the functions that prints the sum and the difference of the two numbers.

Task to do

1. A calculator program, write a simple calculator program using functions. Implement


functions for addition, subtraction, multiplication, and division. The program should
prompt the user to enter two numbers and choose an operation, then call the
appropriate function and display the result.
2. Define a function named greet that takes a string parameter name. The function
should print "Hello, name!". The program should prompt user to enter that name.
3. Write a function named findMax that takes two integers as parameters and returns
the larger of the two. Write a program that calls this function with the values 8 and 12
and prints the result.

Page 5 of 5

You might also like