0% found this document useful (0 votes)
7 views35 pages

PF Lecture 13 Functions Unit 1

The document is a lecture on functions in C++ programming, emphasizing the importance of breaking down complex problems into manageable parts through functions. It discusses the advantages of using functions, including improved readability, code reuse, and the ability to work on different parts of a program simultaneously. The lecture also covers predefined and user-defined functions, including value-returning and void functions, along with examples of their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views35 pages

PF Lecture 13 Functions Unit 1

The document is a lecture on functions in C++ programming, emphasizing the importance of breaking down complex problems into manageable parts through functions. It discusses the advantages of using functions, including improved readability, code reuse, and the ability to work on different parts of a program simultaneously. The lecture also covers predefined and user-defined functions, including value-returning and void functions, along with examples of their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

CS 111 – Programming Fundamentals

Sameer Akram Mirza

CS 111 – Programming Fundamentals

Lecture 13
Functions Unit 1

Sameer Akram

PF – Lecture 13
Functions Unit 1
Sameer Akram Mirza
Functions
 Previously, we learned that a C++ program is a collection of
functions.
 One such function is main().
 The programs in previous lectures, we used only the function
main(); the programming instructions are packed into one
function.
 This technique, however, is good only for short programs.
 For large programs, it is not practical (although it is possible)
to put the entire programming instructions into one function.
 We must learn to break the problem into manageable pieces.
 Coming lectures discuss the functions previously defined and
then discusses user-defined functions.
Functions Explanation
 Let us imagine an automobile factory.
 When an automobile is manufactured, it is not made from
basic raw materials; it is put together from previously
manufactured parts. Some parts are made by the company
itself; others, by different companies.
 Functions are like building blocks.
 They let you divide complicated programs into manageable
pieces. They have other advantages, too:
 While working on one function, you can focus on just that part
of the program and construct it, debug it, and perfect it.
 Different people can work on different functions
simultaneously.
Functions Explanation (Contd.)

 If a function is needed in more than one places in a program


or in different programs, you can write it once and use it many
times.
 Using functions greatly enhances the program’s readability
because it reduces the complexity of the function main.
Introduction to Functions
 A complex problem is often easier to solve by dividing it into
several smaller parts, each of which can be solved by itself.

 This is called structured programming.


 These parts are sometimes made into functions in C++.

 main() then uses these functions to solve the original


problem.
Advantages of Functions
 Functions separate the concept (what is done) from the
implementation (how it is done).
 Functions make programs easier to understand.
 Functions can be called several times in the same program,
allowing the code to be reused.
 C++ allows the use of both internal (user-defined) and
external functions.
 External functions (e.g. abs(), ceil(), rand(),
sqrt(), etc.) are usually grouped into specialized
libraries (e.g., iostream, stdlib, math, etc.)
Functions

Functions

User-defined Functions
Predefined Functions

Value returning functions void Functions


Various parts of function larger()
Predefined Functions
Predefined Functions (Contd.)
Example L13E1: Standard C++ Math Functions
#include <iostream> Sample value x Ceiling Floor
#include <cmath>
using namespace std; 12 / 5 = 2.4 3 2

void main()
{
double x;

cout << "Please enter a real number: “ << endl;


cin >> x;

cout << “The ceil ("<< x <<") = " << ceil(x) << endl;
cout << “The floor ("<< x <<") = " << floor(x) << endl;

system (“pause”);
}

11
Example L13E2: Standard C++ Character Functions *
#include <iostream>
#include <cctype> //character type functions
using namespace std;
void main()
{
char ch;
cout << "Enter a character: ";
cin >> ch;

cout << "The toupper(" << ch << ") = "


<< static_cast<char>( toupper(ch) ) << endl;

cout << "The tolower(" << ch << ") = "


<< (char) tolower(ch) << endl;

12
Example L13E2: Standard C++ Character Functions *

if ( isdigit(ch) )
cout << "'" << ch <<"' is a digit!\n";
else
cout << "'" << ch <<"' is NOT a digit!\n";
}

13
Example 6-1: How to use predefined functions *
#include <iostream>
#include <cmath>
#include <cctype>
using namespace std;

int main()
{
int x;
double u, v;
cout << "Line 1: Uppercase a is “ //Line 1
<< static_cast<char>(toupper('a')) << endl;

u = 4.2;
v = 3.0;
cout << "Line 4: " << u << " to the power of "
<< v << " = " << pow(u, v) << endl; //Line 4
Example 6-1: How to use predefined functions (Contd.)
cout << "Line 5: 5.0 to the power of 4 = "
<< pow(5.0, 4) << endl; //Line 5

u = u + pow(3.0, 3); //Line 6

cout << "Line 7: u = " << u << endl; //Line 7

x = -15; //Line 8
cout << "Line 9: Absolute value of " << x
<< " = " << abs(x) << endl; //Line 9

system("pause");
return 0;
}
Example 6-1: Output ...
User-Defined Functions

 Although C++ is shipped with a lot of standard functions,


these functions are not enough for all users, therefore, C++
provides its users with a way to define their own functions (or
user-defined function)

 User-defined functions in C++ are classified into two


categories:
 Value-returning functions
 Void functions
User-Defined Functions (Contd.)

 Value-returning functions — functions that have a return


type. These functions return a value of a specific data type
using the return statement, which we will explain shortly.

 Void functions — functions that do not have a return type.


These functions do not use a return statement to return a
value.

18
Value-Returning Functions
 The previous section introduced some predefined C++
functions such as pow(), abs(), islower(), and
toupper().
 These are examples of value-returning functions
 To use these functions in your programs, you must know the
name of the header file that contains the functions’
specification.

 You need to include this header file in your program using the
include statement and know the depicted items:
Items needed to call any function in main()

1. The name of the function


2. The number of parameters, if any
3. The data type of each parameter
4. The data type of the value computed (that is, the value
returned) by the function, called the type of the function.
Also called the return type of the function.
Statements enclosed between curly braces form the body of
the function.
Value-Returning Functions (Contd.)
 Because the value returned by a value-returning function is
unique, the natural thing for you to do is to use the value in
one of three ways
1. Save the value for further calculation.
2. Use the value in some calculation.
3. Print the value.
 This suggests that a value-returning function is used:
1. In an assignment statement.
2. As a parameter in a function call.
3. In an output statement.
 That is, a value-returning function is used (called) in an
expression.
Value-Returning Functions – Parameters

 Formal Parameter:
A variable declared in the function heading while defining the
function.

 Actual Parameter:
A variable or expression listed in a call to a function.
Actual parameter is placed during the function call.
Value-Returning Functions – Parameters (Contd.)
Various parts of function larger()
Various calls to function larger()
 Suppose that num, num1, and num2 are double variables.
Also suppose that num1 = 45.75 and num2 = 35.50.
Figure 6-2 shows various calls to the function larger.
Function Prototype
 The function heading without the body of the function.
Program: FunctionAvgOf3Num
#include <iostream>
using namespace std;

int average ( int, int, int ); //function prototype

int main ( )
{
int var1, var2, var3;

cout << "\n Enter three integers: ";


cin >> var1 >> var2 >> var3;

cout << "\n The average of " << var1 << ", " << var2
<< ", and " << var3 << " is = "
<< average ( var1, var2, var3 ); //function call
Program: FunctionAvgOf3Num (Contd.)
cout << endl;
system ( "pause" );
return 0;
}

//function definition
int average ( int x, int y, int z )
{
return ( x + y + z ) / 3;
}

int average ( int x, int y, int z )


{
int temp;
temp = ( x + y + z ) / 3;
return temp;
}
Program: Largest of three numbers
Program: Largest of three numbers (Contd.)
Simple Run
Value-Returning Functions: Some Peculiarity
 A value-returning function must return a value. Consider the
following function, secret, that takes as a parameter an int
value. If the value of the parameter, x, is greater than 5, it
returns twice the value of x; otherwise, the value of x remains
unchanged.

int secret(int x) int secret(int x)


{ {
if (x > 5) if (x > 5)
return 2 * x; return 2 * x;
} else
return x;
}
Value-Returning Functions: Some Peculiarity (Contd.)
 Here, if the value of x is less than or equal to 5, the return
statement in Line 3 executes, which returns the value of x. On
the other hand, if the value of x is, say 10, the return
statement in Line 2 executes, which returns the value 20 and
also terminates the function.

int secret(int x) int secret(int x)


{ {
if (x > 5) if (x > 5) //Line 1
return 2 * x; return 2 * x;//Line 2
else
return x; retrun x; //Line 3
} }
Value-Returning Functions: Some Peculiarity (Contd.)
Recall that in a value-returning function, the return statement
returns the value.
Consider the following return statement:

return x, y; //only the value of y will be returned

This is a legal return statement. You might think that this


return statement is returning the values of x and y. However,
this is not the case.
Remember, a return statement returns only one value, even if
the return statement contains more than one expression. If
a return statement contains more than one expression,
only the value of the last expression is returned. Therefore, in
the case of the above return statement, the value of y is
returned.
That’s end of the presentation!

35

You might also like