0% found this document useful (0 votes)
18 views20 pages

Programming Fundamentals 13

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
18 views20 pages

Programming Fundamentals 13

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 20

COMPUTER

PROGRAMMING
TOPIC: FUNCTION
C++ FUNCTIONS

• A function is a block of code which only runs when it is called.


• You can pass data, known as parameters, into a function.

• Functions are used to perform certain actions, and they are important for reusing code:
Define the code once, and use it many times.
• A function is a set of statements that are put together to perform a specific task. It can be
statements performing some repeated tasks or statements performing some specialty tasks
like printing etc.
WHY DO WE NEED FUNCTIONS?

1. Functions help us in reducing code redundancy. If functionality is performed at


multiple places in software, then rather than writing the same code, again and again, we
create a function and call it everywhere. This also helps in maintenance as we have to
change at one place if we make future changes to the functionality.
2. Functions make code modular. Consider a big file having many lines of code. It
becomes really simple to read and use the code if the code is divided into functions.
3. Functions provide abstraction. For example, we can use library functions without
worrying about their internal work.
TYPES OF FUNCTIONS IN C++
BUILT-IN FUNCTIONS

• Built-in functions are also called library functions. These are the functions that are
provided by C++ and we need not write them ourselves. We can directly use these
functions in our code.

• These functions are placed in the header files of C++. For Example, <cmath>, <string>
are the headers that have in-built math functions and string functions respectively.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << max(5, 10);
cout << sqrt(64) << "\n";
cout << round(2.6) << "\n";
cout << log(2) << "\n";
return 0;
}
USER-DEFINED FUNCTIONS

• C++ also allows its users to define their own functions. These are the user-defined
functions. We can define the functions anywhere in the program and then call these
functions from any part of the code. Just like variables, it should be declared before
using, functions also need to be declared before they are called.
• myFunction() is the name of the function
• void means that the function does not have a return value
FUNCTION DECLARATION AND DEFINITION

• A C++ function consist of two parts:

• Declaration: the return type, the name of the function, and parameters (if any)
• Definition: the body of the function (code to be executed)

void myFunction() { // declaration


// the body of the function (definition)
}
FUNCTION DECLARATION

• A function declaration tells the compiler about the return type of function, the number of
parameters used by the function and its data types. Including the names of the parameters
in the function, the declaration is optional. The function declaration is also called as a
function prototype.

• We have given some examples of the function declaration below for your reference.

• int sum(int, int);


FUNCTION PARAMETERS OR ARGUMENTS

• Information can be passed to functions as a parameter. Parameters act as variables inside


the function.
• Parameters are specified after the function name, inside the parentheses. You can add as
many parameters as you want, just separate them with a comma:
EXAMPLE
RETURN VALUES

• The void keyword, used in the previous examples, indicates that the function should not
return a value.
• If you want the function to return a value, you can use a data type (such as int, string,
etc.) and use the return keyword inside the function:
EXAMPLE
FUNCTION DEFINITION

• A function definition contains everything that a function declaration contains and additionally it also
contains the body of the function enclosed in braces ({}).
• In addition, it should also have named parameters. When the function is called, control of the program
passes to the function definition so that the function code can be executed. When execution of the
function is finished, the control passes back to the point where the function was called.
• For the above declaration of swap function, the definition is as given below:
void swap(int a, int b){
b = a + b;
a = b - a;
b = b - a;
}
CALLING A FUNCTION

• When we have a function in our program, then depending on the requirement we need to
call or invoke this function. Only when the function is called or invoked, the function will
execute its set of statements to provide the desired results.

• The function can be called from anywhere in the program. It can be called from the main
function or from any other function if the program is using more than one function. The
function that calls another function is called the “Calling function”.
RETURN VALUES

• Once the function performs its intended task, it should return the result to the calling
function. For this, we need the return type of the function. The function can return a
single value to the calling function. The return type of the function is declared along with
the function prototype.
• Code Explanation:
1. Include the iostream header file in our program to use its functions.
2. Include the std namespace in our code to use its classes without calling it.
3. Declare a function named addFunc() that takes two integer parameters. This creates the function prototype.
4. Call the main() function. The program logic should be added within the body of this function.
5. Declare three integer variables, x, y, and sum.
6. Print some text on the console. The text asks the user to enter two numbers.
7. Read the user input from the keyboard. The user should enter two numbers for variables x and y, separated by
space.
8. Call the function addFunc() and passing to it the parameters x and y. The function will operate on these
parameters and assign the output to the variable sum.
9. Print the values of variables x, y, and sum on the console alongside other text.
10. The function must return value upon successful completion.
11. End of the body of the main() function.
12. Function definition. We are defining the function addFunc(). We will state what the function will do within its
body, the { }.
13. Declaring an integer variable named addFunc.
14. Adding the values of parameters num1 and num2 and assigning the result to variable addFunc.
15. The addFunc() function should return the value of addFunc variable.
16. End of the function body, that is, function definition.

You might also like