Function. Mathematical function and C+ + function. Input: arguments. Output: return value

Similar documents
Lab Instructor : Jean Lai

Local and Global Variables

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Functions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;

4. C++ functions. 1. Library Function 2. User-defined Function

C++ Programming Lecture 11 Functions Part I

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

EECS402 Lecture 02. Functions. Function Prototype

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

A SHORT COURSE ON C++

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Introduction to Programming

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Introduction. What is function? Multiple functions form a larger program Modular programming

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Computing and Statistical Data Analysis Lecture 3

Using Free Functions

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Chapter 3 - Functions

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Procedural Abstraction and Functions That Return a Value. Savitch, Chapter 4

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

do { statements } while (condition);

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

C++ For Science and Engineering Lecture 15

(created by professor Marina Tanasyuk) FUNCTIONS

Programming Language. Functions. Eng. Anis Nazer First Semester

Chapter 17 - Notes Recursion

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

BITG 1113: Function (Part 2) LECTURE 5

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Department of Computer and Mathematical Sciences. Lab 10: Functions. CS 1410 Intro to Computer Science with C++

Unit 6. Thinking with Functions

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

C++ Programming Lecture 1 Software Engineering Group

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

CSCE Practice Midterm. Data Types

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Ch 6. Functions. Example: function calls function

C++ Programming: From Problem Analysis to Program Design, Third Edition

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Computer Science II Lecture 2 Strings, Vectors and Recursion

C++ basics Getting started with, and Data Types.

6.096 Introduction to C++

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Void Functions Call-By-Reference Parameters Using Procedural Abstraction Testing and Debugging General Debugging Techniques

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Functions and Recursion

W3101: Programming Languages C++ Ramana Isukapalli

Computer Science & Engineering 150A Problem Solving Using Computers

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

Lecture 23: Pointer Arithmetic

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Exercise: Inventing Language

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CSCE Practice Midterm. Data Types

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Dr. Md. Humayun Kabir CSE Department, BUET

DELHI PUBLIC SCHOOL TAPI

Name SECTION: 12:45 2:20. True or False (12 Points)

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

EP241 Computer Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

1 Unit 8 'for' Loops

Name Section: M/W or T/TH. True or False (14 Points)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

SOLUTIONS TO EXERCISES PART ONE: Problem-Solving Techniques

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Chapter 7 - Notes User-Defined Functions II

CSCI 111 Midterm 1 Exam Spring Solutions 09.00am 09.50am, Monday, March 21, 2016

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Fundamentals of Programming CS-110. Lecture 2

Transcription:

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<cmath> Example: double sqrt_of_n = sqrt(n); Input: n Output:sqrt(n) sqrt function takes in a number, and returns the square root sqrt function is defined as double sqrt(double) sqrt function takes an input argument of type double sqrt function returns a value of type double

pow() pow function finds exponential calculation for you It is defined in the cstdlib library, #include<cmath> pow function take two number arguments type of double, and return the result is first number power second number which type of double. pow function defines as pow(double,double) Example: Pow(2,3) will return 8

Exercise Exercise 1: Take a user input number, print out the square root of this number. Exercise 2: Take a user input number, print out the forth root of this number. Exercise 3: Take two number n1, n2 input from user, print out the n1 n2. Exercise 4: Take a user input number, print out the cube root of this number.

Create our own functions Creating a function is much like declaring a variable, it has two parts 1. Prototype This gives the compiler a preview of what your function would look like This usually goes after using namespace std; and before main() 2. Definition This defines the actions the function should take This usually goes after the main() function

Model of function prototype return_type function_name (parameter_list); 1. return_type What the function will return 2. function_name Name of the function 3. parameter_list List of data type of parameter(s)

Model for function definition return_type function_name (parameter_list) { } code goes here parameter_list This parameter list will include the type and the name of the variable

Example of function,reading input Prototype/Header: int getnumber(); Definition: int getnumber() //matches above prototype/header { Calling the function: int main() { int n= getnumber(); return 0; } } int num; cout<< Enter a number: ; cin>>num; return num; /*Note there turn type of the function matches the variable in which the value will be stored.*/

Function s Return Value Function often serve very specific purposes. In our example it was to read in a value from the user. This function getnumber need to be able to communicate this newly obtained value back to the calling function. It does so with a return statement. This return statement is for transferring information from the sub function back calling function. The act of returning a value is done so through the keyword return. Returning a value is NOT the same as cout information to screen. Next example demonstrates a function that outputs to the screen,however does not return a value.

Example - output function Prototype/Header: void printnumber(int); Definition: Calling the function: int main() { void printnumber(int num) // matches above { } cout<<num <<endl; Note the function type is void,nothing is being returned } //gets a number from the user int n=getnumber(); //prints the number to screen printnumber( n ); return0;

Basics of Using Functions At the beginning of your program, declare the function. Somewhere in your program, define the function. Other functions can then call the function.

Example of 3 part #include <iostream> using namespace std; double avg(double x, double y); // Declare int main() { double a = 1.2; double b = 2.7; cout << "Avg is " << avg(a,b); // Call } double avg(double x, double y) // Define { double v = (x + y)/2; return v; }

Why use functions Organizational reason 1. Sometimes we have a lot to do in our program 2. Functions offers a way to break a part a large program into smaller sub programs. Think of a paragraph of text that is very long, if you lost your position, it is hard to find it again. Logical reason 3. A task might be performed repeatedly throughout different parts of the program 4. Instead of copying and pasting the same code into multiple places, we can replace that with a function. If we need to make modifications it is much harder to change it in multiple places Much easier to change it in just that one function

Designing of functions There are many different views on what is consider a well design function. There are even arguments on why functions should be used at all, poorly designed functions will use up a lot of system resources, when the function is called. Each function should do one thing, achieve one task. Functions should be short, not more than X number of lines long X being a number that the designer sees fit and it also depends on what the function needs to accomplish. Think of it as writing a paragraph,as soon as you complete presenting the idea then you are done.

Exercise Exercise 5: Write a function called printhello() that outputs Hello on the screen. Exercise 6: Write a function called circlearea() that takes an double value from parameter, calculates the area of the circle, returns the area of the circle (pi=3.14, circle area = pi * r^2) Exercise 7: Write a function called displayreverse() that takes an integer and displays the reverse of that number on the screen. Exercise 8: Write a function called numbertotens() that takes an integer and returns the corresponding number in one and zeros. For example: if the number is 54321, returns 10000.

Exercise Exercise 9. Write a program that defines and tests a factorial function. The factorial of a number is the product of all whole numbers from 1 to N. For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120 Exercise 10. Write a program that finds the first prime number greater than 1 billion (1,000,000,000).