Introduction to Programming

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CPS120: Introduction to Computer Science Lecture 14 Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Introduction to Programming
Basic concepts of C++ Presented by Prof. Satyajit De
Introduction to Computer Programming
Chapter 1.2 Introduction to C++ Programming
Functions + Overloading + Scope
Programming what is C++
Introduction to Programming
Dr. Shady Yehia Elmashad
REPETITION CONTROL STRUCTURE
Chapter 1.2 Introduction to C++ Programming
CO1401 Programming Design and Implementation
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Dr. Shady Yehia Elmashad
Chapter 5 - Functions Outline 5.1 Introduction
C++ Arrays.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
User Defined Functions
Functions I Creating a programming with small logical units of code.
Introduction to Programming
Flow of Control October 16, 2017.
Introduction to Programming
Conditional Construct
Functions.
CS150 Introduction to Computer Science 1
Functions, Part 1 of 3 Topics Using Predefined Functions
Summary Two basic concepts: variables and assignments Basic types:
CPS120: Introduction to Computer Science
Chapter 9: Value-Returning Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions Extra Examples.
Introduction to Programming
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Programming Fundamental
Programming Fundamental
Programming Fundamental-1
Presentation transcript:

Introduction to Programming Lecture 9

Programming Toolkit Decisions Loops Sequences

Laboratory Stool

Constructing a laboratory Stool

Constructing a laboratory Stool Task: Making a stool Subtask: Make a seat Make legs for the stool Assemble them

What we will study today … What are functions? How are they defined ? How are they declared ? What values are passed to functions ? What values do functions return ?

Function Function name { Body of the function }

Function Two types of functions: Functions that return a value Functions that do not return a value

Function return-value-type function-name( argument-list ) { declarations and statements }

Declaration of Function return-value-type function-name( argument--type-list) ; main ( ) { : }

Example int function-name ( int , int , double ) ; void main ( ) { …. }

Definition of Function int function-name ( int i , double j ) { … }

Return Type of Function Declaration int square ( int ) ; Definition int square ( int i ) { return ( i * i ) ; }

Function Call int x ; x = square ( i ) ;

Example: Function to calculate integer power ( Xn ) double raiseToPow ( double x , int power ) { double result ; int i ; result = 1.0 ; for ( i = 1 ; i <= power ; i ++ ) // braces first result * = x ; // result = result *x } return ( result ) ;

Code to Call the raisetopow Function include < iostream.h > void main ( ) { double x ; int i ; cout << “ Please enter the number “ ; cin >> x ; cout << “ Please enter the integer power that you want this number raised to “ ; cin >> i ; cout << x << “ raise to power “ << i << “is equal to “ << raiseToPow ( x , i ) ; }

Call By Value

Calling function Called function

Area of the Ring Area of Outer Circle = Area of the Ring

Example: Function to calculate the area of a circle double circleArea ( double radius ) { return ( 3.1415926 * radius * radius ) ; }

Calculating ringArea without using Function main ( ) { : ringArea = ( 3.1415926 * rad1 * rad1 ) – ( 3.1415926 * rad2 * rad2 ) ; }

Exercises Modify the raise to power function so that it can handle negative power of x, zero and positive power of x. For the area of ring function put in error checking mechanism.

In today’s lecture We used functions for breaking complex problems into smaller pieces, which is a top-down structured approach. Each function should be a small module, self contained and it should solve a well defined problem. Variable names and function names should be self explanatory. Always comment your code