Functions BCA-105. Few Facts About Functions:
|
|
- Johnathan Hodges
- 5 years ago
- Views:
Transcription
1 Functions When programs become too large and complex and as a result the task of debugging, testing, and maintaining becomes difficult then C provides a most striking feature known as user defined function to solve this problem. If a program is divided into functional parts, then each part may be independently coded and later combined into a single unit. These independently coded programs are called sub programs that are much easier to understand, debug and test. In C these sub programs are referred to as functions. A function is a self contained block of statements that perform a coherent task of some kind. Or A function is a self contained block of code that is designed to perform a specific task. Once a function has been designed and packed then it can be treated as a separate entity/module/component or black box that takes some data from the main program and returns a value. The inner details of operation are invisible to the rest of the program. All that the program knows about a function is: What goes in and what comes out. Every C program is a collection of these functions Few Facts About Functions: 1. Functions can be either library functions or user defined functions. For example printf( ), scanf( ) are library functions. And user defined functions may be of any name like message( ), show( ) or etc. 2. There can be any number of functions in a program and any function can call any other function any number of times. 3. The order in which the function get called and the order in which they are defined in a program need not necessarily be same. 4. The use of user defined functions allows a large program to be broken down into a number of smaller self contained component/blocks, each of which has a definite purpose. Advantages of Functions: There are several advantages of using functions in a program like:- 1. Writing user defined functions avoids rewriting the same code again and again 2. Functions makes debugging or error rectification very easy 3. Functions make testing very easy. 4. Functions provide a main concept of reusability of same code again and again. 5. Functions divide a large program or problem into a number of small sub programs or sub problems User Defined Functions Page: 1 By Hemant Kumar
2 For example: here a small program is given below This program will print the following output: This illustrates the use of C functions The above program contains two user defined functions: main() function and printline() function Elements of User Defined Functions: In order to make use of a user defined functions, we need to establish three elements that are related to functions. 1. Function definition 2. Function call 3. Function declaration The function definition is an independent program module that is specially written to implement the requirements of the function. In order to use this function we need to invoke it at a required place in the program. This is known as function call. The program that calls the function is referred to as the calling function. The calling program should declare any function (like variable declaration) that is to be used later in the program. This is known as the function declaration or function prototype. User Defined Functions Page: 2 By Hemant Kumar
3 Elements of User Defined Functions: A function definition is also known as function implementation. It includes the following elements: 1. Function name 2. Function s return type 3. List of parameters 4. local variable declaration 5. Function statements (or function body or logical statement) 6. A return statement All the six elements are grouped into two parts Function header (first three elements) Function body (second three elements) A general format of a function definition to implement these two parts is given below: The first line Function_type function_name(parameter list) is known as function header. And the statement within the opening and closing braces constitute the function body, which is a compound statement. The parameters which are declared in function header are known as formal parameters. The parameters which are passed in function calling are known as actual parameters. Types of User Defined Functions: User defined functions are of the following types depending upon the parameters are passed or not and whether a value is returned or not. 1. Function with no arguments and no return values 2. Function with arguments and with no return values 3. Function with arguments and with one return value 4. Function with no arguments but return a value 5. Function that return multiple values. User Defined Functions Page: 3 By Hemant Kumar
4 Function with No arguments and No return type: When a function has no arguments, it does not receive any value or data from calling function. Similarly, when it does not return a value, the calling function does not receive any data from the called function. For example: The above function void printline(void) does not take any argument that is why its argument list is void. And does not return any value that is why its return type is void. Function with arguments but No return type: When a function takes some arguments, it receives values or data from calling function or from main function. When it does not return a value, the calling function does not receive any data from the called function. The parameters which are passed in function calling are known as actual parameters and the parameters which are declared in function definition are known as formal parameters. When a function call is made, these actual parameters and formal parameters must be matched in type, sequence, and number. For example: User Defined Functions Page: 4 By Hemant Kumar
5 The above function void printline(char ch) takes one argument that is why its argument list is char ch. And does not return any value that is why its return type is void. Some more examples are given Function with arguments with returning values: Functions with arguments and with returning values: receive input values or data from calling function (or from main function) and return the calculated value as output to the calling function (or to the main function) for the further processing. This concept uses the actual parameters in function calling and formal parameters in function definition and they must be equal in each type like types, numbers, and sequence. For example: Functions with No arguments But return values: The function which do not take any argument or parameter i.e. they don t take any input from main or calling function but they take input data directly from user or they use directly given values with the help of local variables and return the calculated or operated value as output to the calling function in main. Sometimes such functions are also known as utility functions because they provide some certain operated calculated value each time. A typical example of this type as we are discussing is getchar function which is declared in the header file <stdio.h>. The getchar User Defined Functions Page: 5 By Hemant Kumar
6 function has no parameters but it returns an integer type data that represents a character. For example review the following code segment: Functions with returning Multiple values (i.e. Call By Reference) All the above discussed functions are the functions which can return maximum one value only. This is because a return statement can return only one value at a time. But however we want to get more information from the function, we can achieve it using actual arguments not only to send the information to the function definition but also get back information from the function definition. The arguments that are sued to send out information are called output parameters. The mechanism of sending back information through arguments is achieved using what are known as the address operator (&) and indirection operator (*). For example: - The actual arguments x and y are input arguments, s and d are output arguments. In the function call, while we pass the actual values of x and y to the function we pass the address of locations or variables where the values of s and d are stored in the memory. (That is why, the indirection operator (*) is known as address operator). User Defined Functions Page: 6 By Hemant Kumar
7 The indirection operator * in the declaration of sum and diff in the header of function indicates these variables are to store addresses, not actual values of variables. Now, the variables sum and diff point to the memory locations of s and d respectively. The variables *sum and *diff are known as pointers and sum and diff as pointer variables. The use of pointer variables as actual parameters for communicating data between functions is called pass by pointer or call by address or call by reference. Rules for Call By Value (or Pass by Pointers) Nested Functions (or Nesting of Functions) C allows nesting of functions. Main function can call a function func1, which calls func2, which calls func3... and so on. There is in principle no limit as to how deeply functions can be nested. For example: User Defined Functions Page: 7 By Hemant Kumar
8 Recursion In C, it is possible for the functions to call themselves. A function is called recursive if a statement within the body of a function calls the same function. Sometimes it is called circular definition ; recursion is the process of defining something in terms of itself. Recursion is the process in which a function calls itself in it until a specific condition is satisfied or when a function calls to itself within its definition until a specific condition is satisfied this process is known as recursion and such functions are known as recursive functions. Assume that the number entered through scanf( ) is 3. Using figure give below visualizes what exactly happens when the recursive function rec( ) gets called. Go through the figure carefully. The first time when rec( ) is called from main( ), x collects 3. From here, since x is not equal to 1, the if block is skipped and rec( ) is called again with the argument ( x 1 ), i.e. 2. This is a recursive call. Since x is still not equal to 1, rec( ) is called yet another time, with argument (2-1). This time as x is 1, control goes back to previous rec( ) with the value 1, and f is evaluated as 2. User Defined Functions Page: 8 By Hemant Kumar
9 Similarly, each rec( ) evaluates its f from the returned value, and finally 6 is returned to main( ). The sequence would be grasped better by following the arrows shown in following Figure Let it be clear that while executing the program there do not exist so many copies of the function rec( ). These have been shown in the figure just to help you keep track of how the control flows during successive recursive calls. Recursion and Stack There are different ways in which data can be organized. For example, if you are to store five numbers then we can store them in five different variables, an array, a linked list, a binary tree, etc. All these different ways of organizing the data are known as data structures. The compiler uses one such data structure called stack for implementing normal as well as recursive function calls. A stack is a Last in First out (LIFO) data structure. This means that the last item to get stored on the stack (often called Push operation) is the first one to get out of it (often called as Pop operation). Now let us see how the stack works in case of the following program. User Defined Functions Page: 9 By Hemant Kumar
10 In this program before transferring the execution control to the function fun( ) the values of parameters a and b are pushed onto the stack. Following this the address of the statement printf( ) is pushed on the stack and the control is transferred to fun( ). It is necessary to push this address on the stack. In fun( ) the values of a and b that were pushed on the stack are referred as i and j. In fun( ) the local variable sum gets pushed on the stack. When value of sum is returned sum is popped up from the stack. Next the address of the statement where the control should be returned is popped up from the stack. Using this address the control returns to the printf( ) statement in main( ). Before execution of printf( ) begins the two integers that were earlier pushed on the stack are now popped off. User Defined Functions Page: 10 By Hemant Kumar
Unit 7. Functions. Need of User Defined Functions
Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have
More informationTechnical Questions. Q 1) What are the key features in C programming language?
Technical Questions Q 1) What are the key features in C programming language? Portability Platform independent language. Modularity Possibility to break down large programs into small modules. Flexibility
More informationUNIT 3 FUNCTIONS AND ARRAYS
UNIT 3 FUNCTIONS AND ARRAYS Functions Function definitions and Prototypes Calling Functions Accessing functions Passing arguments to a function - Storage Classes Scope rules Arrays Defining an array processing
More informationFunction Call Stack and Activation Records
71 Function Call Stack and Activation Records To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. Students
More informationUnit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.
1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific task. The functions which are created by programmer are
More information15 FUNCTIONS IN C 15.1 INTRODUCTION
15 FUNCTIONS IN C 15.1 INTRODUCTION In the earlier lessons we have already seen that C supports the use of library functions, which are used to carry out a number of commonly used operations or calculations.
More informationIt is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.
Functions A number of statements grouped into a single logical unit are called a function. The use of function makes programming easier since repeated statements can be grouped into functions. Splitting
More informationC Functions. CS 2060 Week 4. Prof. Jonathan Ventura
CS 2060 Week 4 1 Modularizing Programs Modularizing programs in C Writing custom functions Header files 2 Function Call Stack The function call stack Stack frames 3 Pass-by-value Pass-by-value and pass-by-reference
More informationCSE 230 Intermediate Programming in C and C++ Functions
CSE 230 Intermediate Programming in C and C++ Functions Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse230/ Concept of Functions
More informationCS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri
CS356: Discussion #6 Assembly Procedures and Arrays Marco Paolieri (paolieri@usc.edu) Procedures Functions are a key abstraction in software They break down a problem into subproblems. Reusable functionality:
More informationCS1150 Principles of Computer Science Methods
CS1150 Principles of Computer Science Methods Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Opening Problem Find the sum of integers from 1 to
More informationB.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University
Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are
More information& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to.
Computer Programming and Utilization (CPU) 110003 G) Functions 1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific
More informationCS113: Lecture 4. Topics: Functions. Function Activation Records
CS113: Lecture 4 Topics: Functions Function Activation Records 1 Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make
More informationChapter 7 Functions. Now consider a more advanced example:
Chapter 7 Functions 7.1 Chapter Overview Functions are logical groupings of code, a series of steps, that are given a name. Functions are especially useful when these series of steps will need to be done
More informationBSM540 Basics of C Language
BSM540 Basics of C Language Chapter 9: Functions I Prof. Manar Mohaisen Department of EEC Engineering Review of the Precedent Lecture Introduce the switch and goto statements Introduce the arrays in C
More informationPointers, Arrays and Parameters
Pointers, Arrays and Parameters This exercise is different from our usual exercises. You don t have so much a problem to solve by creating a program but rather some things to understand about the programming
More informationM.CS201 Programming language
Power Engineering School M.CS201 Programming language Lecture 4 Lecturer: Prof. Dr. T.Uranchimeg Agenda How a Function Works Function Prototype Structured Programming Local Variables Return value 2 Function
More informationAdvanced C Programming and Introduction to Data Structures
FYBCA Semester II (Advanced C Programming and Introduction to Data Structures) Question Bank Multiple Choice Questions Unit-1 1. Which operator is used with a pointer to access the value of the variable
More informationCSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU
CSE101-lec#12 Designing Structured Programs Introduction to Functions Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Designing structured programs in C: Counter-controlled repetition
More informationCSCI 2132 Software Development. Lecture 17: Functions and Recursion
CSCI 2132 Software Development Lecture 17: Functions and Recursion Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 15-Oct-2018 (17) CSCI 2132 1 Previous Lecture Example: binary
More informationProgramming for Engineers Introduction to C
Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines
More informationFunctions. Systems Programming Concepts
Functions Systems Programming Concepts Functions Simple Function Example Function Prototype and Declaration Math Library Functions Function Definition Header Files Random Number Generator Call by Value
More information5 Functions & Pointers
5 Functions & Pointers What is a Function Why Use Functions Passing Values between Functions Scope Rule of Functions Calling Convention One Dicey Issue Advanced Features of Functions Function Declaration
More informationLecture 05 I/O statements Printf, Scanf Simple statements, Compound statements
Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple
More informationC Programming for Engineers Functions
C Programming for Engineers Functions ICEN 360 Spring 2017 Prof. Dola Saha 1 Introduction Real world problems are larger, more complex Top down approach Modularize divide and control Easier to track smaller
More informationComputer Systems Lecture 9
Computer Systems Lecture 9 CPU Registers in x86 CPU status flags EFLAG: The Flag register holds the CPU status flags The status flags are separate bits in EFLAG where information on important conditions
More informationMODULE 5: Pointers, Preprocessor Directives and Data Structures
MODULE 5: Pointers, Preprocessor Directives and Data Structures 1. What is pointer? Explain with an example program. Solution: Pointer is a variable which contains the address of another variable. Two
More informationLecture 04 FUNCTIONS AND ARRAYS
Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions
More informationSoftware Development & Education Center C Programming
Software Development & Education Center C Programming Detailed Curriculum History of C Features of C Requirement from C development Turbo C 3 Turbo C 45 Borland C++ 502 Rules for C Programming Writing
More informationCS1150 Principles of Computer Science Methods
CS1150 Principles of Computer Science Methods Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Opening Problem Find the sum of integers from 1 to
More informationProgramming Studio #9 ECE 190
Programming Studio #9 ECE 190 Programming Studio #9 Concepts: Functions review 2D Arrays GDB Announcements EXAM 3 CONFLICT REQUESTS, ON COMPASS, DUE THIS MONDAY 5PM. NO EXTENSIONS, NO EXCEPTIONS. Functions
More informationFundamentals of Programming & Procedural Programming
Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Four: Functions: Built-in, Parameters and Arguments, Fruitful and Void Functions
More informationFunctions. Using Bloodshed Dev-C++ Heejin Park. Hanyang University
Functions Using Bloodshed Dev-C++ Heejin Park Hanyang University 2 Introduction Reviewing Functions ANSI C Function Prototyping Recursion Compiling Programs with Two or More Source Code Files Finding Addresses:
More informationLecture 3: C Programm
0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory
More informationCOMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1
COMP 202 Recursion CONTENTS: Recursion COMP 202 - Recursion 1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself COMP 202 - Recursion
More informationCSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions
CSE 2421: Systems I Low-Level Programming and Computer Organization Functions Read/Study: Reek Chapters 7 Gojko Babić 01-22-2018 Predefined Functions C comes with libraries of predefined functions E.g.:
More information1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \
BSc IT C Programming (2013-2017) Unit I Q1. What do you understand by type conversion? (2013) Q2. Why we need different data types? (2013) Q3 What is the output of the following (2013) main() Printf( %d,
More informationFunctions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011
Functions in C Lecture Topics Introduction to using functions in C Syntax Examples Memory allocation for variables Lecture materials Textbook 14.1-14.2, 12.5 Homework Machine problem MP3.2 due March 18,
More informationIECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.
Subject code - CCP01 Chapt Chapter 1 INTRODUCTION TO C 1. A group of software developed for certain purpose are referred as ---- a. Program b. Variable c. Software d. Data 2. Software is classified into
More informationFunctions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.
Functions In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create
More informationLab 3. Pointers Programming Lab (Using C) XU Silei
Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions
More informationC Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,
More informationCOMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others
COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is
More informationFunctions. Lecture 6 COP 3014 Spring February 11, 2018
Functions Lecture 6 COP 3014 Spring 2018 February 11, 2018 Functions A function is a reusable portion of a program, sometimes called a procedure or subroutine. Like a mini-program (or subprogram) in its
More informationLecture 9 - C Functions
ECET 264 C Programming Language with Applications Lecture 9 C Functions Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 9- Prof. Paul I. Lin
More informationLecture 04 FUNCTIONS AND ARRAYS
Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions
More informationFlow Control. CSC215 Lecture
Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements
More informationFunctions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.
Lab 4 Functions Introduction: A function : is a collection of statements that are grouped together to perform an operation. The following is its format: type name ( parameter1, parameter2,...) { statements
More informationA PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions.
FUNCTIONS IN C A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions. Big problems require big programs too big to be written
More informationAMCAT Automata Coding Sample Questions And Answers
1) Find the syntax error in the below code without modifying the logic. #include int main() float x = 1.1; switch (x) case 1: printf( Choice is 1 ); default: printf( Invalid choice ); return
More informationCode No: R Set No. 1
Code No: R05010106 Set No. 1 1. (a) Draw a Flowchart for the following The average score for 3 tests has to be greater than 80 for a candidate to qualify for the interview. Representing the conditional
More informationA Fast Review of C Essentials Part I
A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types
More informationC++ For Science and Engineering Lecture 15
C++ For Science and Engineering Lecture 15 John Chrispell Tulane University Wednesday September 29, 2010 Function Review Recall the basics you already know about functions. Provide a function definition.
More informationUnderstand Execution of a Program
Understand Execution of a Program Prof. Zhang September 17, 2014 1 Program in Memory When you execute a program, the program (i.e., executable code) is loaded into the memory (i.e., main memory, RAM) in
More informationFaculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions
Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2013 C++ Programming Language Lab # 6 Functions C++ Programming Language Lab # 6 Functions Objective: To be familiar with
More informationCS-201 Introduction to Programming with Java
CS-201 Introduction to Programming with Java California State University, Los Angeles Computer Science Department Lecture IX: Methods Introduction method: construct for grouping statements together to
More informationMODULE 3: Arrays, Functions and Strings
MODULE 3: Arrays, Functions and Strings Contents covered in this module I. Using an Array II. Functions in C III. Argument Passing IV. Functions and Program Structure, locations of functions V. Function
More informationCOMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS
COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS S.LAWRENCE CHRISTOPHER, M.C.A., B.Ed., LECTURER IN COMPUTER SCIENCE PONDICHERRY
More informationFundamentals of Programming. Lecture 3: Introduction to C Programming
Fundamentals of Programming Lecture 3: Introduction to C Programming Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department Outline A Simple C
More informationToday's Topics. CISC 458 Winter J.R. Cordy
Today's Topics Last Time Semantics - the meaning of program structures Stack model of expression evaluation, the Expression Stack (ES) Stack model of automatic storage, the Run Stack (RS) Today Managing
More informationFundamentals of Programming Session 4
Fundamentals of Programming Session 4 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2011 These slides are created using Deitel s slides, ( 1992-2010 by Pearson Education, Inc).
More informationWriting an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and
Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and #include The Use of printf() and scanf() The Use of printf()
More informationCS201 Some Important Definitions
CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data
More informationMore loops Ch
More loops Ch 3.3-3.4 Announcements Quiz next week! -Covers up to (and including) HW1 (week 1-3) -Topics: cout/cin, types, scope, if/else, etc. Review: Loops We put a loop around code that we want to run
More informationFunctions. Arash Rafiey. September 26, 2017
September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic
More informationLecture 5: Methods CS2301
Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int
More informationWhat Is a Function? Illustration of Program Flow
What Is a Function? A function is, a subprogram that can act on data and return a value Each function has its own name, and when that name is encountered, the execution of the program branches to the body
More informationQUIZ. What are 3 differences between C and C++ const variables?
QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,
More informationCHAPTER 4 FUNCTIONS. 4.1 Introduction
CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main
More informationChapter 5 C Functions
Chapter 5 C Functions Objectives of this chapter: To construct programs from small pieces called functions. Common math functions in math.h the C Standard Library. sin( ), cos( ), tan( ), atan( ), sqrt(
More informationCSCI 2132 Software Development. Lecture 18: Functions
CSCI 2132 Software Development Lecture 18: Functions Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 18-Oct-2017 (18) CSCI 2132 1 Previous Lecture Example: binary search Multidimensional
More informationPERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur
PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam-613 403 Thanjavur 01. Define program? 02. What is program development cycle? 03. What is a programming language? 04. Define algorithm? 05. What
More informationFundamentals of Programming
Fundamentals of Programming Pointers Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 1
More informationData Types. Data Types. Integer Types. Signed Integers
Data Types Data Types Dr. TGI Fernando 1 2 The fundamental building blocks of any programming language. What is a data type? A data type is a set of values and a set of operations define on these values.
More informationProgramming for Engineers Pointers
Programming for Engineers Pointers ICEN 200 Spring 2018 Prof. Dola Saha 1 Pointers Pointers are variables whose values are memory addresses. A variable name directly references a value, and a pointer indirectly
More informationFunctions in MIPS. Functions in MIPS 1
Functions in MIPS We ll talk about the 3 steps in handling function calls: 1. The program s flow of control must be changed. 2. Arguments and return values are passed back and forth. 3. Local variables
More informationQUIZ. Source:
QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Ch. 4: Data Abstraction The only way to get massive increases in productivity is to leverage off other people s code. That
More informationIV Unit Second Part STRUCTURES
STRUCTURES IV Unit Second Part Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure
More informationThere are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.
Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate
More informationHere's how you declare a function that returns a pointer to a character:
23 of 40 3/28/2013 10:35 PM Violets are blue Roses are red C has been around, But it is new to you! ANALYSIS: Lines 32 and 33 in main() prompt the user for the desired sort order. The value entered is
More informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
More informationInformatica e Sistemi in Tempo Reale
Informatica e Sistemi in Tempo Reale Puntatori Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 5, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction to C October
More informationb. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.
UNIT IV 1. Appropriately comment on the following declaration int a[20]; a. Array declaration b. array initialization c. pointer array declaration d. integer array of size 20 2. Appropriately comment on
More informationFunctions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
1 Functions Functions are everywhere in C Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Introduction Function A self-contained program segment that carries
More informationDecision Making -Branching. Class Incharge: S. Sasirekha
Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the
More informationAPS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:
APS105 Functions (and Pointers) Functions Tetbook Chapter5 1 2 Modularity Modularity Break a program into manageable parts (modules) Modules interoperate with each other Benefits of modularity: Divide-and-conquer:
More informationComputer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore
Sir Syed University of Engineering and Technology. Computer ming & Problem Solving ( CPPS ) Functions Chapter No 1 Compiled By: Sir Syed University of Engineering & Technology Computer Engineering Department
More informationAN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University
AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:
More informationFunctions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa
Functions Autumn Semester 2009 Programming and Data Structure 1 Courtsey: University of Pittsburgh-CSD-Khalifa Introduction Function A self-contained program segment that carries out some specific, well-defined
More informationCopyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented
More informationIntroduction to Programming (Java) 4/12
Introduction to Programming (Java) 4/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction
More informationCS536 Spring 2011 FINAL ID: Page 2 of 11
CS536 Spring 2011 FINAL ID: Page 2 of 11 Question 2. (30 POINTS) Consider adding forward function declarations to the Little language. A forward function declaration is a function header (including its
More informationC-LANGUAGE CURRICULAM
C-LANGUAGE CURRICULAM Duration: 2 Months. 1. Introducing C 1.1 History of C Origin Standardization C-Based Languages 1.2 Strengths and Weaknesses Of C Strengths Weaknesses Effective Use of C 2. C Fundamentals
More informationComputer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];
Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data
More informationTo declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows
Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful
More informationLecture 5 C Programming Language
Lecture 5 C Programming Language Summary of Lecture 5 Pointers Pointers and Arrays Function arguments Dynamic memory allocation Pointers to functions 2D arrays Addresses and Pointers Every object in the
More informationPROGRAMMAZIONE I A.A. 2017/2018
PROGRAMMAZIONE I A.A. 2017/2018 FUNCTIONS INTRODUCTION AND MAIN All the instructions of a C program are contained in functions. üc is a procedural language üeach function performs a certain task A special
More informationsends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;
INPUT AND OUTPUT IN C Function: printf() library: sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ; format_string it is
More informationMPATE-GE 2618: C Programming for Music Technology. Unit 4.1
MPATE-GE 2618: C Programming for Music Technology Unit 4.1 Memory Memory in the computer can be thought of as a long string of consecutive bytes. Each byte has a corresponding address. When we declare
More information