Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.

Slides:



Advertisements
Similar presentations
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Advertisements

Computer Science 1620 Loops.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
Functions:Passing Parameters by Value Programming.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Functions.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Radian Measure. Many things can be measured using different units.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Basics of Most C++ Programs // Programmer: Clayton Price date: 9/4/ // File: fahr2celc.cpp 03. // Purpose:
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Celsius and Fahrenheit
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Passing Value Passing by Value (or by Copy) main() { int i = 5; pr_it(i); return 0; } pr_it(int i) { cout
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
C++ Basics Lecture 2.
Repetitive Structures
Intermediate Programming using C++ Functions part 2
CO1401 Programming Design and Implementation
Arrays Part-1 Armen Keshishian.
Chapter 2.2 Control Structures (Iteration)
לולאות קרן כליף.
While Loops.
Repetition Statements
פונקציות לעיתים קרובות לא נוח להגדיר את כל התוכנה בתוך גוף אחד.
CS1201: Programming Language 2
Compound Assignment Operators in C++
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Value returning Functions
CS150 Introduction to Computer Science 1
Counting Loops.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
The Run-Time Stack and Reference Parameters
CS150 Introduction to Computer Science 1
CS1201: Programming Language 2
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements.
Pointers & Functions.
Functions Divide and Conquer
HNDIT11034 More Operators.
CS150 Introduction to Computer Science 1
Module 3 Selection Structures 12/7/2019 CSE 1321 Module 3.
Presentation transcript:

Lecture 4 Function example

Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout << max (x, y); }

Example 2 Write C++ program to calculate the squared value of a number passed from main function. Use this function in a program to calculate the squares of numbers from 1 to 10: #include int square ( int y ) { int z; z = y * y; return ( z ); } void main( ) { ………………………………….. }

Example 3 Write C++ program using function to calculate the average of two numbers entered by the user in the main program: #include …………… averge (………….…….) { …………………… } void main( ) { float x; // x is the varible for average int num1,num2; cout << "Enter 2 positive number \n"; cin >> num1 >> num2; x = ……………………………….. cout << x; }

Example 4 Write C++ program, using function, to find the summation of the following series: #include int summation ( int x) { } void main ( ) { int n,s; cout << "enter positive number"; cin >> n; s = summation ( n ); cout << "sum is: " << s << endl; }

Example 5 Write a C++ program, using function, to convert a given temperature radian in a degrees Fahrenheit (F) to degrees Celsius (C) using the formula: C = 5 / 9 ( F - 32 )