CSC1201: Programming Language 2

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Introduction to C++ Programming
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Chapter 3 Getting Started with C++
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
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.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
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.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Basic concepts of C++ Presented by Prof. Satyajit De
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Functions + Overloading + Scope
Programming what is C++
Review 1.
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
CSC1201: Programming Language 2
Function Topic 4.
Introduction to C++ Systems Programming.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Dr. Shady Yehia Elmashad
User-Defined Functions
Dr. Shady Yehia Elmashad
Lab 1 Introduction to C++.
CSC1201: Programming Language 2
User Defined Functions
Value returning Functions
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.
Name: Rubaisha Rajpoot
Lab 1 Introduction to C++.
FUNCTION CSC128.
Chapter 6: User-Defined Functions I
CSC1201: Programming Language 2
Functions Imran Rashid CTO at ManiWeber Technologies.
Single-Result Functions & Modularity
CS1201: Programming Language 2
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
CPS125.
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

CSC1201: Programming Language 2 Functions I

– Organize code in program – Code are easier to maintain? A Function is a group of statements that together perform a task. It can be used anywhere in the program. Why we need function? – Organize code in program – Code are easier to maintain? Function declaration: return_type FuncName(Type arg1, Type arg2,….. Type argN) { function body } A program can contain one or many functions Must always have a function called “main”. The main function is the starting point of all C++ programs The compiler will not compile the code unless it finds a function called “main” within the program.

“Hello World” program #include <iostream> using namespace std; int main ()‏ { cout << “Hello World\n”; Return 0; }

Function When we need function? – When you need to repeat the same process over and over in a program. – The function can be called many times but appears in the code once.

1- Predefined functions Predefined functions are functions that are built into C++ Language to perform some standard operations. The C++ standard library provides numerous built-in functions that your program can call. For example, function strcat() to concatenate two strings the definitions have been written and it is ready to be used. – User needs to include pre-defined header file (i.e. math.h, time.h)

2- User defined functions Function that been created by the user. – This functions need to be declared and defined by the user

functions Value returning functions: Void functions: functions that have a return type. These functions return a value of a specific data type using the return statement. Void functions: functions that do not have a return type. These functions do not use a return statement to return a value.

Value returning functions The syntax is: FuncType FuncName(formal parameter list )‏ { statements }

Void functions The syntax is: Void FuncName ( formal parameter list )‏ { statements }

Examples: Write a Function larger, which returns the larger of the two given integers. Write a Function Square, which returns the square of the given integer. Write a function number_type. The function should output the number and message saying whether the number is positive, negative, or zero.

Example: With return value Double larger ( double x , double y )‏ { double max; if ( x >= y )‏ max = x; else max = y; return max; } Function Call …….. Cout << “The larger of 5 and 6 is “ << larger(5 , 6) << endl; ………. Solution Ex 1

Example: With return value #include<iostream> using std::cin; using std::cout; using std::endl; int square (int x)‏ { return x*x; } int 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; return 0; Solution Ex 2

Example: Without return value Void number_type ( int x)‏ { if ( x > 0 )‏ cout << x << “ is positive.” << endl; else if ( x < 0 )‏ cout << x << “ is negative.” << endl; else cout<< x << “is a zero.”<<endl; } Function Call …….. Number_type( 5 ); ………. Solution Ex 3