CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.

Slides:



Advertisements
Similar presentations
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)
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
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.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Chapter 6: Functions.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Functions g g Data Flow g Scope local global part 4 part 4.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
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.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
GE 211 Function in C++ Dr. Ahmed Telba Math Library Functions Defining a Function: The general form of a C++ function definition is as follows:
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 12.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
C++ Programming Lecture 12 Functions – Part IV
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Functions + Overloading + Scope
-Neelima Singh PGT(CS) KV Sec-3 Rohini
CSC1201: Programming Language 2
Functions and an Introduction to Recursion
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Functions.
CSC1201: Programming Language 2
CS1201: Programming Language 2
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
CS1201: Programming Language 2
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
The Function Prototype
CS1201: Programming Language 2
Functions and an Introduction to Recursion
CS1201: Programming Language 2
CSC1201: Programming Language 2
CSC1201: Programming Language 2
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
Programming Fundamental
Functions Chapter No. 5.
C++ PROGRAMMING SKILLS Part 3 User-Defined Functions
Presentation transcript:

CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif

Global VS Local variables local variable ▫ Variables that declared inside a function ▫ Declared inside any block of code Global variables ▫ Opposite of local the known throughout the entire program

Global VS Local variables #include Using namespace std;

Global VS Local variables #include Using namespace std; void main() { int i=4; int j=10; i++; if (j > 0) cout<<“ i is “<<i<<endl; if (j > 0) { int i=100; /* 'i' is defined and so local to * this block */ Cout << "i is” << i; } //end if cout<<"i is “<<i; } //end main I is 5 I is 100 I is 5

Function reusable The function are reusable. ProceduresRoutines Methods functions may be called Procedures or Routines and in object oriented programming called Methods. Save programmers’ time.

Prototype VS. Declaration prototype : n ormally placed before the start of main() but must be before the function definition. General form : function_return_type function_name (type parameter1, type parameter2,…, type parameterN) ; EX: void Factorial (int x); // prototype -- x is a parameter

Prototype VS. Declaration Prototype : void foo(int x); // prototype -- x is a parameter Declaration void foo(int x) // declaration -- x is a parameter { Statement. }

EX: #include using namespace std; int square (int x)‏ { return x*x; } Void main ( )‏ { int number; cout<<"Enter any number to Calculate the square of this number "; cin>>number; cout<<endl; cout<<"the square of "<<number<<" is " <<square(number)<<endl; }

EX: #include using namespace std; int square (int x)‏ ; Void main ( )‏ { int number; cout<<"Enter any number to Calculate the square of this number "; cin>>number; cout<<endl; cout<<"the square of "<<number<<" is " <<square(number)<<endl; } int square (int x)‏ {return x*x;}

Finding Errors in Function Code int sum(int x, int y) { int result; result = x+y; }  this function must return an integer value as indicated in the header definition (return result;) should be added  this function must return an integer value as indicated in the header definition (return result;) should be added

Finding Errors in Function Code int sum (int n) { if (n==0) return 0; else n+(n-1); }  the result of n+sum(n-1) is not returned; sum returns an improper result, the else part should be written as:- else return n+(n-1);  the result of n+sum(n-1) is not returned; sum returns an improper result, the else part should be written as:- else return n+(n-1);

Finding Errors in Function Code void f(float a); { float a; cout<<a<<endl; }  ; found after function definition header.  redefining the parameter a in the function void f(float a) { float a2 = a + 8.9; cout <<a2<<endl; }  ; found after function definition header.  redefining the parameter a in the function void f(float a) { float a2 = a + 8.9; cout <<a2<<endl; }

Finding Errors in Function Code void product(void) { int a, b, c, result; cout << “enter three integers:”; cin >> a >> b >> c; result = a*b*c; cout << “Result is” << result; return result; }  According to the definition it should not return a value, but in the block (body) it did & this is WRONG.  Remove return Result;  According to the definition it should not return a value, but in the block (body) it did & this is WRONG.  Remove return Result;

Parameters vs Arguments Up until now, we have not differentiated between function parameters and arguments. In common usage, the terms parameter and argument are often interchanged. ▫A function parameter is a variable declared in the prototype or declaration of ▫An argument is the value that is passed to the function in place of a parameter.

methods of passing There are 2 primary methods of passing arguments to functions: ▫pass by value, ▫pass by reference,

Passing arguments by value By default, arguments in C++ are passed by value. When arguments are passed by value, a copy of the argument is passed to the function. void foo(int y) { cout << "y = " << y << endl; } int main() { foo(5); // first call int x = 6; foo(x); // second call foo(x+1); // third call return 0; }

Passing arguments by value Because a copy of the argument is passed to the function, the original argument can not be modified by the function. 1.void foo(int y) 2.{ 3. cout << "y = " << y << endl; y = 6; cout << "y = " << y << endl; 8.} // y is destroyed here int main() 11.{ 12. using namespace std; 13. int x = 5; 14. cout << "x = " << x << endl; foo(x); cout << "x = " << x << endl; 19. return 0; 20.}

Adv. And DisAdv. Of Passing by value AdvantagesDisadvantages ▫Arguments passed by value can be:  variables (eg. x),  literals (eg. 6),  or expressions (eg. x+1) or (eg foo()). ▫Arguments are never changed by the function being called, which prevents side effects. Copying large structs or classes can take a lot of time to copy, and this can cause a performance penalty, especially if the function is called many times.