Download presentation
Presentation is loading. Please wait.
1
Function Basics
2
Function Basics The basic concept of a function is to break down the larger programming project into smaller, more specialized blocks of code. int main() { display_greetings(); gather_data(); calculate(); output(); sign_off(); return 0; }
3
Motivation Functions will help you to organize your code into smaller units or modules that provide a sort of outline for the program. Functions allow the programming of very large projects to be done by teams of programmers. You will occasionally find yourself using a module of code more than once. Some units of code are so useful that you can carry them from project to project.
4
Function Format return_type function_name ( formal_parameter_list ) { local variable declarations statements return value_of_return_type; }
5
return_type function_name ( formal_parameter_list ) { local variable declarations statements return value_of_return_type; } int main ( ) { char quit = ‘y’; do { cout << “Loop again? ”; cin >> quit; } while (quit != ‘n’); return 0; }
6
return_type function_name ( formal_parameter_list ) { local variable declarations statements return value_of_return_type; } int main ( ) { char quit = ‘y’; do { cout << “Loop again? ”; cin >> quit; } while (quit != ‘n’); return 0; }
7
return_type function_name ( formal_parameter_list ) { local variable declarations statements return value_of_return_type; } int main ( ) { char quit = ‘y’; do { cout << “Loop again? ”; cin >> quit; } while (quit != ‘n’); return 0; }
8
return_type function_name ( formal_parameter_list ) { local variable declarations statements return value_of_return_type; } int main ( ) { char quit = ‘y’; do { cout << “Loop again? ”; cin >> quit; } while (quit != ‘n’); return 0; }
9
return_type function_name ( formal_parameter_list ) { local variable declarations statements return value_of_return_type; } int main ( ) { char quit = ‘y’; do { cout << “Loop again? ”; cin >> quit; } while (quit != ‘n’); return 0; }
10
return_type function_name ( formal_parameter_list ) { local variable declarations statements return value_of_return_type; } int main ( ) { char quit = ‘y’; do { cout << “Loop again? ”; cin >> quit; } while (quit != ‘n’); return 0; }
11
return_type function_name ( formal_parameter_list ) { local variable declarations statements return value_of_return_type; } int main ( ) { char quit = ‘y’; do { cout << “Loop again? ”; cin >> quit; } while (quit != ‘n’); return 0; }
12
#include <iostream> using namespace std; void greetings( ); int main( ) { greetings( ); return 0; } void greetings( ) { cout<<"Hello there, welcome to my world."<<endl; return; }
13
#include <iostream> using namespace std; void greetings( ); int main( ) { greetings( ); return 0; } void greetings( ) { cout<<"Hello there, welcome to my world."<<endl; return; }
14
#include <iostream> using namespace std; void greetings( ); int main( ) { greetings( ); return 0; } void greetings( ) { cout << "Hello there, welcome to my world." << endl; return; }
15
#include <iostream> using namespace std; void greetings( ); int main( ) { greetings( ); return 0; } void greetings( ) { cout << "Hello there, welcome to my world." << endl; return; }
16
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
17
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
18
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
19
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
20
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
21
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
22
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
23
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
24
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
25
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
26
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
27
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
28
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
29
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
30
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
31
#include <iostream> using namespace std; void display_number (int a_number); int main() { int bob = 5; display_number (bob); return 0; } void display_number (int a_number) { cout << a_number; return; }
32
#include <iostream> using namespace std; int get_number ( ); int main() { int bob; bob = get_number ( ); return 0; } int get_number() { int value_to_get; cout << "Enter an integer: "; cin >> value_to_get; return value_to_get; }
33
#include <iostream> using namespace std; int get_number ( ); int main() { int bob; bob = get_number ( ); return 0; } int get_number() { int value_to_get; cout << "Enter an integer: "; cin >> value_to_get; return value_to_get; }
34
#include <iostream> using namespace std; int get_number ( ); int main() { int bob; bob = get_number ( ); return 0; } int get_number() { int value_to_get; cout << "Enter an integer: "; cin >> value_to_get; return value_to_get; }
35
#include <iostream> using namespace std; int get_number ( ); int main() { int bob; bob = get_number ( ); return 0; } int get_number() { int value_to_get; cout << "Enter an integer: "; cin >> value_to_get; return value_to_get; }
36
#include <iostream> using namespace std; int get_number ( ); int main() { int bob; bob = get_number ( ); return 0; } int get_number() { int value_to_get; cout << "Enter an integer: "; cin >> value_to_get; return value_to_get; }
37
#include <iostream> using namespace std; int get_number ( ); int main() { int bob; bob = get_number ( ); return 0; } int get_number() { int value_to_get; cout << "Enter an integer: "; cin >> value_to_get; return value_to_get; }
38
#include <iostream> using namespace std; int get_number ( ); int main() { int bob; bob = get_number ( ); return 0; } int get_number() { int value_to_get; cout << "Enter an integer: "; cin >> value_to_get; return value_to_get; }
39
#include <iostream> using namespace std; int get_number ( ); int main() { int bob; bob = get_number ( ); return 0; } int get_number() { int value_to_get; cout << "Enter an integer: "; cin >> value_to_get; return value_to_get; }
40
#include <iostream> using namespace std; int get_number ( ); int main() { int bob; bob = get_number ( ); return 0; } int get_number() { int value_to_get; cout << "Enter an integer: "; cin >> value_to_get; return value_to_get; }
41
#include <iostream> using namespace std; int get_number ( ); int main() { int bob; bob = get_number ( ); return 0; } int get_number() { int value_to_get; cout << "Enter an integer: "; cin >> value_to_get; return value_to_get; }
42
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
43
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
44
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
45
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
46
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
47
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
48
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
49
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
50
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
51
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
52
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
53
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
54
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
55
void greetings(); int get_number (); float average (const float val1, const float val2); int main() { float bob, sue, the_average; greetings(); bob = get_number(); sue = get_number(); the_average = average (bob, sue); return 0; } float average (const float val1, const float val2) { float sum, ave; sum = val1 + val2; ave = sum / 2; return ave; }
56
float average (const float val1, const float val2); int main() { float the_average; the_average = average (45, 3.1); return 0; } float average (const float val1, const float val2) { return (val1 + val2) / 2; }
57
float average (const float val1, const float val2); int main() { float the_average; the_average = average (45, 3.1); return 0; } float average (const float val1, const float val2) { return (val1 + val2) / 2; }
58
float average (const float val1, const float val2); int main() { float the_average; the_average = average (45, 3.1); return 0; } float average (const float val1, const float val2) { return (val1 + val2) / 2; }
59
#include <iostream> using namespace std; const float PI = 3
#include <iostream> using namespace std; const float PI = ; float cyl_vol (const float, const float); int main() { cout << cyl_vol(3,5); cout << radius; return 0; } float cyl_vol (const float radius, const float height) { return PI*radius*radius*height; }
60
#include <iostream> using namespace std; const float PI = 3
#include <iostream> using namespace std; const float PI = ; float cyl_vol (const float, const float); int main() { cout << cyl_vol(3,5); cout << radius; return 0; } float cyl_vol (const float radius, const float height) { return PI*radius*radius*height; }
61
#include <iostream> using namespace std; const float PI = 3
#include <iostream> using namespace std; const float PI = ; float cyl_vol (const float, const float); int main() { cout << cyl_vol(3,5); cout << radius; return 0; } float cyl_vol (const float radius, const float height) { return PI*radius*radius*height; }
62
void blanklines (const short num_lines);
int main() { blanklines(5); return 0; } void blanklines (const short num_lines) { for(short i = 1; i <= num_lines; i++) cout<<endl; return; }
63
End of Session
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.