Beruflich Dokumente
Kultur Dokumente
C++ Functions
11/10/2019
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
by
SHEHZAD LATIF
Assistant Professor,
Hajvery University – Lahore
Email: Shehzadch49@yahoo.Com
1
C++ Functions
11/10/2019
specific task.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
Depending on whether a function is predefined or created by programmer;
1. Library Function
2. User-defined Function
2
1. Library Function
• Library functions are the built-in function in C++ programming.
11/10/2019
• Programmer can use library function by invoking function directly; they don't
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
need to write it themselves.
3
Example 1: Library Function
#include <iostream>
#include <cmath> //header file must be added
11/10/2019
using namespace std;
int main()
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
{
double number, squareRoot;
cout << "Enter a number: ";
cin >> number;
• Enter a number: 26
• Square root of 26 = 5.09902
Programming Fundamentals by
11/10/2019
5
11/10/2019
root of a number.
• Notice code #include <cmath> in the above program. Here, cmath is a header
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
file. The function definition of sqrt()(body of that function) is present in the
• You can use all functions defined in cmath when you include the content of
• Every valid C++ program has at least one function, that is, main() function.
6
2. User-defined Function
• C++ allows programmer to define their own function.
11/10/2019
• A user-defined function groups code to perform a specific task and that group
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
of code is given a name(identifier).
• When the function is invoked from any part of program, it all executes the
7
Programming?
How user-defined function works in C
Programming Fundamentals by
11/10/2019
8
11/10/2019
• When a program begins running, the system calls the main() function, that is,
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• When control of the program reaches to function_name() inside main(), it
executed.
• Then, control of the program moves back to the main function where the code
11/10/2019
// Function prototype {
(declaration) int add;
int add(int, int); add = a + b;
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
// Return statement
int main() return add;
{ }
int num1, num2, sum;
cout<<"Enters two
numbers to add: ";
cin >> num1 >> num2;
// Function call
sum = add(num1, num2);
cout << "Sum = " << sum;
10
return 0;
}
Sum = 4
Output
Enters two integers: 8 -4
Programming Fundamentals by
11/10/2019
11
11/10/2019
In C++, function prototype is a declaration of function without its body to give
compiler information about user-defined function. Function prototype in the above
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
example is:
int add(int, int);
You can see that, there is no body of function in prototype. Also, there are only
return type of arguments but no arguments. You can also declare function
prototype as below but it's not necessary to write arguments.
11/10/2019
invoked(called).
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• In the above program, add(num1,num2); inside main() function calls the user-
defined function.
13
Function Definition
• The function itself is referred as function definition. Function definition in the
above program is:
11/10/2019
// Function definition
int add(int a,int b)
{
int add;
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
add = a + b;
return add;
}
• When the function is called, control is transferred to the first statement of the
function body.
11/10/2019
function (function definition) while calling it.
In the above example, two variables, num1 and num2 are passed to function
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
during function call. These arguments are known as actual arguments.
The value of num1 and num2 are initialized to variables a and b respectively.
11/10/2019
• The type of first actual argument should match the type of first formal
argument. Similarly, type of second actual argument should match the type of
second formal argument and so on.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• You may call function a without passing any argument. The number(s) of
argument passed to a function depends on how programmer want to solve the
problem.
• You may assign default values to the argument. These arguments are known
as default arguments.
• In the above program, both arguments are of int type. But it's not necessary to
have both arguments of same type. 17
Return Statement
• A function can return a single value to the calling program using return
11/10/2019
statement.
• In the above program, the value of add is returned from user-defined function
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
to the calling program using statement below:
return add;
18
Explanation
• The figure below demonstrates the working of return
statement.
11/10/2019
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
19
• In the above program, the value of add inside user-defined function is returned
to the calling function. The value is then stored to a variable sum.
11/10/2019
• Notice that the variable returned, i.e., add is of type int and sum is also of int
type.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• Also, notice that the return type of a function is defined in function declarator
int add(int a, int b). The int before add(int a, int b) means the function should
return a value of type int.