0% found this document useful (0 votes)
5 views10 pages

Lecture 11

Uploaded by

h7ussin3
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)
5 views10 pages

Lecture 11

Uploaded by

h7ussin3
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/ 10

‫جامعة بغداد‪ /‬كلية التربية للعلوم الصرفة ابن الهيثم‪/‬قسم علوم الحاسبات‬

‫المرحلة االولى‪ /‬نظري‬


‫السنة الدراسية ‪2024-2023‬‬

‫‪Structured Programming‬‬
‫‪Lecture 11– Functions‬‬
Functions
• A 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. A function definition has a name,


parentheses pair containing zero or more parameters and a body.

• For each parameter, there should be a corresponding is taken to be an


integer by default. The general form of the function definition is:
• 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.
• Any variable declared in the body of 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
• void square ( int a, int b ) \\ a,b are the formal parameters
• float output ( void ) \\ function without formal parameters
• The keyword return is used to terminate function and return a value
to its caller. The return statement may or may not include an
expression. Its general syntax is:
• return;
• return ( expression ) ;
• The return statements terminate the execution of the function and
pass the contral back to the calling environment.
Function Definition
• Definition before main function
• The functions are defined before the first appearance of calls to them
in the source code. These calls were generally in function main which
we have always left at the end of the source code.
• Example 1 : Write a C++ program using function to print any text.
Example 2 : Write a C++ program to read two numbers and return the
maximum number between them using function.
Example 3 : Write a C++ program to read two numbers and return the
summation of these numbers using function.
Note:

At the point at which the function is called from within main, the control is lost by
main and passed to function sum. The value of both arguments passed in the call
(99 and 10) are copied to the local variables int a and int b within the function.
Function sum declares another local variable (int r), and by means of the
expression r=a+b, it assigns to r the result of a plus b. Because the actual
parameters passed for a and b are 99 and 10 respectively, the result is 109. The
following line of code: return (r);
Definition after main function (prototype)
• There is an alternative way to avoid writing the whole code of a
function before it can be used in main function. This can be achieved
by declaring just a prototype of the function before it is used, instead
of the entire definition.
• This declaration is shorter than the entire definition, but significant
enough for the compiler to determine its return type and the types of
its parameters.
• type name (type1 argument1, type2 argument2, ...);
• It is identical to a function definition, except that it does not include
the body of the function itself (i.e., the function statements that in
normal definitions are enclosed in braces { }) and instead of that we
end the prototype declaration with a semicolon (;).
Example 4 : Write a C++ program using function prototype to calculate
the average of two numbers entered by the user in the main program.

You might also like