0% found this document useful (0 votes)
11 views21 pages

Fun Cation

The document provides an overview of functions in C++, emphasizing their role in breaking down complex programs into manageable modules. It covers function definitions, types (built-in and user-defined), and the importance of using functions for code reusability and maintenance. Additionally, it explains function parameters, including value and reference parameters, and provides examples for better understanding.

Uploaded by

moathjubouri
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)
11 views21 pages

Fun Cation

The document provides an overview of functions in C++, emphasizing their role in breaking down complex programs into manageable modules. It covers function definitions, types (built-in and user-defined), and the importance of using functions for code reusability and maintenance. Additionally, it explains function parameters, including value and reference parameters, and provides examples for better understanding.

Uploaded by

moathjubouri
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/ 21

‫‪Programming C++‬‬

‫م‪.‬د‪ .‬حسين أحمد علي الويس‬


‫ﺟﺎمﻌﺔ كركوك‬

‫كليﺔ علوم الحﺎسبﺎت وتكنلوﺟيﺎ المﻌلومﺎت‬

‫قسم تكنلوﺟيﺎ المﻌلومﺎت‬

‫‪91‬‬
Functions
• Most computer programs that solve real-world problems include hundreds and even
thousands of lines.

• Experience has shown that the best way to develop and maintain a large program is
to construct it from smaller pieces or modules, each of which is more manageable
than the original program.

• This technique is called divide and conquer.

• A Function : is a self-contained block of statements that perform a specific


task.
• using a function is something like hiring a person to do a specific job for
you.

92
In Real Life
In Programming

Task 4
Task 1
Function ( )
Function ( )

Task 3
Task 2
Function ( ) Function ( )
93
Why to use Functions ?
• Functions allows to write more manageable units of code.
• Writing functions avoids rewriting the same code over and over.
• Easy maintenance for programs.

Important Tips

1- Don’t try to cram the entire logic in one function. It is a very bad style of
programming.

2- Instead, break a program into small units and write functions for each of these
isolated subdivisions.

3- Don’t hesitate to write functions that are called only once.

94
Calculate and print out The
sum and the Average of 3
student marks.

Task 4
Task 1
Print_out ( )
Get_Marks( )

Task 3
Task 2
Calc_sum( ) Calc_Average ( )
95
Functions
Function Definition

return-value-type function-name( parameter-list )


{
Lines of code to be return-value-type:
executed Is the data type for the
… returned value from the
… function to the calling
… program. It is Mandatory, if
(Body) no returned value expected
Parameter-List : Is a list of
} , we use keyword Void.
data values supplied from
the calling program as
input to the function. It is
Optional

96
Function Types
1. Built in Functions (Ready Made).
• For Example : Math Library Functions <cmath> Like

97
Built in Functions
To correctly use the built in functions we must know well its header (prortotype).

return-value-type function-name( parameter-list )

int abs (int number) ;

double pow (double base, double exponent) ;

Double floor (double number);

Double sqrt (double number);

98
#include <iostream>
#include <cmath>
using namespace std;
int main ( )
{
int i = -5 ;
double x = 5.0;
double y = 2.0;
double z = 5.21;

cout << "The absolute value of i is " << abs (i) << "\n \n";

cout << "The power of x to the power y is " << pow (x,y) << "\n \n";

cout << "The Floor for Z is " << floor (z) <<"\n \n";

cout << "The Ceil for Z is " << ceil (z) <<"\n \n";
}
99
Function Types
2. User Defined Functions ( Tailored by Programmers).
Working with user defined functions is done in three steps :

1- Function declaration (Prototype , in C++ written before Main ( ) ).

2- Function Definition (Body, in C++ written After Main ( ) ).

3- Invoking Function (Calling, From The Main ( ) ).

100
Building Functions
1- Function Declaration (Written before The main( ) )

return-value-type function-name( Formal parameter-list )

2- Function Definition (Written after The main( ) )


Formal Parameter-List Actual
: Parameter-List : Is a
return-value-type
Is a list of input list of Formal
parameters
function-name( input parameter-list
parameters )
{ with their data types . without their data types .
Lines Int
of code to be( int x );Cout << abs ( i );
abs
executed
}

3- Invoking Function ( From The main( ) )

Function name ( Actual Parameter- List );

101
User-Defined Functions
• Value-returning functions: have a return type
– Return a value of a specific data type using the return statement
– the returned value can be used in one of the following scenarios:
• Save the value for further calculation
Int result;
Result = sum ( x , y );

• Use the value in some calculation


Result = sum(x , y) /2;

• Print the value


Cout << sum(x , y);

• Void functions: do not have a return type


– Do not use a return statement to return a value
Flow of Execution
• Execution always begins at the first statement in the function main

• Other functions are executed only when they are called

• Function prototypes appear before any function definition


– The compiler translates these first

• The compiler can then correctly translate a function call


Flow of Execution (cont'd.)
• A function call results in transfer of control to the first statement in the
body of the called function

• After the last statement of a function is executed, control is passed back


to the point immediately following the function call

• A value-returning function returns a value


– After executing the function the returned value replaces the function
call statement
• For Example : write a program that ask the user to Enter 2 integer numbers
and print out the larger of them.
int larger (int num1 , int num2);
int main ()
{
int n1 , n2 , result;
cout <<"Please Enter 2 integer numbers \n";
cin >>n1 >> n2;
result = larger(n1,n2);
cout <<" The Larger number is " << result<<"\n";
}

int larger (int num1 , int num2)


{
int max;
if (num1 >= num2)
max = num1;
else
max = num2;
return max;
}
105
For Example: Write a program to calculate the Area and volume for a sphere.

– The area of sphere = 4 * PI * Radius * Radius .


– The Volume of sphere = 4/3 * PI * Radius * Radius * Radius .
– Note : PI = 3.14

106
For Example : write a program that ask the user to Enter 3 integer numbers
and print out their sum and Average.

int sum (int num1 , int num2, int num3);


float average (int num1,int num2, int num3 );
int main ()
{
int n1 , n2 , n3 ;
cout <<"Please Enter 3 integer numbers \n";
cin >>n1 >> n2 >> n3;
cout <<" The sum of the 3 number is " << sum (n1, n2,n3) <<"\n";
cout <<" The average of the 3 number is " << average (n1, n2,n3) <<"\n";
}
int sum (int num1 , int num2, int num3)
{
return num1+num2+num3;
}

float average (int num1, int num2, int num3 )


{
return sum (num1 , num2 , num3)/3;
} 107
Function Parameter’s Default Value
int sum (int num1 , int num2, int num3 = 90);

int main( )
{
int n1 , n2 ;
cout <<"Please Enter 3 integer numbers \n";
cin >>n1 >> n2 ;
cout <<" The sum of the 3 number is " << sum (n1, n2) <<"\n";
}
int sum (int num1 , int num2, int num3 = 90)
{
return num1+ num2+ num3;
}

108
Function Parameters Types
• Value parameter: Reference parameter:

– The value parameter has its own copy of the data

– During program execution, The value parameter manipulates the data


stored in its own memory space. So , the actual parameter not Affected
with any change.

void swap( int x, int y )


{ y
x
int temp;
10 20
temp= x;
x = y; Temp
x = temp;
}
109
Function Parameters Types
• Reference parameter:

– A reference parameter stores the address of the corresponding actual parameter

– During program execution to manipulate data, The address stored in the


reference parameter directs it to the memory space of the corresponding actual
parameter

• Reference parameters are useful in three situations:

– Returning more than one value

– Changing the actual parameter

– When passing the address would save memory space and time

110
Function Parameters Types

void swap( int &x, int &y )


{
int temp;
temp= x;
x = y;
x = temp;
}

111

You might also like