BSM540 Basics of C Language

Similar documents
BSM540 Basics of C Language

C Programming Language

BSM540 Basics of C Language

C Programming Language

C Programming Language

Lecture 9 - C Functions

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

C Programming Language

CSCI 2132 Software Development. Lecture 18: Functions

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

C Programming Lecture V

Chapter 7. Basic Types

Functions. Arash Rafiey. September 26, 2017

Lecture 3. More About C

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

ET156 Introduction to C Programming

Functions BCA-105. Few Facts About Functions:

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Subject: Fundamental of Computer Programming 2068

CS113: Lecture 4. Topics: Functions. Function Activation Records

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

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

C Programming Language

Calling Prewritten Functions in C

AMCAT Automata Coding Sample Questions And Answers

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

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

C: How to Program. Week /Mar/05

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

ET156 Introduction to C Programming

Unit 7. Functions. Need of User Defined Functions

Multiple Choice Questions ( 1 mark)

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Fundamentals of Programming & Procedural Programming

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

C-LANGUAGE CURRICULAM

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

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

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

Conditional Expressions

CS240: Programming in C

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Programming in C. Session 2. Seema Sirpal Delhi University Computer Centre

C: How to Program. Week /Apr/23

Lab 3. Pointers Programming Lab (Using C) XU Silei

DECLARAING AND INITIALIZING POINTERS

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Chapter 5 C Functions

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Introduction to the C Programming Language

Storage class and Scope:

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

EL2310 Scientific Programming

Computer Programming: Skills & Concepts (CP) Variables and ints

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

COP 3275: Chapter 07. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Programming Language B

struct structure_name { //Statements };

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

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

ECET 264 C Programming Language with Applications. C Program Control

2. Numbers In, Numbers Out

Arrays and Pointers. Lecture Plan.

Programming Language B

Data Type Fall 2014 Jinkyu Jeong

EC 413 Computer Organization

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

Programming for Engineers Functions

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Chapter 8: Function. In this chapter, you will learn about

Chapter 2 - Introduction to C Programming

C Programming for Engineers Functions

Engineering 12 - Spring, 1999

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

User Defined Functions

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

CC112 Structured Programming

Lecture 04 FUNCTIONS AND ARRAYS

OBJECTIVE QUESTIONS: Choose the correct alternative:

Introduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department

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

C Program Reviews 2-12

Programming for Engineers Introduction to C


Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Chapter 4. Section 4.4 Passing Parameters to Functions. CS 50 Hathairat Rattanasook

C Functions. 5.2 Program Modules in C

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Fundamentals of Programming Session 8

Transcription:

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 language Introduce the rand() and srand() functions Keywords 2

Lecture Objectives Introduce the functions in C language 3

Functions What is a function? It is a self-contained unit of program code designed to accomplish a particular task. Examples printf(), scanf(), sizeof(), rand(), srand(), getchar(), putchar(), pow() Can we create our own functions? Yes, Sure! Why do we need functions? Make you code organized Make your code reusable Make your code easily debuggable Make your code shorter by simply calling a function several times Avoid repetition 4

Functions contd. Again what is a function? It is a black box that has a name, input(s), output(s) or without, and type A function can be without inputs or outputs or none of them Function type Example It can be any variable type, void, or a pointer to a variable int max(int x, int y); x y max (x > y)? x : y; inputs the function max output 5

Example 1: A void function Functions contd. # include <stdio.h> void main(){ int ii; char name[20]; 1 2 printf( Please Please, enter your name: ); scanf( %s, name); for(ii = 0; ii < 20; ii++) putchar( - ); printf( \n ); printf( your name is %s\n, name); for(ii = 0; ii < 10; ii++) putchar( - ); printf( \n ); Output Please, enter your name: Huey -------------------- Your name is Huey -------------------- // a void function # include <stdio.h> // prototype (function declaration) void dash(); void main(){ char name[20]; printf( Please, enter your name: ); scanf( %s, name); dash(); // function call printf( your name is %s\n, name); dash(); // function call void dash() // function definition { int ii = 0; for(ii = 0; ii < 20; ii++) putchar( - ); printf( \n ); 6

Functions contd. Example 1 What is new? Function declaration Tells the compiler the function s name (example: dash) Tells what type of function is dash() What arguments does it take and what outputs does it return, if any. Example: void dash(); The function s name is dash It is of type void (does not have outputs), and does not have inputs Function call Causes the function to be executed. Function definition Specifies what the function does. // a void function # include <stdio.h> // prototype (function declaration) void dash(); void main(){ char name[20]; printf( Please, enter your name: ); scanf( %s, name); dash(); // function call printf( your name is %s\n, name); dash(); // function call void dash() // function definition iti { int ii = 0; for(ii = 0; ii < 10; ii++) putchar( - ); printf( \n ); 7

Functions contd. Example 2: A function with integer output and input Find the sum of integers from 1 up to an integer input by the user. # include <stdio.h> // prototype (function declaration) int sum(int); // same as - int sum(int a); - void main(){ int limit = 0, out = 0; printf("enter an integer: "); scanf("%d", &limit); out = sum(limit); // function call printf("the sum from 1 to %d = %d\n", limit, out); int sum(int count){ // function definition i i int ii = 0, result = 0; result count = ii ii = 1 for(ii = 1; ii <= count; ii++) result += ii; return result; Output Enter an integer: 5 The sum from 1 to 5 = 15 8

Functions contd. Example 2: What is new? Arguments are placed between the parentheses int sum(int count); The function has one argument of type integer (count) The function returns an integer (function s type) we must use the return statement void main(void) The function does not have any argument. It does not return any value Types of arguments Formal argument A variable such as: out = sum(limit); limit is a formal argument Actual argument Use the actual value instead of the formal argument: out = sum(5); The 5 is an actual argument 9

Functions contd. Example 2: What is new? The following function calls use actual arguments out = sum(rand()); out = sum(17 x); out = sum(pow(x, 2)); out = sum(rand() () % 11); etc. 10

Functions contd. Example 3: A function with multiple arguments # include <stdio.h> # include <math.h> # define N 10 // prototype (function declaration) double POLY(double a, int b); void main(){ double r = 0., out = 0.; printf( Enter a double number: "); scanf("%lf", &r); out = POLY(r, N); // function call printf("the result = %.3lf\n", out); double POLY(double r, int n){ // function definition int ii = 0; double result = 0; result N = r ii = 1 ii for(ii = 1; ii <= N; ii++) result += pow(r, ii); return result; Output Enter a double number: 0.5 The result = 0.999 11

Functions contd. Example 4: A function with string argument # include <stdio.h> // prototype (function declaration) int str_length(char a[]); void main(){ char name[30]; int len = 0; printf("please enter your name: "); scanf("%s", name); len = str_length(name); th( printf("your name is %s of length %d\n", name, len); int str_length(char a[]) { int count = 0; while(a[count]!= '\0') count++; return count; Output Please enter your name: Freeman Your name is Freeman of 7. 12

Next Class A function with an array as an argument Can the argument be an implicit output? How can a function modify its arguments? Using address (&) Functions with the same name Take the example of printf() Introduce for the Final Project Programming in C is amazing! 13

Keywords Function declaration (prototype) Function call Function definition Actual argument Formal argument The return statement Lecture Keywords 14

Lecture Summary Introduced the functions in C language 15