Modular C Programming & Function #include printNewLine() { printf(“\n”); } main() { printf(“C programming is easy”); printNewLine(); printf(“But, you still.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

C Language.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Passing Arguments Question Example: IS1102 Exam Autumn 2001.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
CS102 Introduction to Computer Programming Chapter 6 Functions Continued.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
User defined functions
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
1 ICS103 Programming in C Lecture 8: Functions I.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Computer Programming for Engineers
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
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.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
C Language By Sra Sontisirikit
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Functions.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Lab 1 Introduction to C++.
Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 8 The Loops By: Mr. Baha Hanene.
Functions, Part 1 of 3 Topics Using Predefined Functions
Fundamental Programming
Functions Extra Examples.
COMPUTER PROGRAMMING SKILLS
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Standard Version of Starting Out with C++, 4th Edition
ICS103: Programming in C 6: Pointers and Modular Programming
Presentation transcript:

Modular C Programming & Function #include printNewLine() { printf(“\n”); } main() { printf(“C programming is easy”); printNewLine(); printf(“But, you still fail!”); } C programming is easy But, you still fail! Guess what the output is We have used a function in the program. The function’s name is printNewLine(). This function serves to display a new line.

Modular C Programming & Function #include printNewLine() { printf(“\n”); } main() { printf(“C programming is easy”); printNewLine(); printf(“But, you still fail!”); } During the execution, the program starts in main function. When reach statement printNewLine(), the statement(s) defined within the function will be executed. When it finishes, it will return back to the main function and execute the next statement.

Modular C Programming & Function #include printNewLine() { printf(“\n”); } main() { printf(“C programming is easy”); printNewLine(); printf(“But, you still fail!”); } Terminology Function Definition or Function Implementation Function call

Modular C Programming & Function # include printSquare() { printf(“**\n”); } printTriangle() { printf(“ *\n”); printf(“***\n”); } main() { printTriangle(); printSquare(); printTriangle(); } * *** ** * *** Exercise: What’s the output?

Modular C Programming & Function Game Menu 1 – Red Alert 2 – War Craft Enter your choice: 1 Your CMPB164 already Red Alert Play next time when it is no longer Red Alert Exercise: What’s the output if user input 1? What’s the output if user input 2? Game Menu 1 – Red Alert 2 – War Craft Enter your choice: 2 War already started in Palestine You no longer need a simulated one What if user input 3? Game Menu 1 – Red Alert 2 – War Craft Enter your choice: 3 Can you read English?

Modular C Programming & Function #include int add_Numbers(int num1, int num2) { return num1+num2; } main() { int result; result = add_Numbers(1, 2) printf(“Value returned by the function is %d “, result); } Value return by the function is 3 Guess what the output is

Modular C Programming & Function int add_Numbers(int num1, int num2) { return num1+num2; } This function has 2 formal parameters – variable num1 of type int variable num2 of type int This function is expected to return a value of type int. In this case it return the sum of num1 and num2. Formal parameters of a function are variables declared in the format parameter list within the brackets ( ) right after the function’s name. They are used to send values to the function, return values from it, or both.

Modular C Programming & Function main() { int result; result = add_Numbers(1, 2) printf(“Value returned by the function is %d “, result); } When the main function calls add_Numbers(), it passes value 1 and 2 through num1 and num2 respectively into the function. add_Numbers() then return the sum of num1 and num2 to its caller – main function and this value is then assigned to result

Modular C Programming & Function In general, function defination or implementation has the following format ReturnDataType FunctionName (FormalParameterList) { CompoundStatements } Formal parameters of a function are variables declared in the format parameter list within the brackets ( ) right after the function’s name. They are used to send values to the function, return values from it, or both. Return Data Type specifies the type of the data which the function shall return back to the caller. Both of these features serve to establish data communication between a function and its caller.

Modular C Programming & Function #include int square(int num) { return num*num; } main() { int result; result = square(4); printf(“Square of 4 is %d “, result); } Square of 4 is 16 Guess what the output is

Modular C Programming & Function #include int power(int base, int exponent) { int i, temp; temp = 1; for (i=1; i<=exponent; i++) temp *= base; return temp; } main() { int result; result = power(2, 8); printf(“2 to the power of 8 is %d “, result); } 2 to the power of 8 is 256 Guess what the output is

Modular C Programming & Function #include printSymbol(char symbol, int count) { int i; for (i=1; i<=count; i++) printf(“%c”,symbol); } main() { int i; for (i=1; i<=3; i++) { printSymbol(‘*’, i); printSymbol(‘\n’, 1); } * ** *** Guess what the output is This function print the specified character for count times

Modular C Programming & Function #include printSymbol(char symbol, int count) { int i; for (i=1; i<=count; i++) printf(“%c”,symbol); } printRectangle (char symbol, int height, int width) { int h; for (h=1; h<=height; h++) printSymbol(symbol, width); } main() { printRectangle(‘#’, 3, 4); } #### Guess what the output is