Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Modular Programming With Functions
Chapter 7: User-Defined Functions II
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Storage Classes.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Principles of Programming Chapter 6: Function & Recursion  In this chapter, you will learn about  Introduction to function  User define function  Function.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
FUNCTIONS. C function can be classified into two categories, namely, library functions and user – defined functions. main is an example of user – defined.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
Principles of Programming - NI Chapter 6: Function In this chapter, you will learn about Introduction to function User define function Function prototype.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
C++ Programming Lecture 12 Functions – Part IV
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Functions Course conducted by: Md.Raihan ul Masood
Chapter 4 Function and Structure.
Introduction to Programming
C Functions -Continue…-.
INC 161 , CPE 100 Computer Programming
C++.
Lecture 7: Repeating a Known Number of Times
Functions and Structured Programming
The Three Attributes of an Identifier
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-defined Functions
Scope, Parameter Passing, Storage Specifiers
Tejalal Choudhary “C Programming from Scratch” Function & its types
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Chapter 6 - Functions Outline 5.1 Introduction
User-defined Functions
Functions Recursion CSCI 230
CSC 253 Lecture 7.
A function with one argument
C Storage classes.
Dr Tripty Singh Tutorial for Fuctions
Function In this lesson, you will learn about Introduction to Function
Chapter 7: User-Defined Functions II
The Function Prototype
Function.
In C Programming Language
In C Programming Language
1-6 Midterm Review.
Function.
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Storage Classes.
Presentation transcript:

Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.

Declaration and definition Function is a self-contained block of statements that perform a logical task Declaration or prototype: int add2(int,int); Definition is actually writing the full function: int add2(int a, int b){ return (a+b); }

Two ways to write functions int add2(int,int); main(){ printf(“%d”,add2(10,20)); } int add2(int a, int b){ return (a+b); int add2(int a, int b){ return (a+b); } main(){ printf(“%d”,add2(10,20));

Why to use functions? Avoids rewriting the same code Makes the program easier to write and modify

How does function work?

int add2(int,int); //prototype int sub2(int,int); int main(){ int i=1230,j=-100,sum,diff; sum = add2(i,j); //function call with i,j parms diff = sub2(i,j); printf("sum = %d, diff = %d\n",sum,diff); } int add2(int a, int b){ //definition return a+b; // a,b are formal params int sub2(int a, int b){ return a-b;}

Any function can call any function #include<stdio.h> int message(); int main( ){ message( ) ; } int message( ){ printf ( "\nCan't imagine life without C" ) ; main( ) ;

Recursion

Recursion – function calling itself #include<stdio.h> int fact(int); int main(){ int n=5; printf("Factorial = %d\n",fact(n)); } int fact(int n){ if(n>1) return n*fact(n-1); //calling itself with n-1 else return 1;

Recursion programs to try Finding summation of n numbers Fibonacci series in which a0=0, a1=1, a(n) = a(n-2) + a(n-1) i.e. any term is sum of previous two terms. So series = 0,1,1,2,3,5,8,13... Tower of Hanoi game https://www.youtube.com/watch?v=7AUG7zXe-KU

Loops versus recursion Iteration involves repetition of same structure. Recursion involves repetition through calling the function itself with different inputs. Iteration needs less memory and time. Recursion consumes more memory and is slower but makes the program simpler.

Two types of functions Library functions Ex. printf( ), scanf( ) User-defined functions Ex. add2( ), fact( )

Okay to have many return statements but one return value int fun(int n){ if(n>1) return n; else return (-n); } // return (n,-n); is wrong

Valid returns return; return 23; return (23); //brackets are optional return ‘m’;

Void means no return void msg(){ printf("just print\n"); }

What is the output void fun ( int b ){ b = 60 ; printf ( "\n%d", b ) ; } main( ){ int a = 30 ; fun ( a ) ; printf ( "\n%d", a ) ;

Variables scope – local and global Local variables are defined and thus alive within a function or a code segment enclosed within {} Global variables are declared outside all the functions

Local versus global #include<stdio.h> int g = 20; int main () { printf("g = %d\n",g); //global int g = 10; //local printf("g = %d\n",g); //local }

Storage Classes in C (a) Where the variable would be stored. (b) What will be the initial value (c) What is the scope of the variable (d) How long would the variable exist.

Four Storage classes Auto Register Static External

AUTO AUTO Storage memory Initial value random Scope Local Life Till within the scope

AUTO – example 1 main( ){ auto int i = 1 ; { printf ( "\n%d ", i ) ; } printf ( "%d ", i ) ; printf ( "%d", i ) ;

AUTO – example 2 main( ){ auto int i = 1 ; { auto int i = 2 ; printf ( "\n%d ", i ) ; } printf ( "%d ", i ) ; printf ( "%d", i ) ;

Register – fast but limited in number Storage CPU Initial value random Scope Local Life Till within the scope

REGISTER – example 1 main( ){ register int i ; for ( i = 1 ; i <= 10 ; i++ ) printf ( "\n%d", i ) ; }

STATIC – value persists and shared among functions Storage memory Initial value zero Scope Local Life value persists among different functions

Extern – global variables Storage memory Initial value zero Scope global Life as long as the program

Extern – example 1 int i ; increment( ) {i++; printf ( "i = %d\n", i ) ; } decrement( ){i--; printf ( "i = %d\n", i ) ;} main( ){ printf ( "\n i = %d", i ) ; increment( ) ; increment( ) ; decrement( ) ; decrement( ) ; }

Extern – 1 variable and 2 files //file m.c #include"e.c" int i = 5; main(){ display(); } //file e.c #include<stdio.h> extern int i; display(){ printf("\n i = %d",i); } gcc m.c e.c  then run using ./a.out

EXTERN – declaration vs definition extern int count; is a declaration int count=5; is the definition

#include<stdio.h> main(){ extern int i; printf("%d",i); } //Error – undefined integer i