Introduction. What is function? Multiple functions form a larger program Modular programming
|
|
- Gilbert Kelley
- 6 years ago
- Views:
Transcription
1 FUNCTION CSC128
2 Introduction What is function? Module/mini program/sub-program Each function/module/sub-program performs specific task May contains its own variables/statements Can be compiled/tested independently An approach to handle complexity in solving problems where:- A larger / complex problem is broken up into smaller problems (less complex) The solution to the problem (a module), consists of a combination of solutions for the smaller problems (sub modules) Multiple functions form a larger program Modular programming Break 1 large program or 1 module into sub-modules
3 Introduction
4 Example WITHOUT functions int main() float num1, num2, sum; cin >> num1 >> num2; sum = num1 + num2; cout << "Total Sum: " << sum; return 0; 4
5 Example WITH functions 1/2 float num1, num2, sum; void getinputs(); void calculatesum(); void displaysum(); int main() getinputs(); calculatesum(); displaysum(); return 0; Variables declaration Modules/functions declaration Main function Calling 5 modules/functions
6 Example WITH functions 2/2 void getinputs() cin >> num1 >> num2; Function Definitions or the actual modules are void calculatesum() here, right after void main(). Each module perform sum = num1 + num2; specific task void displaysum() cout << "Total Sum: " << sum; 6
7 Example WITHOUT and WITH functions
8 Benefits of functions Divide and conquer Manageable program development Software reusability Use existing functions as building blocks for new programs Abstaction-hide internal details (library functions) Avoid repetition Simplify main() function/program Planning, coding, testing, debugging, understanding, maintaining a computer program will be easier Same function can be reused in another program Prevent function duplication Reduce time of writing a program
9 Introduction TWO (2) types of functions :- [1] Pre-defined/built-in/library Required special pre-processor (*.h files older version) iostream: cin, cout cstring: strcpy, strcmp cmath: sqrt(), pow(), abs() [2] Programmer-defined/independent Depends on programmer what kind of task will be performed by the function
10 Contents Predefined Function User defined Function
11 Family of function
12 Predefined function
13 Predefined Function Functions that have been defined by the producer of a compiler. Declaration of the functions are stored in header file. (.h extension older version) Also known as built in functions Used by the programmers to speed up program writing.
14 Predefined Function Programmers may use the existing code to perform common tasks without having to rewrite any code. These functions are predefined by the producer of a compiler, (C++) and are stored in the header files (.h files) called libraries To use a pre defined function, appropriate library file must be included in the program using the #include directive.
15 Those without the.h is C++ header files while those with.h are C header files. This only applies to the standard header files in C++. The old standard used the #include <filename.h> syntax. When namespaces and templates were added to the language, the standard was changed to #include <filename> The #include <filename.h> was common in C++ code prior to the C++ standard. The standard changed it to #include <filename> with everything from the header placed in the std namespace.
16 Predefined Function Header files Functions Descriptions cmath pow(x, y) Raise to power. sqrt(x) floor(x) ceil(x) abs(x) Compute square root. Round down value. Round up value. Absolute value cstring strcpy(x,y) Copy sequence of character strcmp(x,y) strcat(x,y) To compare two sequence of character To concatenate sequence of character
17 Predefined Function iomanip: header file to manipulate input/output data. Built-in functions: Function setw(x) setfill(x) setprecision(x) Description Set field width to x Set the fill character with x Set the floating point precision to x 17
18 Predefined Function cstring: header file contains string manipulation functions. Built-in functions: Function Description strcmp(s1, s2) Compares one string to another strcpy(s1, s2) Copies one string to another strlen(s) Calculates the length of a string strcat(s1, s2) Appends one string to another 18
19 Predefined Function cctype: header file for character handling functions. Built-in functions: Function toupper(c) tolower(c) isupper(c) islower(c) Description Converts character c from lowercase to uppercase letter Converts character c from uppercase to lowercase letter Return TRUE if c is an uppercase letter Return TRUE if c is a lowercase letter 19
20 Predefined Function cctype: header file for character handling functions. Built-in functions: Function isdigit(c) isalpha(c) isspace(c) Description Return TRUE if c is digit Return TRUE if c is an alphanumeric character Return TRUE if c is a space character 20
21 Predefined Function cmath: header file for mathematical functions. Built-in functions: Functions pow(x, y) sin(x) acos(x) cosh(x) sqrt(x) cos(x) atan(x) tanh(x) ceil(x) tan(x) log(x) log10(x) floor(x) asin(x) sinh(x) exp(x) fabs(x) 21
22 Predefined Function # include<iostream> # include<cmath> using namespace std; void main() int x,y,z; y = 2, z = 4; x = pow(z,y); cout<< x: <<x; Function call Function call: is used to call or to invoke a function. z and y are called parameters or arguments of the function pow.
23 Predefined Function #include <iostream> #include <cmath> using namespace std; void main() cout << pow(2, 3); Pre-processor files Power function retrieved from cmath cout function retrieved from iostream 23
24 Defining and Calling Functions Function call: statement causes a function to execute Function definition: statements that make up a function
25 Function Definition Definition includes: return type: data type of the value that function returns to the part of the program that called it name: name of the function. Function names follow same rules as variables parameter list: variables containing values passed to the function body: statements that perform the function s task, enclosed in
26 Function Definition Note: The line that reads int main() is the function header.
27 Variables
28 Variables Two types of variables: 1) Local variable 2) Global variable
29 Variables local variable Variable that is declared within a body of a function definition. Variable that is declared within the main() function are said to be local to main function.
30 Variables local variable Example: #include <iostream> using namespace std; void main() int a; Local variable
31 Variables local variable
32 Variables global variable Variable that is declared outside and above main function. Accessible to all function in the program. Its value can be changed at any point during execution New value will replace old value assigned to a particular global variable It is not wise to use global variables any more than you have to.
33 Variables global variable Example: global variable #include <iostream> using namespace std; int a; void main()
34 Variables global variable
35 User defined function
36 User-defined function Functions that are defined / written by the programmer / user in a program. Characteristics of user-defined function: A function name is named with unique name. Can be called from other function A function performs a specific task. Task is a discrete job that the program must perform as part of its overall operation. A function is independent. A function may receive values from the calling program (function call). A function may return value to the calling program (function call).
37 User-defined function In general a function has the following syntax: returnvaluetype functionname(list of parameters)
38 User-defined function void/ value returning function. With/ without parameters. returnvaluetype functionname(list of parameters)
39 User-defined function To create a user defined function, you must write the following: 1) Function prototype (function s declaration) 2) Function definition 3) Function call If the function definition is defined before the main function, you may not need the function prototype.
40 User-defined Function Requirements variable declarations; //global void functiona(); void main() variable declarations; //local functiona(); statements; //other statements void functiona() statements; 1) Function prototype declarations 2) Function call 3) Function definitions 40
41 User-defined Function Requirements int calculatesum(int, int); void main() int num1, num2; cin >> num1 >> num2; cout << calculatesum(num1, num2); void calculatesum (int x, int y) int total = x + y; return total; 1) Function prototype declarations 41 2) Function call 3) Function definitions
42 User-defined Function Requirements int calculatesum(int, int); void calculatesum (int x, int y) int total = x + y; return total; void main() int num1, num2; cin >> num1 >> num2; calculatesum(num1, num2); 1) Function prototype declarations 3) Function definitions 2) Function call 42
43 1) Function prototype Is used to declare a function. Problem statement: to create a function to calculate total of 2 numbers. Steps: 1) Identify function name. 2) void/ value returning function? 3) With/ without parameter?
44 1) Function prototype..cont. void function without parameter void calculatesum(); Can you see the differences? void function with parameter? void calculatesum (int x, int y); Value returning function, without parameter. (return integer value) int calculatesum(); Value returning function, with parameter.. (return integer value) int calculatesum (int x, int y);
45 1) Function prototype..cont. Is placed in between preprocessor directives and main function. #include <iostream> using namespace std; void calculatesum (); void main()
46 2) Function definition Is a block of code (statements) that perform specific task. Statements are usually declaration and/or executable statements. Must be declared 1 st before it can be used Place it BEFORE or AFTER main()
47 2) Function definition..cont. Syntax: Function header returntype functionname (type parameter_list) declaration(s); statement(s); return expression; Function body 47
48 2) Function definition..cont. Example using void return type: void sumnumbers(int, int, int) int total = a + b + c; cout << total; If you use void in function header, you must NOT have return statement inside function body 48
49 2) Function definition..cont. Example using int return type: int sumnumbers(int, int, int) int total = a + b + c; return total; If you use int/float/double/long/char in function header, you MUST HAVE return statement inside function body 49
50 3) Function call When a function is called, the program control is passed to the called function and the statements inside the function will be executed until control is passed back the calling function To call a function, specify the function name and the values of the parameters [if any]
51 3) Function call..cont. [3a]Function call that returns no value Contains void return type on function header Once complete execute last statement in function definition, the control is passed back to the statement that calls the function in the calling function Then, next statement will be executed 51
52 3) Function call..cont. [3b]Function call that returns a value Contains other than void on function header When execute of function definition is complete, control is passed back to the calling function with a value That returned value can be used: In an arithmetic expression, logical expression, assignment statement, output statement 52
53 Recap No return value Return value Without parameters void calculatesum () int calculatesum () With parameters void calculatesum (int a, int b) int calculatesum (int a, int b) 53
54 void function without parameter General syntax:
55 void function without parameter Example: return_type function_name parameter_list void caltotal() int a, b, result; a = 1, b = 2; result = a + b; Function header function_body cout<<result;
56 void function without parameter Function call: is used to invoke/ to call a function to execute task. Is placed inside main function. Example: int main() caltotal(); return 0;
57 void function without parameter #include<iostream> using namespace std; void Full code: caltotal(); int main() caltotal(); return 0; Function header Function prototype Function call void caltotal() int a, b, result; a = 1, b = 2; result = a + b; cout<<result; Function definition
58 void function without parameter Exercise 1: using void function, write a program to calculate an average of 3 numbers.
59 void function with parameter Parameter: Parameter is ways how calling function and called function are communicated. Through parameter, data can pass from calling function to called function and from called function to calling function. Formal parameters are optional Two types of parameter: Value parameter Reference parameter
60 void function with parameter Value parameter A formal parameter that receives a copy of the content of the corresponding actual parameter. Reference parameter* A formal parameter that receives the location of the corresponding parameter.
61 void function with parameter (value) void caltotal(int x, int y) int result; result = x + y; cout<<result; Formal parameter
62 void function with parameter (value) #include<iostream> using namespace std; void caltotal(int, int); void main() int a; int b; a = 2, b = 3; caltotal(a, b); void caltotal(int x, int y) int result; Actual parameter result = x + y; cout<<result;
63 void function with parameter (value) void Example: findstatus(int); void main() int mark; cout<< enter mark: ; cin>>mark; findstatus(mark); void findstatus(int value) if (value>=80) cout<< excellent ; else if(value>=60) cout<< good ; else if(value>=50) cout<< pass ; else cout<< fail ;
64 void function with parameter (value) Exercise 2: using void function with parameter, write a program that require a user to input 2 numbers and determine the maximum number.
65 Value returning function Is a function that after completing its assigned task, return precisely one value to the calling function (function call). Why we return a value? To use it for further calculation or process. Is used in an assignment statement or in an output statement.
66 Value returning function Function definition: Return type or data type int caltotal(int x, int y) int result; result = x + y; return result;
67 Value returning function #include<iostream.h> int caltotal(int, int); Function prototype void main() int a,b,z; a = 2, b = 3 z = caltotal(a, b); cout<< total : <<z; Function call: Assignment statement int caltotal(int x, int y) int result; result = x + y; return result;
68 Value returning function #include<iostream.h> int caltotal(int, int); void main() int a,b; a = 2, b = 3 cout<< total : << caltotal(a, b); Function call: Output statement int caltotal(int x, int y) int result; result = x + y; return result;
69 Passing Value Between Functions cout 2 return 2 int calcsum(int, int); void main() int a, b; cin >> a >> b; cout << calcsum(a, b); int calcsum(int x, int y) return (x + y); Example using return statement a = x = 1 b = y = 1 69
70 Value returning function Exercise 3: using value returning function, write a program to determine maximum value of two numbers.
71 Value returning function Exercise 4: using value returning function write a program to calculate an average of three numbers.
72 Function (reference parameter)
73 Function reference parameter If a formal parameter is a reference parameter It receives the address (memory location) of the corresponding actual parameter During program execution to manipulate the data The address stored in the reference parameter directs it to the memory space of the corresponding actual parameter Can pass one or more values from a function Can change the value of the actual parameter
74 Function reference parameter A reference parameter receives and stores the address 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.
75 Function reference parameter
76 Function value parameter #include <iostream> using namespace std; void addfour(int a, int b); void main() int num1,num2; num1 = 3; num2 = 1; cout<< num1<<num2<<endl; addfour(num1,num2); cout<<num1<<num2<endl; In main BEFORE calling addfour function In addfour function void addfour(int a, int b) a = a + 4; Value parameter b++; cout<<a<<b<<endl; In main AFTER calling addfour function num1 3 a 7 num1 3 num2 1 b 2 num2 1
77 Function reference #include parameter <iostream> using namespace std; void addfour(int& a, int b); void main() int num1,num2; num1 = 3; num2 = 1; cout<< num1<<num2<<endl; addfour(num1,num2); cout<<num1<<num2<endl; In main BEFORE calling addfour function In addfour function void addfour(int &a, int b) a = a + 4; //line 1 b++; //line 2 cout<<a<<b<<endl; Reference parameter When the value of a changes, value num1 changes as well Reference parameter In main AFTER calling addfour function num1 3 a 7 num1 7 num2 1 b 2 num2 1
78 Function reference parameter How it works? In main BEFORE calling addfour function In addfour function num1 3 a num2 1 b 1 Before statement in line 1 execute!!
79 In main BEFORE calling addfour function In addfour function num1 7 a num2 1 b 2 In function addfour, after statement in line 1 and line 2 execute!!
80 Scope of a local and global variable #include <iostream> void caltotal(); using namespace std; void main() int a; void caltotal() int a; Variable a in function main() is different from variable a in Function caltotal()
81 Global variable #include <iostream> using namespace std; void caltotal(); int num1; void main() int num2; total = num1 + num2 void caltotal() int num2; total = num1 num2; Variable num1 is a global variable that are accessible to all function definitions in the file.
82 Summary void function: a function that does not have a data type A return statement without any value can be used in a void function to exit function early The heading of a void function starts with the word void To call a void function, you use the function name together with the actual parameters in a stand-alone statement
83 Summary (continued) Two types of formal parameters: value parameters and reference parameters A value parameter receives a copy of its corresponding actual parameter A reference parameter receives the address (memory location) of its corresponding actual parameter
84 Summary (continued) If a formal parameter needs to change the value of an actual parameter, in the function heading you must declare this formal parameter as a reference parameter Variables declared within a function (or block) are called local variables Variables declared outside of every function definition (and block) are called global variables
85 Summary (continued) If a formal parameter needs to change the value of an actual parameter, in the function heading you must declare this formal parameter as a reference parameter Variables declared within a function (or block) are called local variables Variables declared outside of every function definition (and block) are called global variables
86
C++ Programming Lecture 11 Functions Part I
C++ Programming Lecture 11 Functions Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Introduction Till now we have learned the basic concepts of C++. All the programs
More informationA. Introduction to Function 1. Modular Programming input processing output functions library functions 2. Function 1. Benefit of Using Functions
Lesson Outcomes At the end of this chapter, student should be able to: Use pre-defined functions: (sqrt(), abs(), pow(), toupper(), tolower(), strcmp(), strcpy(), gets()) Build independent functions or
More informationSummary of basic C++-commands
Summary of basic C++-commands K. Vollmayr-Lee, O. Ippisch April 13, 2010 1 Compiling To compile a C++-program, you can use either g++ or c++. g++ -o executable_filename.out sourcefilename.cc c++ -o executable_filename.out
More informationChapter 6 - Notes User-Defined Functions I
Chapter 6 - Notes User-Defined Functions I I. Standard (Predefined) Functions A. A sub-program that performs a special or specific task and is made available by pre-written libraries in header files. B.
More informationEngineering Problem Solving with C++, Etter/Ingber
Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input
More informationA function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.
Chapter-12 FUNCTIONS Introduction A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called. Types of functions There are two types
More informationChapter 2. Outline. Simple C++ Programs
Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.
More information3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.
1 3. Functions 1. What are the merits and demerits of modular programming? Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.
More informationComputer Science & Engineering 150A Problem Solving Using Computers
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Stephen Scott (Adapted from Christopher M. Bourke) 1 / 41 Fall 2009 Chapter 3 3.1 Building Programs from Existing Information
More informationChapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.
Chapter 3 Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus Lecture 03 - Introduction To Functions Christopher M. Bourke cbourke@cse.unl.edu 3.1 Building Programs from Existing
More informationComputer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Functions Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Chapter 3 3.1 Building
More informationC++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 6: User-Defined Functions I In this chapter, you will: Objectives Learn about standard (predefined) functions and discover
More information4. C++ functions. 1. Library Function 2. User-defined Function
4. C++ functions In programming, function refers to a segment that group s code to perform a specific task. Depending on whether a function is predefined or created by programmer; there are two types of
More informationC++ Quick Reference. switch Statements
C++ Quick Reference if Statements switch Statements Array/Initializer if (condition) if (condition) else if (condition1) else if (condition2) else Loop Statements while (condition) do while (condition);
More informationChapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016
Chapter 6: User-Defined Functions Objectives In this chapter, you will: Learn about standard (predefined) functions Learn about user-defined functions Examine value-returning functions Construct and use
More informationFunctions. Lecture 6 COP 3014 Spring February 11, 2018
Functions Lecture 6 COP 3014 Spring 2018 February 11, 2018 Functions A function is a reusable portion of a program, sometimes called a procedure or subroutine. Like a mini-program (or subprogram) in its
More informationLecture 04 FUNCTIONS AND ARRAYS
Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions
More informationC++ Overview. Chapter 1. Chapter 2
C++ Overview Chapter 1 Note: All commands you type (including the Myro commands listed elsewhere) are essentially C++ commands. Later, in this section we will list those commands that are a part of the
More information3.1. Chapter 3: The cin Object. Expressions and Interactivity
Chapter 3: Expressions and Interactivity 3.1 The cin Object Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 The cin Object Standard input stream object, normally the keyboard,
More informationFunctions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;
Function Topic 4 A function is a subprogram that performs a specific task. you know: cout > number; Pre-defined and User-defined Pre-defined Function Is a function that is already defined
More informationProgramming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1
300580 Programming Fundamentals 3 With C++ Variable Declaration, Evaluation and Assignment 1 Today s Topics Variable declaration Assignment to variables Typecasting Counting Mathematical functions Keyboard
More informationIntroduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation
Introduction to Algorithms and Data Structures Lecture 6 - Stringing Along - Character and String Manipulation What are Strings? Character data is stored as a numeric code that represents that particular
More informationLab Instructor : Jean Lai
Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.
More informationFunction. Mathematical function and C+ + function. Input: arguments. Output: return value
Lecture 9 Function Mathematical function and C+ + function Input: arguments Output: return value Sqrt() Square root function finds the square root for you It is defined in the cmath library, #include
More informationMore Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4
More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS! Thursday, 10 AM 12 PM
More informationOutline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters
Outline Functions Predefined functions User-defined functions Actual parameters Formal parameters Value parameters Variable parameters Functions 1 Functions 2 Functions Predefined Functions In C++ there
More informationMethods CSC 121 Fall 2014 Howard Rosenthal
Methods CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class Learn the syntax of method construction Learn both void methods and methods that
More informationCSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011
CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011 Date: 01/18/2011 (Due date: 01/20/2011) Name and ID (print): CHAPTER 6 USER-DEFINED FUNCTIONS I 1. The C++ function pow has parameters.
More informationC++ Programming: Functions
C++ Programming: Functions Domingos Begalli Saddleback College, Computer Science CS1B, Spring 2018 1 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 1/22 22 we will cover predefined functions user-defined
More informationC++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions
Areas for Discussion C++ Functions Joseph Spring School of Computer Science Operating Systems and Computer Networks Lecture Functions 1 Last Week Introduction to Functions Program Structure and Functions
More informationBIL 104E Introduction to Scientific and Engineering Computing. Lecture 4
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4 Introduction Divide and Conquer Construct a program from smaller pieces or components These smaller pieces are called modules Functions
More informationSuperior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6
Superior University Department of Electrical Engineering CS-115 Computing Fundamentals Experiment No.6 Pre-Defined Functions, User-Defined Function: Value Returning Functions Prepared for By: Name: ID:
More information6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x
(Function) -1.1 Math Library Function!"#! $%&!'(#) preprocessor directive #include !*+!"#!, Function Description Example sqrt(x) square root of x sqrt(900.0) is 30.0 sqrt(9.0) is 3.0 exp(x) log(x)
More informationLOGO BASIC ELEMENTS OF A COMPUTER PROGRAM
LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM Contents 1. Statements 2. C++ Program Structure 3. Programming process 4. Control Structure STATEMENTS ASSIGNMENT STATEMENTS Assignment statement Assigns a value
More informationA SHORT COURSE ON C++
Introduction to A SHORT COURSE ON School of Mathematics Semester 1 2008 Introduction to OUTLINE 1 INTRODUCTION TO 2 FLOW CONTROL AND FUNCTIONS If Else Looping Functions Cmath Library Prototyping Introduction
More informationCISC 1110 (CIS 1.5) Introduc2on to Programming Using C++
CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++ Spring 2012 Instructor : K. Auyeung Email Address: Course Page: Class Hours: kenny@sci.brooklyn.cuny.edu hbp://www.sci.brooklyn.cuny.edu/~kenny/cisc1110
More informationC Functions. 5.2 Program Modules in C
1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking
More informationWeek 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.
Week 2: Console I/O and Operators Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.1) CS 1428 Fall 2014 Jill Seaman 1 2.14 Arithmetic Operators An operator is a symbol that tells the computer to perform specific
More informationGovernment Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING
Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++
More informationANSI C Programming Simple Programs
ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include #include #include main() { /* Declare and initialize variables */ double
More informationIt is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.
Functions A number of statements grouped into a single logical unit are called a function. The use of function makes programming easier since repeated statements can be grouped into functions. Splitting
More informationFunctions. CS111 Lab Queens College, CUNY Instructor: Kent Chin
Functions CS111 Lab Queens College, CUNY Instructor: Kent Chin Functions They're everywhere! Input: x Function: f Output: f(x) Input: Sheets of Paper Function: Staple Output: Stapled Sheets of Paper C++
More informationOperations. Making Things Happen
Operations Making Things Happen Object Review and Continue Lecture 1 2 Object Categories There are three kinds of objects: Literals: unnamed objects having a value (0, -3, 2.5, 2.998e8, 'A', "Hello\n",...)
More informationCT 229 Java Syntax Continued
CT 229 Java Syntax Continued 06/10/2006 CT229 Lab Assignments Due Date for current lab assignment : Oct 8 th Before submission make sure that the name of each.java file matches the name given in the assignment
More informationDepartment of Computer and Mathematical Sciences. Lab 10: Functions. CS 1410 Intro to Computer Science with C++
_ Unit 2: Programming in C++, pages 1 of 8 Department of Computer and Mathematical Sciences CS 1410 Intro to Computer Science with C++ 10 Lab 10: Functions Objectives: The objective of this lab is to understand
More informationFORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):
FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The basic commands that a computer performs are input (get data), output (display result),
More information5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!
FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. Assume that all variables are properly declared. The following for loop executes 20 times.
More informationCHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.
More informationwww.thestudycampus.com Methods Let s imagine an automobile factory. When an automobile is manufactured, it is not made from basic raw materials; it is put together from previously manufactured parts. Some
More informationMethods CSC 121 Fall 2016 Howard Rosenthal
Methods CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods
More informationChapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.
Chapter 4 Procedural Abstraction and Functions That Return a Value 1 Overview 4.1 Top-Down Design 4.2 Predefined Functions 4.3 Programmer-Defined Functions 4.4 Procedural Abstraction 4.5 Local Variables
More informationStrings and Library Functions
Unit 4 String String is an array of character. Strings and Library Functions A string variable is a variable declared as array of character. The general format of declaring string is: char string_name
More informationCHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.
More informationBITG 1233: Introduction to C++
BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token
More informationC++ basics Getting started with, and Data Types.
C++ basics Getting started with, and Data Types pm_jat@daiict.ac.in Recap Last Lecture We talked about Variables - Variables, their binding to type, storage etc., Categorization based on storage binding
More informationcase control statement (switch case)
KEY POINTS: Introduction to C++ C++ is the successor of C language & developed by Bjarne Stroustrup at Bell Laboratories, New Jersey in 1979. Tokens- smallest individual unit. Following are the tokens
More informationIntroduction to Programming
Introduction to Programming session 9 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel s slides Sahrif University of Technology Outlines
More informationB.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University
Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are
More informationIntroduction to Engineering gii
25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB - Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2.
More informationGE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004
GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 Functions and Program Structure Today we will be learning about functions. You should already have an idea of their uses. Cout
More informationC++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh
C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,
More informationMethods CSC 121 Spring 2017 Howard Rosenthal
Methods CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods
More informationChapter 7 - Notes User-Defined Functions II
Chapter 7 - Notes User-Defined Functions II I. VOID Functions ( The use of a void function is done as a stand alone statement.) A. Void Functions without Parameters 1. Syntax: void functionname ( void
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine
More informationChapter 15 - C++ As A "Better C"
Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference
More informationC Functions. CS 2060 Week 4. Prof. Jonathan Ventura
CS 2060 Week 4 1 Modularizing Programs Modularizing programs in C Writing custom functions Header files 2 Function Call Stack The function call stack Stack frames 3 Pass-by-value Pass-by-value and pass-by-reference
More informationDownloaded from
Function: A function is a named unit of a group of statements that can be invoked from other parts of the program. The advantages of using functions are: Functions enable us to break a program down into
More informationDiscussion 1E. Jie(Jay) Wang Week 10 Dec.2
Discussion 1E Jie(Jay) Wang Week 10 Dec.2 Outline Dynamic memory allocation Class Final Review Dynamic Allocation of Memory Recall int len = 100; double arr[len]; // error! What if I need to compute the
More informationC++, How to Program. Spring 2016 CISC1600 Yanjun Li 1
Chapter 6 Function C++, How to Program Deitel & Deitel Spring 2016 CISC1600 Yanjun Li 1 Function A function is a collection of statements that performs a specific task - a single, well-defined task. Divide
More informationProgramming Language. Functions. Eng. Anis Nazer First Semester
Programming Language Functions Eng. Anis Nazer First Semester 2016-2017 Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities
More informationReview. Modules. CS 151 Review #6. Sample Program 6.1a:
Review Modules A key element of structured (well organized and documented) programs is their modularity: the breaking of code into small units. These units, or modules, that do not return a value are called
More informationMultiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationChapter 3 - Functions
Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number
More informationChapter 1 INTRODUCTION
Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware consists of the physical components of the system. The software is the collection of programs that a computer
More informationSuperior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II
Superior University Department of Electrical Engineering CS-115 Computing Fundamentals Experiment No.7 User Defined Functions II Prepared for By: Name: ID: Section: Semester: Total Marks: Obtained Marks:
More informationBasic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces
Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program
More informationI/O Streams and Standard I/O Devices (cont d.)
Chapter 3: Input/Output Objectives In this chapter, you will: Learn what a stream is and examine input and output streams Explore how to read data from the standard input device Learn how to use predefined
More informationDr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)
Lecture 9 Functions Dr M Kasim A Jalil Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Objectives In this chapter, you will learn: To understand how to construct programs modularly
More informationFunctions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.
1 6 Functions and an Introduction to Recursion 2 Form ever follows function. Louis Henri Sullivan E pluribus unum. (One composed of many.) Virgil O! call back yesterday, bid time return. William Shakespeare
More informationUNIT - I. Introduction to C Programming. BY A. Vijay Bharath
UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been
More informationCSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial
CSI31 Lecture 5 Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial 1 3.1 Numberic Data Types When computers were first developed, they were seen primarily as
More informationCS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27
CS31 Discussion 1E Jie(Jay) Wang Week5 Oct.27 Outline Project 3 Debugging Array Project 3 Read the spec and FAQ carefully. Test your code on SEASnet Linux server using the command curl -s -L http://cs.ucla.edu/classes/fall16/cs31/utilities/p3tester
More informationTop-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names
Chapter 4 In this chapter, you will learn about: Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names Top-Down Design Top-Down
More informationC++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters
C++ Arrays Areas for Discussion Strings and Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Lecture Arrays 1 Lecture Arrays 2 To declare an array: follow
More information1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.
1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format
More informationPIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment
PIC 10A Lecture 3: More About Variables, Arithmetic, Casting, Assignment Assigning values to variables Our variables last time did not seem very variable. They always had the same value! Variables stores
More informationCHAPTER 4 FUNCTIONS. 4.1 Introduction
CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main
More informationBITG 1113: Array (Part 2) LECTURE 9
BITG 1113: Array (Part 2) LECTURE 9 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of C-strings (character arrays) 2. Use C-string functions 3. Use
More informationUnderstanding main() function Input/Output Streams
Understanding main() function Input/Output Streams Structure of a program // my first program in C++ #include int main () { cout
More informationLecture 5: Methods CS2301
Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int
More informationCHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++
CHAPTER 9 C++ 1. WRITE ABOUT THE BINARY OPERATORS USED IN C++? ARITHMETIC OPERATORS: Arithmetic operators perform simple arithmetic operations like addition, subtraction, multiplication, division etc.,
More informationASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]
ASSIGNMENT-1 2016-17 CLASS-11 COMPUTER SCIENCE [C++] 1 Consider the following C++ snippet: int x = 25000; int y = 2*x; cout
More informationIntroduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department
Introduction to Functions in C Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department Function definition For example Pythagoras(x,y,z) double x,y,z; { double d;
More informationProgramming in MATLAB
trevor.spiteri@um.edu.mt http://staff.um.edu.mt/trevor.spiteri Department of Communications and Computer Engineering Faculty of Information and Communication Technology University of Malta 17 February,
More informationFunctions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single
Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module
More informationEngineering Problem Solving with C++, 3e Chapter 2 Test Bank
1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A. integer B 1.427E3 B. double D "Oct" C. character B -63.29 D. string F #Hashtag
More informationEngineering Problem Solving with C++, Etter
Engineering Problem Solving with C++, Etter Chapter 6 Strings 11-30-12 1 One Dimensional Arrays Character Strings The string Class. 2 C style strings functions defined in cstring CHARACTER STRINGS 3 C
More informationFunctions. Systems Programming Concepts
Functions Systems Programming Concepts Functions Simple Function Example Function Prototype and Declaration Math Library Functions Function Definition Header Files Random Number Generator Call by Value
More informationFunctions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction
Functions Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur Programming and Data Structure 1 Function Introduction A self-contained program segment that
More informationOrdinary Differential Equation Solver Language (ODESL) Reference Manual
Ordinary Differential Equation Solver Language (ODESL) Reference Manual Rui Chen 11/03/2010 1. Introduction ODESL is a computer language specifically designed to solve ordinary differential equations (ODE
More information