Chinese Proverb says……... Advantages void main( ) { int n, k, i ; printf(“\n Enter number:-”); scanf(“%d”, &n); for(i=2 ; i <=(n/2) ; i++ ){ if ( n %

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.
C Language.
Spring Semester 2013 Lecture 5
Modular Programming With Functions
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
/* program to find area of a ring */ #include int main() { float a1,a2,a,r1,r2; printf("Enter the radius : "); scanf("%f",&r1); a1 = 3.14*r1*r1; printf("Enter.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
1 Functions and Structured Programming. 2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. –The.
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.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Storage Classes.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
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.
FUNCTIONS. C function can be classified into two categories, namely, library functions and user – defined functions. main is an example of user – defined.
Return function Random function Recursion function Function in C 1.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
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.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
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.
C++ Lecture 2 Friday 11 July Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
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.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Chapter 6: Function Introduction to function Standard functions User defined functions –function prototype –function definition Function call Storage classes.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
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.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
CS314 – Section 5 Recitation 9
Chapter 7: Function.
Chapter 4 Function and Structure.
C Functions -Continue…-.
C++.
The Three Attributes of an Identifier
Functions Dr. Sajib Datta
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Functions Dr. Ashish Sasankar. In programming, a function is a segment that groups code to perform a specific task. A C program has at least one function.
Scope, Parameter Passing, Storage Specifiers
Tejalal Choudhary “C Programming from Scratch” Function & its types
Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
Local Variables, Global Variables and Variable Scope
C Storage classes.
Outline Defining and using Pointers Operations on pointers
Storage class.
Functions.
In C Programming Language
ETE 132 Lecture 6 By Munirul Haque.
C Programming Lecture-17 Storage Classes
Scope Rules.
Presentation transcript:

Chinese Proverb says……..

Advantages void main( ) { int n, k, i ; printf(“\n Enter number:-”); scanf(“%d”, &n); for(i=2 ; i <=(n/2) ; i++ ){ if ( n % i = =0 ) { k=1; } // if ends } // for ends if ( k = = 1 ){ printf(“Not Prime”); } else{ printf (“ Prime ” );} } // main ends

Advantages int prime (int ); int AskFromUser( ); void display( ); void main( ) { int n, k, i ; n=AskFromUser( ); k=prime(n); display(k); } OR void main( ){ display ( prime( AskFromUser( ))); }

int prime( int n){ int flag=0,i; for(i=2 ; i <=(n/2) ; i++ ){ if ( n % i = = 0 ) { flag=1; } return(flag); } int AskFromUser( ){ int n; Printf(“\nEnter A number:”); Scanf(“%d”,&n); }Advantages

void display(int k) { if (k==1) printf(“Not Prime”); else printf(“Prime”); }Advantages

Advantages  Reduces Complexity & increases Readability  Makes Program size smaller  Debugging becomes very easy  can be reused in other programs

Types of Functions Eg. Printf( ), Scanf ( ), etc Eg. Prime( ), fibonaci( ), etc

Questions ?  What is the Type of main ( ) ?  What type of language is C ? and why ?  How many types of datatype classes r there in C ?  What r keywords ?  Functions names are stored in the same way as variables.

Brain Teaser 1 func( ); main( ) { printf( “ %d”, func); } func( ) { return (0); }

Brain Teaser 2 main( ) { printf( “ %u”, main( ) ); }

Three main things….. Function Declaration ( prototype) Function call Function Definition

Types of Arguments(Parameters) Formal Arguments Actual Arguments Eg : Call  func ( 10,20,30, 40); Defi.  func (int a, int b,int c,int d );

ANSI Method ANSI Method void main(){ Func(10,20); } Func(int a,int b) { ◦ Printf (“ Sum=%d ”,(a+b)); }

K & R Method void main(){ Func(10,20); } Func(a,b) int a; int b; { ◦ printf (“ Sum= %d ”,(a+b)); }

How Arguments r passed ?  In TC, the argument calling convention is from right to left For Eg: func(10,20,30,40); // function call func( int a, int b,int c, int d) // function Defi. { ……..some code…… }

How Arguments r passed ? void func(int, int,int, int ); void main( ) { int a=5; func( a, a++, ++a, --a ); } void func(int a, int b,int c,int d ) { printf(“ %d %d %d %d ”, a, b, c, d ); }

Call By Value & Call By Reference void main( ){ int func(int ) ; int j=10, i ; i=fun( j ); printf (“ %d %d ”,--i, j ); } int fun(int j){ return ( j++ ); }

Recursion void main ( ) { int i=0; func(i ); } void func(int i ){ i++; printf(“%d”, i ); if ( i!=5 ) func( i); }

Recursion void main ( ) { int i=0; func(i ); } void func(int i ){ i++; if ( i!=5 ) func( i); printf(“%d”, i); }

Question ? What is required in Recursion ? What is better Recursion or using loops ?

Scope (active), Visibility & Longetivity ( alive) int i=10; void main( ){ Func( ); Printf ( “%d ”, i ); } // main ends Func( ){ int i=20; // scope of this i is only inside this // func. Global i is not visible here // but it is alive Printf(“ %d ”, i); }

Type of Variables Automatic ( local or internal) Static (can be local or external) Register Extern Global ( external )

Automatic Variable By Default Given Garbage value if uninitialized They r local

Brain Teaser void main(){ int a; Func(a); Printf(“%d ”, a); } Func(int a) { a=++a || a++ || a-- && --a; return(a); }

Static Variable Retains its value during function calls Can be local or external Difference between global varaible and external static variable Gets default value =0 A func can also be static

Brain Teaser void main( ){ int i; for(i=0;i<3;i++){ Func( ); } Func( ){ Static int i=0; i++; Print f(“%d”, i); }

Register Variable  Faster Access  Loop Control variables  Cant be pointed by some pointers

Brain Teaser void main( ) { register int i=10; int *p; p=&i; p=(*p)++; printf (“%d”,*p); }

Extern Variable #include void main( ) { extern int x; printf( “ %d ”, x ); } int x=10;

Extern Variable #include void main( ) { extern int x; printf( “ %d ”, x ); }

A function cannot be the target of an assignment You cannot define func inside another function.

Brain Teaser int a; void main( ) { Func( )=10; printf( “ %d ”, a); } Func( ) { return (a); }

Brain Teaser void main( ) { void func( ) ◦ { ◦ printf( “ Hello C ”); ◦ } func(); }

Function returning Pointers int *func(); void main(){ int i,*p; p=func(); } int *func(){ int x=10,*q; q=&x; return (q); }

Pointer To Function void func( ); void main(){ int (*p)( ); p=func; (*p)( ); } void func( ){ Printf(“Hello LD”); }

Brain Teaser int *func( ); void main(){ int *p; p=func(); printf(“\n”); printf( “ %d ”, *p); } int *func( ){ int k=35; return ( &k); }