Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.

Slides:



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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
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.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
CS 201 Functions Debzani Deb.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
CS 201 Functions Debzani Deb.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Chapter 6: Functions.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions Why we use functions C library functions Creating our own functions.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
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 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
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.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
© 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.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
Introduction to Programming
C Functions -Continue…-.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
User Defined Functions
Chapter 6 - Functions Outline 5.1 Introduction
Chapter 9: Value-Returning Functions
Introduction to Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Programming
Programming Fundamental
Programming Fundamental
Presentation transcript:

Introduction to Programming Lecture 6

Functions – Call by value – Call by reference Today's Lecture Includes

Functions

Laboratory Stool

Constructing a laboratory Stool

Task: Making a stool – Subtask: Make a seat Make legs for the stool Assemble them

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

Function Function name { Body of the function }

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

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

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

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

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

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

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

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 ) ; } Example: Function to calculate integer power ( X n )

include 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 ) ; } Code to Call the raisetopow Function

Call By Value

Calling function Called function

Area of the Ring ____ Area of Inner Circle Area of Outer Circle = Area of the Ring

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

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

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

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 What we have studied so far?

#include Header Files

int functionName ( int, int ); Prototype Return value Assignment List with data type

double pi = ; Using Header Files It is better to define this value in a header file It is better to define this value in a header file Then simply by including the header file in the program this value is defined and it has a meaningful name Then simply by including the header file in the program this value is defined and it has a meaningful name

#define pi Name can be used inside a program exactly like a variable It cannot be used as a variable CircleArea = pi * radius * radius Circumference = 2 * pi * radius #define

Identifier is any name user creates in his/her program Functions are also identifiers Labels are also identifiers Scope of Identifiers

Scope means visibility A variable declared inside a block has visibility within that block only Variables defined within the function has a scope that is function wide Scope of Identifiers

Example void functionName ( ) { int i ; } ….. }

Do not create variables with same name inside blocks, inside functions or inside bigger blocks Try to use separate variable names to avoid confusion Reuse of variables is valid Identifiers Important Points

# include int i ; File Scope Global variable

Can be used anywhere in program Can cause logical problems if same variable name is used in local variable declarations For good programming Try to minimize the use of global variables Try to use local variables as far as possible Global Variable

Global Scope Anything identified or declared outside of any function is visible to all functions in that file Function level scope Declaring variables inside a function can be used in the whole function Block level scope Variables or integers declared inside block are used inside block Visibility of Identifiers

for ( int i = 0 ; i < 10 ; i++ ) It is block level scope declared in for loop When for is finished “ i ” no longer exists Example: Block Scope

#include int i ; void f ( void ) ; main ( ) { i = 10 ; cout<< “ within main i = “ << i ; f ( ) ; } Example: Global Scope

void f ( void ) { cout<< “ Inside function f, i =“ << i ; i = 20 ; } Example: Global Scope

#include int f ( int ) ; main ( ) { int i = 10 ; cout << “In main i = " << i ; f ( i ) ; cout << " Back in main, i = " << i ; } Example: Call by Value s

int f ( int i ) { cout << "In function f, i = " << i ; i *= 2 ; cout << "In function f, i is now = “ << i ; return i ; } Example: Call by Value

double square ( double x ) { return x * x ; } main ( ) { double number = ; cout << “ The square of “ << number << “ is “<< square ( number ) ; cout << “ The current value of “ << number << “is “ << number ; } Example : Square of a Number

#include double sqrt ( double ); log 10, pow ( x y ), sin, cos, tan … Math.h

Today we studied Functions Variable Scope