Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming

Similar documents
Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

CSE123. Program Design and Modular Programming Functions 1-1

AMCAT Automata Coding Sample Questions And Answers

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

Holtek C and ANSI C Feature Comparison User s Guide

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

ECE 2400 Computer Systems Programming Fall 2017 Topic 12: Transition from C to C++

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

ECET 264 C Programming Language with Applications

The C Language Reference Manual

C Functions Pearson Education, Inc. All rights reserved.

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #05

Chapter 4 Functions By C.K. Liang

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

High-performance computing and programming I. Introduction to C programming in *NIX

Subject: Fundamental of Computer Programming 2068

Programming in C. Part 1: Introduction

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Functions in C C Programming and Software Tools

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Chapter 5 C Functions

Programming and Data Structure

Programming in C Quick Start! Biostatistics 615 Lecture 4

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

CSE2301. Functions. Functions and Compiler Directives

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

C Libraries. Bart Childs Complementary to the text(s)

ECE 2400 Computer Systems Programming Fall 2018 Topic 11: Transition to C++

BİL200 TUTORIAL-EXERCISES Objective:

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

COMP 208 Computers in Engineering

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Functions. Arash Rafiey. September 26, 2017

Multiple Choice Questions ( 1 mark)

Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Lecture 04 FUNCTIONS AND ARRAYS

EPITA SPE C Workshop - D0

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Technical Questions. Q 1) What are the key features in C programming language?

C Functions. 5.2 Program Modules in C

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Introduction to C Programming (Part A) Copyright 2008 W. W. Norton & Company. All rights Reserved


Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Computer Programming

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

The C standard library

Computer Science & Engineering 150A Problem Solving Using Computers

UNIT 3 FUNCTIONS AND ARRAYS

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

The Hyderabad Public School, Begumpet, Hyderabad, A.P

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Make sure the version number is marked on your scantron sheet. This is Version 1

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

OBJECTIVE QUESTIONS: Choose the correct alternative:

LAB 6 FUNCTION PART 1 School of Computer and Communication Engineering Universiti Malaysia Perlis

Real Time Embedded Systems. Lecture 5 January 24, 2012 Embedded C Programming

BSM540 Basics of C Language

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Practice Sheet #07 with Solutions

Lecture 9 - C Functions

Functions. Systems Programming Concepts

Programming in C. UVic SEng 265. Daniel M. German Department of Computer Science University of Victoria. October 23, 2002 Version: 1.

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Assoc. Prof. Dr. Tansu FİLİK

Introduction to Programming Systems

Chapter 1 Fundamentals and History of C

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

C: How to Program. Week /Apr/23

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England. and Associated Companies throughout the world

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

ESC101N: Fundamentals of Computing End-sem st semester

SUHAIL T A

C Programming Multiple. Choice

Part II Answer to all the questions (2 Marks):

Computer Programming

Tutorial No. 2 - Solution (Overview of C)

CS16 Exam #1 7/17/ Minutes 100 Points total

Pointers and Arrays 1

Question Bank (SPA SEM II)

ME 172. C Programming Language Sessional Lecture 8

Functions and Recursion

Functions BCA-105. Few Facts About Functions:

Transcription:

PDS Lab Section 16 Autumn-2018 Tutorial 5 Functions The C language is termed as function-oriented programming Every C program consists of one or more functions. The concept is based on the divide-and conquer policy. A large program can be decomposed into a number of relatively smaller segments Easy to code, debug, maintain, etc. In C, there is no limit on the number of such functions in a program. In C programs, any function can call any other function, as many time as it may be. One of these functions, there shall be one must be called main. Execution of a program always begins by carrying out the instructions in main.

Functions in C A function is a block of code that performs a set of pre-defined commands to produce desired result. Example 1: #include <stdio.h> int factorial (int m) int i, temp=1; for (i=1; i<=m; i++) temp = temp * i; return (temp); main() int n; for (n=1; n<=10; n++) printf ( %d! = %d \n, n, factorial (n) ); Functions definition (prototype) includes basic structural information: it tells the compiler what the function will return, how the function will be called, as well as the arguments that can be can be passed.

Built-in functions <assert.h> <float.h> <math.h> <stdarg.h> <stdlib.h> <ctype.h> <limits.h> <setjmp.h> <stddef.h> <string.h> <errno.h> <locale.h> <signal.h> <stdio.h> <time.h> Example 2: printf(), scanf(), rand(), getchar(), etc. Note: A function returns a value of predefined type.

User Defined Functions Way 1 Usually, a function is defined before it is called. main() is the last function in the program. Easy for the compiler to identify function definitions in a single scan through the file. Way 2 However, many programmers prefer a top-down approach, where the functions follow main(). Must be some way to tell the compiler. Function prototypes are used for this purpose. Only needed if function definition comes after use. Example 3:

Example 4: #include<stdio.h>.. <function declaration>;.. main( )...... <function definition>... D. Samanta,. IIT

Example 5: return-value-type function-name ( parameter-list ) declarations statements return ( ); Example 6: # include <stdio.h> float eval_quad_poly( float a, float b, float c); int main() float A,B,C,X, Z; int numb_x_val =1; printf ( Enter the coefficients of the polynomial ); scanf( %f %f %f,&a, &B,&C); printf ( A= %f, B=%f, C =%f\n, A,B,C); //Evaluate the quadratic polynomial for 10 values of X for (numb_x_val=1; numb_x_val <=10; ++numb_x_val) return 0; printf ( Enter value of X ); scanf( %f,&x); Z = eval_quad_poly(a,b,c); printf( For X = %f Z= %f \n, X, Z); //calling the user defined function

float eval_quad_poly( float a, float b, float c) //Defining the user defined function to evaluate a quadratic polynomial a* x^2 + b*x +c float y; y= a*(x*x) +b*x +c; return (y);

Nested Functions A function cannot be defined within another function. All function definitions must be disjoint. Nested function calls are allowed. A calls B, B calls C, C calls D, etc. The function called last will be the first to return. A function can also call itself, either directly or in a cycle. A calls B, B calls C, C calls back A. Called recursive call or recursion. Example 7: #include <stdio.h> int ncr (int n, int r); int fact (int n); main() int i, m, n, sum=0; scanf ( %d %d, &m, &n); for (i=1; i<=m; i+=2) sum = sum + ncr(n, i); printf ( Result: %d \n, sum); int ncr (int n, int r) return(fact(nfac(r)/fact(n-r)); int fact (int n) int i, temp=1; for (i=1; i<=n; i++) temp *= i; return (temp);

Example 8: #include <stdio.h> #include <stdlib.h> #include <time.h> void main() int i; time_t t; unsigned seed; /* Initialize random number generator */ srand((unsigned)time(&t)); /* Print 10 random numbers between 1 and 6 */ for(i=1;i<=10;i++) printf( %d,1+rand()%6); if (i%5 == 0)printf( \n ); return 0;

Tutorial Problems Problem 1 Determine what each of the following foomatic functions computes: 1. -------------------------------------------------------- unsigned int foo1 ( unsigned int n ) unsigned int t = 0; while (n > 0) if (n % 2 == 1) ++t; n = n / 2; return t; 2. -------------------------------------------------------- unsigned int foo2 ( unsigned int n ) unsigned int t = 0; while (n > 0) if (n & 1) ++t; n >>= 1; return t;

3. -------------------------------------------------------- double foo3 ( double a, unsigned int n ) double s, t; s = 0; t = 1; while (n > 0) s += t; t *= a; --n; return s; 4. -------------------------------------------------------- double foo4 ( float A[], int n ) float s, t; s = t = 0; for (i=0; i<n; ++i) s += A[i]; t += A[i] * A[i]; return (t/n)-(s/n)*(s/n);

Problem 2 A set of number is given. Write a function to find the minimum number form the set of numbers. #include <stdio.h> int x[100]; int size; int minimum() int i, min = 99999; for (i=0; i<size; i++) if (min > x[i]) min = x[i]; return (min); void main() int i; scanf ( %d, &size); for (i=0; i<size; i++) scanf ( %d, &x[i]); printf( \n Minimum is %d,minimum());

Problem 3 What this main() function does? #include <stdio.h> #define PI 3.1415926 main() float r = 4.0, area; area = PI*r*r; #define sqr(x) x*x r = sqr(a) + sqr(30); // r = a*a + 30*30; r = sqr(2+5); // r = 2+5*2+5; #define sqr(x) (x)*(x) r = sqr(a+b); r = (a+b)*(a+b);

Problem 4 Which of the following function definitions are invalid? Why? a) average(x, y, z); b) sqrt(int n); c) int rand(); d) power (int x, int n) e) double minimum(float x; float y); Problem 5 The following is the function prototype to retune the value of x/y. double divide(float x, float y) return (x/y); What will be the value of the following function calls? a) divide (10, 2); b) divide (4.5, 1.0); c) divide (1, 0);

Problem 6 Determine the output of the following program. #include <stdio.h> int p, q; int product(int i, int j); void main () int x = 10, y = 20; p = product(x, y); q = product(p, product(x,2)); printf( %d %d \n, p, q); int product(int a, int b) return(a*b); Problem 7 What will be the output of the following programs given that s = d%samanta and s = Debasis Samanta? printf(s); printf( %s, s);

Important links: http://cse.iitkgp.ac.in/~dsamanta/courses/pds/index.html