Introduction to Algorithms and Programming COMP151

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Parameter Passing Mechanisms Reference Parameters.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Chapter 5 Functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
Functions Pass by Value Pass by Reference IC 210.
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
1 CS161 Introduction to Computer Science Topic #10.
Functions. Let’s look at the 2 programs of evaluating: y=2^3+2^5+2^6; #include using namespace std; int main() { int y=0; int partResult=1; for (int i=1;
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Instructor - C. BoyleFall Semester
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
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.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Fucntions in C++ Malik Jahan Khan
Chapter 6: User-Defined Functions I
Functions in C ++ Subject: Programming Languages ​​for III year Topic: functions, member class.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Class and Objects UNIT II.
FUNCTIONS.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Writing Reuseable Formulas
Addis Ababa Institute of Technology Yared Semu May 2012
Reference parameters (6.2.3)
Extra.
CSC1201: Programming Language 2
CS150 Introduction to Computer Science 1
Pointers & Functions.
Anatomy of a Function Part 1
FUNCTION CSC128.
Introduction to Algorithms and Programming COMP151
Dr. Khizar Hayat Associate Prof. of Computer Science
Chapter 6: User-Defined Functions I
CS149D Elements of Computer Science
Pointers and Pointer-Based Strings
CSC1201: Programming Language 2
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CSC1201: Programming Language 2
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
Introduction to Algorithms and Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
Dr. Khizar Hayat Associate Prof. of Computer Science
CS1201: Programming Language 2
Anatomy of a Function Part 1
Introduction to Algorithms and Programming COMP151
Introduction to Functions
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Introduction to Algorithms and Programming COMP151
Presentation transcript:

Introduction to Algorithms and Programming COMP151 LAB 9 FUNCTIONS Introduction to Algorithms and Programming COMP151

User Defined Functions Example program for Simple Function (no arguments) #include<iostream> using namespace std; void starline(); //Function Declaration (or) Prototype int main() { cout<<"Program for Function \n"; starline(); //Call to Function starline cout<<"This is the simple Program\n "; cout<<"Printing Star line to decorate the output\n"; return(0); } void starline() //Function definition int j; for(j=0;j<45;j++) cout<<"*"; cout<<endl;

Passing Parameters (or) Arguments to Functions When a function is called, the program may send values into the function. Values that are passed into a function are called actual arguments (or) actual parameter. Two ways to pass arguments to functions Pass-by-Value 2. Pass-by-Reference

Pass-by-Value

Example: swapping-pass by value #include<iostream> using namespace std; void swap(int,int); int main() { int a,b; cout<<"Enter the value for a"<<endl; cin>>a; cout<<"Enter the value for b"<<endl; cin>>b; swap(a,b); return(0); } void swap(int c,int d) int temp; temp=c; c=d; d=temp; cout<<"Value of c after swapping"<<c<<endl; cout<<"Value of d after swapping"<<d<<endl;

2. Pass-by-Reference A reference variable that used as a formal argument allows access to the original values of actual arguments. A reference variable is an alias for another variable. Any changes made to the reference variable are actually performed on the variable for which it is an alias. Reference variables are declared like regular variables, except you have to place an ampersand (&) symbol in front of the name.

Example: swapping-pass by reference #include<iostream> using namespace std; void swap(int&,int&); int main() { int a,b; cout<<"Enter the value for a"<<endl; cin>>a; cout<<"Enter the value for b"<<endl; cin>>b; swap(a,b); cout<<"Value of a after swapping"<<a<<endl; cout<<"Value of b after swapping"<<b<<endl; return(0); } void swap(int &c,int &d) int temp; temp=c; c=d; d=temp;

Library Functions (or) Predefined Functions

Exercise 1 Write C++ program to find x to the power y using the predefined function pow

Exercise 2 Write C++ program to find the square root of a given number using the predefined function sqrt