0% found this document useful (0 votes)
3 views19 pages

Programming - Lec. 3 (Functions in C++)

The document is a lecture note from the University of Al-Nahrain's College of Engineering focusing on functions in C++. It covers the definition, types, local and global variables, return statements, and recursive functions, along with examples and homework assignments for students to practice. The aim is to help students learn how to use functions effectively in C++ programming.

Uploaded by

Zaid Mustafa
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)
3 views19 pages

Programming - Lec. 3 (Functions in C++)

The document is a lecture note from the University of Al-Nahrain's College of Engineering focusing on functions in C++. It covers the definition, types, local and global variables, return statements, and recursive functions, along with examples and homework assignments for students to practice. The aim is to help students learn how to use functions effectively in C++ programming.

Uploaded by

Zaid Mustafa
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/ 19

‫جامعة النهرين‪/‬كلية الهندسة‬

‫قسم هندسة الطب الحياتي‬

‫‪Functions in‬‬
‫‪C++‬‬

‫‪Lec-3‬‬

‫‪1st year‬‬
‫‪2nd Semester‬‬

‫‪1‬‬
Objectives

• Learn the student the use of different function which used in C++.

• Write several C++ program which use of function.


Functions
Functions

The function is a set of statements designed to accomplish a particular task. Experience has
shown that the best way to develop and maintain a large program is to construct it from smaller
pieces or (modules). Modules in C++ are called functions.

The general format of the function definition is:

return-type function-name ( parameters-list )


{
(body of function)
statement1 ;
statement2 ;
:
statement-n ;
(return something)
}
Functions

The type of the function may be int, float, char, etc. It may be declared as type (void), which
informs the compiler not to the calling program. For example:

• Void function_name (---)


• Int function_name (---)

Any variable declared in the body of a function is said to be local to that function. Other variables
which are not declared either as arguments or in the function body are considered “global” to the
function and must be defined externally. For example
Functions
Return Statement
Return Statement

The keyword return is used to terminate function and return a value to its caller. The
return statement may also be used to exit a function without returning a value. The
return statement may or may not include an expression. Its general syntax is:

Return;
Return (expression);
Local and Global
variables
Local and Global variables

1. Local variables: Identifiers, variables and functions in a block are said to


belong to a particular block or function and these identifiers are known as the
local parameters or variables. Local variables are defined inside a function block
or a compound statement. For example,

Void func (int i, int j)


{
Int k,m; // local variables

…… // body of the function

}
Local and Global variables

2. Global variables: these are variables defined outside the main function
block. These variables are referred by the same data type and by the same name
through out the program in both the calling portion of the program and in the
function block.
Int x;
Int y=5; // x and y are global variables
void main( ) {
X=10;
Void sum(void);
Sum();
}
Void sum(void) {
Int sum;
Sum=x+y;
}
Recursive Functions
Recursive Functions
A function which calls itself directly or indirectly again and again is known as the recursive
function. Recursive functions are very useful while constructing the data structures like linked
lists, double linked lists, and trees. There is a distinct difference between normal and
recursive functions. A normal function will be invoked by the main function whenever the
function name is used, where as the recursive function will be invoked by itself directly or
indirectly as long as the given condition is satisfied. For example:

Void main(void){
Void func1(); //function declaration
_____
_____
Func1(); //function calling
}
Void func1() //function definition
{
____
____
Func1(); //function calls recursively
}
Procedure
Procedure
• Example 1: Write C++ program to calculate the squared value of a number passed from main
function. Use this function in a program to calculate the squares of numbers from 1 to 10.
Procedure
• Example 2: Write C++ program using function to calculate the average of two numbers entered
by the user in the main program.
Procedure
• Example 3: Write a function to find the largest integer among three integers entered by the user in
the main function.
Homework and
Assignment
Homework and Assignment

1. Write a C++ program, using function, to counts uppercase letter in a 20 letters entered by
the user in the main program.

2. Write a C++ program, using function, that reads two integers (feet and inches) representing distance,
then converts this distance to meter.

Note: 1 foot = 12 inch , 1 inch = 2.54 Cm

3. Write a C++ program, using function, to inputs a student’s average and returns 4 if student’s average
is 90-100, 3 if the average is 80-89, 2 if the average is 70-79, 1 if the average is 60-69, and 0 if the
average is lower than 60.

You might also like