C PROGRAMMING LECTURE METHODS

Slides:



Advertisements
Similar presentations
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Advertisements

CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
Chapter 6: Functions.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
1 Lecture04: Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Engineering Computing I Chapter 4 Functions and Program Structure.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 FUNCTIONS - II Chapter 9 and Today we will continue with functions covering…. Passing by Reference  Scope Sorting variables Function Structure.
1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya.
User-Written Functions
INC 161 , CPE 100 Computer Programming
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
INC 161 , CPE 100 Computer Programming
Functions, locals, parameters, and separate compilation
A Lecture for the c++ Course
FUNCTIONS.
Pointers.
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
INC 161 , CPE 100 Computer Programming
Pre-processor Directives
Templates.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
INC 161 , CPE 100 Computer Programming
Buy book Online -
14th September IIT Kanpur
Scope, Parameter Passing, Storage Specifiers
Functions Inputs Output
Multiple Files Revisited
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.
Functions, Part 2 of 3 Topics Functions That Return a Value
INC 161 , CPE 100 Computer Programming
Pointers.
Simulating Reference Parameters in C
The Function Prototype
7. Pointers, Dynamic Memory
CS150 Introduction to Computer Science 1
Functions.
CS150 Introduction to Computer Science 1
In C Programming Language
CS150 Introduction to Computer Science 1
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
ETE 132 Lecture 6 By Munirul Haque.
Introduction to Computing Lecture 09: Functions (Part II)
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Scope Rules.
Presentation transcript:

C PROGRAMMING LECTURE METHODS Deepak Majeti M-Tech CSE IIT-Kanpur mdeepak@iitk.ac.in 2/23/2019 C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Some Essentials Scope of a variable: - The areas in the code where the variable is visible. Life of a variable: - Areas of the code where the variable exists in memory. C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Example for( int i =0; I < 100 ; i++ ){ \\scope of i } can’t see i here. ****************************************************** int main(){ int i = 0; }// i dies after the main program ends. C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Method Definition: A block of code which performs some well defined computational task. a method may have some input a method may also have some output Example: int add( int , int) Can visualize as a mathematical function which takes a value and returns another value. C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Why Methods Huge codes are difficult to understand and debug. Makes the code look simpler, neater and makes your task easier. Re-usability of code. Example: Imagine a program in which we need to swap two numbers often. Life becomes easy if we could swap in a single step. printf(),scanf() are functions too. They make our work so easy. C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Some terminologies Function Declaration/ Prototype: int func(int , int); Function Definition: int func ( int a, int b){ printf(“Welcome to func”); return (a + b); } C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Some more The value passed to a method is called its “argument” The variable which receives the arguments is called “parameter” Parameters are declared inside the parenthesis and we must declare the type of the parameter. Argument may be an expression but, type (argument) = type (parameter) C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Typical Example #include <stdio.h> void add_print(int , int); //function declaration int main(){ int a=4; int b=5; printf(“Entering ‘add_print’ function\n”); add_print(a,b); printf(“Just came from ‘add_print’ function\n”); return 0; } //function definition void add_print(int val1,int val2){ int c; printf(“The two values entered are:%d,%d \n”,val1,val2); c=val1+val2; printf(“Sum of numbers entered is:%d \n”,c); //parameters, arguments ????????? C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Contd. In the above example, ‘a’, ‘b’ are arguments to the function. ‘val1’, ‘val2’ are the parameters of the function. Scope and Life ‘a’, ‘b’ Have scope limited to the main function. Their life is till the main exits. ‘val1’, ‘val2’ Have scope limited to the function block. Their life is till the function call is over. C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Another Example #include <stdio.h> int mult_return(int , int); //function declaration int main(){ int a=4; int b=5; int c; printf(“Entering ‘mult_return’ function\n”); c = mult_return(a+4,b*2); // sending an expression printf(“Just came from ‘mult_return’ function. The value is %d \n”, c); return 0; } //function definition int mult_return(int val1,int val2){ int c; // “c” again???? printf(“The two values entered are:%d,%d \n”,val1,val2); c=val1*val2; return c; C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Another Example #include <stdio.h> void swap(int , int); //function declaration int main(){ int a=4; int b=5; swap(a,b); printf(“The value of ‘a’ is %d and the value of ‘b’ is %d\n”, a,b); return 0; } // the values of a and b did not swap . //function definition void swap(int val1,int val2){ int temp; printf(“The two values entered are:%d,%d \n”,val1,val2); temp=val1; val1=val2; val2=temp; } C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Some problems Till now the functions took the values of the arguments from the calling function (main). What if we need to change the values of variables in the calling function? How do we get access to the calling function’s data? Simple!! Send the address of the variable C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Another Example #include <stdio.h> int swap(int *, int *); //function declaration int main(){ int a=4; int b=5; swap(&a,&b); printf(“The value of ‘a’ is %d and the value of ‘b’ is %d \n”, a,b); return 0; } // the values of a and b did swap . //function definition int swap(int *val1,int *val2){ int temp; printf(“The two values entered are:%d,%d \n”,*val1,*val2); temp=*val1; *val1=*val2; *val2=temp; } C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Preprocessing We can write methods, declare variable in multiple files. Need to link these. # include<filename> includes the file filename. # define ABC(X) X*X Replaces the occurrences of ABC(z) with z*z. What happens to ABC(z+1)? // try out C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Contd What if ABC(X) is already defined? #ifndef - #endif #ifndef ABC(X) #define ABC(X) X*X #endif C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 End Of Lecture 4 ? QUESTIONS ? C Course, Programming club, Fall 2008