Functions. Arash Rafiey. September 26, 2017

Similar documents
Arrays and Strings. Arash Rafiey. September 12, 2017

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

Subject: Fundamental of Computer Programming 2068

Principles of Programming. Chapter 6: Arrays

Unit 7. Functions. Need of User Defined Functions

AMCAT Automata Coding Sample Questions And Answers

Lecture 9 - C Functions

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

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

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Programming. Decision Making (2) Loops

ECE264 Fall 2013 Exam 1, September 24, 2013

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

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

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

BSM540 Basics of C Language

Basic and Practice in Programming Lab7

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

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

n Group of statements that are executed repeatedly while some condition remains true

C: How to Program. Week /Mar/05

Introduction: The Unix shell and C programming

Language comparison. C has pointers. Java has references. C++ has pointers and references

Control Structure: Loop

Study Guide for Test 2

C Program Development and Debugging under Unix SEEM 3460

15 FUNCTIONS IN C 15.1 INTRODUCTION

Chapter 2 - Introduction to C Programming

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

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

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

M1-R4: Programing and Problem Solving using C (JAN 2019)

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

Trace call recsum(x, 3)

CSCI 2132 Software Development. Lecture 18: Functions

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

Procedural programming with C

Chapter 7 Functions. Now consider a more advanced example:

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

Fundamentals of Programming Session 14

Introduction to C An overview of the programming language C, syntax, data types and input/output

Code No: R Set No. 1

Decision Making -Branching. Class Incharge: S. Sasirekha

Flow Chart. The diagrammatic representation shows a solution to a given problem.

Multiple Choice Questions ( 1 mark)

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Chapter 6 - Pointers

LAB 6 FUNCTIONS I School of Computer and Communication Engineering

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

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

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

CS1100 Introduction to Programming

This is CS50. Harvard University Fall Quiz 0 Answer Key

Computer Programming: C++

EECS402 Lecture 02. Functions. Function Prototype

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

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?

More Pointers Week 11

Dept. of CSE, IIT KGP

Programming in C++: Assignment Week 1

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Principles of C and Memory Management

Programming Studio #9 ECE 190

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Introduction to Scientific Computing and Problem Solving

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

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

H192 Midterm 1 Review. Tom Zajdel

EENG 212 Lab 2. Recursive Functions

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

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

Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to.

Solutions to Assessment

UNIT 3 FUNCTIONS AND ARRAYS

C Exercises. Ben Vandiver February 28, 2003

Introduction to Computer Science Midterm 3 Fall, Points

DC54 DATA STRUCTURES DEC 2014

Questions Bank. 14) State any four advantages of using flow-chart

Pointers. Introduction

Func%ons. CS449 Fall 2017

UNIVERSITY OF WINDSOR Fall 2006 QUIZ # 1. Examiner:Ritu Chaturvedi Dated : Oct 3rd, Student Name: Student Number:

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

MODULE 3: Arrays, Functions and Strings

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Engr 123 April 25, 2018 Final Exam Review. 3. Write a method which will accept a string and return the number of three-letter words in the string.

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

FORM 2 (Please put your name and form # on the scantron!!!!)

Chapter 1 & 2 Introduction to C Language

Transcription:

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 building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. In C functions provide reusability of code and improve understandability of large programs.

Function Definition Syntax for function definition: return type function name ( parameter list ) { functon body }

Function Definition Syntax for function definition: return type function name ( parameter list ) { functon body } Return type: can return values. return type is the data type of the value which the function returns.

Function Definition Syntax for function definition: return type function name ( parameter list ) { functon body } Return type: can return values. return type is the data type of the value which the function returns. If the function does not return any value, then return type is void

Function Definition Parameter list: Parameter list are the parameters ( or arguments) that are passed to the function when it is called.

Function Definition Parameter list: Parameter list are the parameters ( or arguments) that are passed to the function when it is called. It consists of parameter type and parameter name. A function may contain no parameters.

Function Definition Parameter list: Parameter list are the parameters ( or arguments) that are passed to the function when it is called. It consists of parameter type and parameter name. A function may contain no parameters. Function body: The function body consists of statements that perform a specific task.

Example Example: Simple function to find the maximum of two numbers.

Example Example: Simple function to find the maximum of two numbers. int max ( int a, int b ) { if ( a<b ) return b; else return a; }

Function Declaration Function declaration tells the compiler about the function name, return type and the parameters.

Function Declaration Function declaration tells the compiler about the function name, return type and the parameters. Syntax: return type function name ( parameter list );

Function Declaration Function declaration tells the compiler about the function name, return type and the parameters. Syntax: return type function name ( parameter list ); Example: int sum ( int a, int b);

Function Call A function performs its specified task when it is called by the program.

Function Call A function performs its specified task when it is called by the program. Syntax to call a function: function name ( parameter(1),...parameter(n) );

Function Call A function performs its specified task when it is called by the program. Syntax to call a function: function name ( parameter(1),...parameter(n) ); Example: sum( a, b);

Function Call A function performs its specified task when it is called by the program. Syntax to call a function: function name ( parameter(1),...parameter(n) ); Example: sum( a, b); If the function returns some value then we can store the return value;

Function Call A function performs its specified task when it is called by the program. Syntax to call a function: function name ( parameter(1),...parameter(n) ); Example: sum( a, b); If the function returns some value then we can store the return value; Example: int x = sum ( a, b );

Example Example: Program to find factorial of a number. # include <stdio.h> long int fact(int); void main() { int i,n; long int factorial; } printf( Enter a number \n ); scanf( %d, &n); factorial = fact( n ); printf( Factorial is: %ld, factorial );

Example long int fact( int num ) { } int i; long int f=1; for (i = 1; i<num; i++) f = f i; return f;

Sorting an Array # include <stdio.h> void sort(int a, int numbers[ ] ); void main() { } int i, n; printf( Enter number of elements in array \n ); scanf( %d,&n); int arr[n]; printf( Enter the elements of the array \n ); for (i = 0; i<n; i++) scanf( %d, &arr[i]); sort( n, arr );

Sorting an Array void sort( int a, int numbers[ ] ) { int i, j, temp; for (i = 0; i<a; i++){ } } for (j = i+1; j<a; j++) { if (numbers[i]> numbers[j]){ temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; } printf( The numbers in ascending order are: \n ); for (i = 0; i<a; ++i) printf( %d \n, numbers[i]); }

Function call When a function is called, the parameters can be passed in two ways. Call by value Call by reference

Call by value Call by value: In this method the actual value of the parameter is copied into the formal parameter of the function.

Call by value Call by value: In this method the actual value of the parameter is copied into the formal parameter of the function. Formal parameter is the value which is used in the function definition.

Call by value Call by value: In this method the actual value of the parameter is copied into the formal parameter of the function. Formal parameter is the value which is used in the function definition. Any changes to the formal parameters inside the function does not affect the actual value of the parameters.

Call by value Call by value: In this method the actual value of the parameter is copied into the formal parameter of the function. Formal parameter is the value which is used in the function definition. Any changes to the formal parameters inside the function does not affect the actual value of the parameters. In C, call by value is used by default.

Call by value Example: Program to swap two numbers using call by value # include<stdio.h> void swap( int a, int b); void main() { int x = 10, y = 20; printf( Values before swap in main x = %d, y = %d \n,x,y); //x=10 y=20 swap(x,y); printf( Values after swap in main x = %d, y = %d \n,x,y); //x=10 y=20 }

Call by value void swap( int a, int b ) { int temp; temp = a; a = b; b = temp; printf( Values after swap inside swap function: x = %d, y = %d, a, b); } //x=20 y=10

Call by reference Call by reference: In this method the address of the actual parameter is copied into the formal parameter of the function.

Call by reference Call by reference: In this method the address of the actual parameter is copied into the formal parameter of the function. Actual parameter is the parameter which is used in the function call.

Call by reference Call by reference: In this method the address of the actual parameter is copied into the formal parameter of the function. Actual parameter is the parameter which is used in the function call. Changes to the formal parameters changes the value of the actual parameters.

Call by reference Example: Program to swap two numbers using call by reference # include<stdio.h> void swap( int a, int b); void main() { int x = 10, y = 20; printf( Values before swap in main x = %d, y = %d \n,x,y); //x=10 y=20 swap(&x,&y); printf( Values after swap in main x = %d, y = %d \n,x,y); //x=20 y=10 }

Call by reference void swap( int a, int b ) { int temp; temp = a; // the value store in a is assigned to temp a = b; // here a is an address. Exchange the values of the addresses of // a, b b = temp; printf( Values after swap inside swap function: x = %d, y = %d, a, b); } //x=20 y=10

Tutorial for Thursday September 28 Question (1 by Lavanya) Write a C program to calculate the square of a given number using function concepts. function name: square Under main() function: 1. Read an integer n 2. pass n to the function square 3. print the output Under square(int n) function: 1. calculate the square of n 2. Store it under a variable res 3. return res Sample Output: Enter the number: 5 Square of given number 5: 5 2 = 25

Question (2) Write a program that reads n integer numbers from the user and then pass them to a function reverse which prints them in the reverse order. Hint : You need to declare and array and store the numbers in that array then pass the array to the reverse fuction which it goes from the beginning to the end of the array and print the elements of the array. (See the slides 23,24).

Question (3 by Lavanya) Write a C program using switch-case: if input = A, perform addition operation on given two numbers and return the output if input = S, perform subtraction operation on given two numbers and return the output if input = D, perform division operation on given two numbers and return the output if input = E, print No operation selected and exit Sample Output: Press A for addition, S for subtraction, D for division, E to exit A 5 10 Addition operation: 5 + 10 = 15 See slide 22 http://cs.indstate.edu/~arash/teaching/cs256lec3.pdf

Question (4) Write a program that reads two strings A1, A2 from the user and send them to a function compare-strings that checks whether they are the same or not. Hint : bool compare-strings (char A1[], char A2[]); int main() { bool flag; // boolean variable char A1[30]; char A2[30]; scanf( %s, &A1); scanf( %s, &A2); flag=compare-strings(a1,a2); if ( flag ) printf( the same ); else printf ( different ); }

bool compare-string (char A1[], char A2[] ) { int len1=0; int len2=0; }

Question (5) What does this code print? int main() { int x=20; int p; p = &x; p = 30; printf( x is %d,x); }

Question (6) Write a program that reads two sorted lists (sorted arrays) L1, L2 of lengths n, m (respectively) and merge them into a new sorted list L3. Sample Input: L1 : 2, 3, 7, 10, 21, 37 and L2 : 1, 2, 9, 45 Output : L3 : 1, 2, 2, 3, 7, 9, 10, 21, 37, 45