3 - Functions - Part1
3 - Functions - Part1
UNIVERSITY
College of Computer Science & IT
Department of CS
Welcome to
CS 221:Fundamentals of Programming
Weeks (4): Functions – Par 1
Chapters 9 and 10 (Zak Textbook)
Chapter 4 (Savitch Textbook)
Objectives
2
CS221: Fundamentals of Programming
TOP-DOWN DESIGN
Top Down Design
• To write a program
– Develop the algorithm that the program will use
– Translate the algorithm into the programming
language
Slide 4- 4
Top Down Design
Example: Make Movie
Slide 4- 5
Top Down Design
Example: Main Function in program
Slide 4- 6
Benefits of Top Down Design
Slide 4- 7
CS221: Fundamentals of Programming
FUNCTIONS
8 8
Motivation 3
sum = 0;
for (int i = 20; i <= 30; i++) sum += i;
cout << " Sum from 20 to 30 is " << sum;
sum = 0;
for (int i = 35; i <= 45; i++) sum += i;
cout << "Sum from 35 to 45 is " << sum ;
What is Function?
• Functions are like building blocks
• They allow complicated programs to be divided into
manageable pieces
• Some advantages of functions:
– A programmer can focus on just that part of the program
and construct it, debug it, and perfect it.
• Functions
– Called modules .
– Like small programs.
– Can be put together to form a larger program.
يعني الفنكشنز زي الوحدات وكأنها برامج صغيرة نستخدمها
عشان نسوي برامج اكبر
11
Function Syntax: Header and Definition
Or
Prototype
• Syntax:
13
Function Syntax: Function Call
14
Function Syntax (Example)
• Heading:(Function Type, Function Name, Formal Parameters)
– Example: int abs(int number) Heading
15
Functions Type OR Returning Type
Returning
Type
Value
Void
returning-
16
Functions Type OR Returning Type
17
Void Functions
• Void functions and value-returning functions have similar
structures
– Both have a heading part and a statement part
20
Value-Returning Functions
(continued)
• Because the value returned by a value-returning
function is unique, we must:
– Save the value for further calculation
– Use the value in some calculation
– Print the value
21
return Statement
22
Syntax: return Statement
• The return statement has the following syntax:
23
Syntax: return Statement
وﺣﺪة ع اﻻﻗﻞ وﻣﻤﻜﻦ اﻛﺜﺮreturn اﻟﻔﻨﻜﺸﻦ اﻟﻠﻲ ﺗﺴﻮي رﯾﺘﯿﺮن ﯾﻌﻨﻲ اي ﻓﻨﻜﺸﻦ ﻏﯿﺮ اﻟﻔﻮﯾﺪ ﻻزم ﯾﻜﻮن ﻓﯿﮭﺎ
24
Value-Returning Functions (continued)
25
Does the following functions do
the same thing?
26
Types of Functions
Functions
Pre-defined User-defined
27
CS221: Fundamentals of Programming
PRE-DEFINED FUNCTIONS
28
Function Libraries
• Predefined functions are found in libraries
• The library must be “included” in a program
to make the functions available
• An include directive tells the compiler which
library header file to include.
– You cannot use functions without calling its header.
– In case you call function without includes its header, linking
error will appear in compilation time.
iostream cmath cctype cstdlib
cout sqrt tolower rand
cin Pow toupper abs
floor
Slide 4- 29
Includes Predefined Functions
• To include the math library containing sqrt():
#include <cmath>
• Newer standard libraries, such as cmath, also require
the directive
using namespace std;
30
Function Call Syntax
• Function_name (Argument_List)
– Argument_List is a comma separated list:
Slide 4- 31
Predefined Functions (continued)
• pow(x,y) calculates xy
pow and sqrt
– x and y are the parameters (or arguments) return
– Returns a value of type double
– pow(2, 3) = 8.0 double!!!
33
)Predefined Functions (continued
كلهم دبل اال absو randوخواتها و tolowerو toupper
abs(-7.33) = 7
34
Predefined Functions (continued)
35
Predefined Functions (code)
36
Predefined Functions (code)
• Example 6-1 sample run:
37
An Example Program
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cctype>
using namespace std;
Int main()
{
cout<<"abs(3.5): "<<abs(3.5)<<endl;
cout<<"abs(-3.5): "<<abs(-3.5)<<endl;
cout<<"ceil(59.76): "<<ceil(59.76)<<endl;
cout<<"ceil(-59.76): "<<ceil(-59.76)<<endl;
cout<<"exp(1) : "<<exp(1)<<endl;
cout<<"fabs(3.5): "<<fabs(3.5)<<endl;
cout<<"cos(0): "<<cos(0)<<endl;
cout<<"floor(40.8): "<<floor(40.8)<<endl;
cout<<"floor(-40.8): "<<floor(-40.8)<<endl;
cout<<"tolower(65): "<<tolower(65)<<endl;
cout<<"toupper(97) : "<<toupper(97)<<endl;
Return 0;
} 38
39
40
Predefined Functions (example)
• The rand, srand, and time Functions
Positive integer number less than RAND_MAX constant in my compiler
RAND_MAX >= x >= 0
• rand():The function returns an integer that is greater than
or equal to 0 but less than or equal to the value stored in the
RAND_MAX constant, which is one of many constants built into
the C++ language.
• RAND_MAX Constant
Assign it to variable
Cout it
Use it in calculation
42
Predefined Functions (example)
Generate Random value within a Specific range
Range ( 1-6 )
عشان مايعطيني صفر1+
43
Predefined Functions (example)
Generate Random value within a Specific range
44
Predefined Functions (example)
• You should initialize the random number generator in each
program in which it is used. Otherwise, it will generate the
same series of numbers each time the program is executed.
لحالها بيعطيني قيمة ثابته دائماrand ملا تكون
45
Predefined Functions (example)
46
Random Number Generator (code)
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
cout << RAND_MAX;
srand(time(0));
// or srand(static_cast<int>(time(0)));
int no;
for (int j=0; j<5; j++)
{
no=rand();
cout<<endl<<"No genrated: "<<no;
} 47