Functions Venkatesh Ramamoorthy 21-March-2005. Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout << “The sine of 45 degrees is: ” << y.

Slides:



Advertisements
Similar presentations
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Advertisements

Return values.
Lab 8 User Defined Function.
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Overview creating your own functions calling your own functions.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Functions Why we use functions C library functions Creating our own functions.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32),
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
A Sample Program #include using namespace std; int main(void) { cout
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Week 3 - Wednesday CS 121.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Variables A piece of memory set aside to store data
Arithmetic & other operations
Introduction to Programming
Formatted and Unformatted Input/Output Functions
Sum and Difference Identities
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Value returning Functions
COMPUTER PROGRAMMING SKILLS
CPS125.
Presentation transcript:

Functions Venkatesh Ramamoorthy 21-March-2005

Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout << “The sine of 45 degrees is: ” << y << std::endl ; …

Arguments and Parameters Arguments –Passed to a function by a calling program Parameters –Taken in by a function from its caller program The difference stems from where you view the program –Either from the outside, or –From inside the function

Exercise Write a program that takes two inputs: – a positive number x (not necessarily an integer) –A positive number y (again, not necessarily an integer) and displays the following values correct to 4 decimal places: –The sine of x –The cosine of x –The tangent of x –The logarithm of x to the base 10 –The value of x y

Function Definitions By function definition, we mean the actual implementation of the function Syntax (return-type) function-name( type-1 parmeter1, type-2parmeter2, …) { return (variable-name) ; } Example double sin(double x) ;

Parts of the above syntax Let’s identify the different parts of the above syntax –Return-type –Function-name –Type of each parameter –Each parameter

Return type What type of result does a function return to its caller? –The usual, i.e. int, float, double, char, etc –This suggests that the function evaluates and outputs, to its caller, an integer, a float, a double, a character, etc –Example int f1(int a, double b, char c) { int x ; return (x) ; }

Function name The same rules for naming variables in C++ apply for naming functions as well –Ensure that you do not use standard or built-in function names (e.g. sin, cos, tan, log, etc) Example int EvaluatePolynomial(int polynomial, int degree) { int x ; <Lines of code to evaluate p of degree d. The value gets stored into x.> return (x) ; }

Parameters These are inputs, taken in by the function from the external world The inputs are populated as a comma- separated list Each list is a pair, consisting of the following: –The type of the input parameter variable –The name of the parameter variable

Example int abs(int x){…} –x is an input parameter of type int –The name of the function is abs –This function outputs (returns) an integer –This integer is the absolute value of the input x –In the above example, we haven’t amplified on the code to implement the function It turns out that this is a standard math function!

More examples int EvaluatePolynomial(int polynomial, int degree) { int x ; <Lines of code to evaluate p of degree d. The value gets stored into x.> return (x) ; } –What does this function do?

Exercise Write a function EvaluatePolynomial that does the following: –Takes an integer input x –Evaluates the polynomial ( x 3 – 6x x – 6 ) into a variable y –Returns y as an output to the calling program Write the main program that calls this function –Input an integer x using a suitable prompt –Call EvaluatePolynomial –Display its return value on the screen using a suitable prompt

Program Skeleton int EvaluatePolynomial(int x) { int y ; return y ; } int main() { int number, result ; /* Input number */ result = EvaluatePolynomial(number) ; /* Display result */ return (0) ; }