C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.

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.
Spring Semester 2013 Lecture 5
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 6: User-Defined Functions I
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. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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.
Chapter 6: Function Introduction to function Standard functions User defined functions –function prototype –function definition Function call Storage classes.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
A First Book of ANSI C Fourth Edition
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter 7: Function.
Functions Course conducted by: Md.Raihan ul Masood
Chapter 6: User-Defined Functions I
Introduction to Programming
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
C Functions -Continue…-.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
C Functions Pepper.
UNIT - V STORED PROCEDURE.
Quiz 2.
Function There are two types of Function User Defined Function
Module 4 Functions – function definition and function prototype.
C Short Overview Lembit Jürimägi.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSCI 161: Introduction to Programming Function
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Tejalal Choudhary “C Programming from Scratch” Function & its types
Chapter 6 Methods: A Deeper Look
Chapter 6 - Functions Outline 5.1 Introduction
6 Chapter Functions.
Functions and Modular Programming
Storage class.
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Function.
In C Programming Language
Based on slides created by Bjarne Stroustrup & Tony Gaddis
1-6 Midterm Review.
Functions Imran Rashid CTO at ManiWeber Technologies.
Function.
CPS125.
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.
C Parameter Passing.
Presentation transcript:

C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof

Contents Functions Storage classes

What is a function? A function is a self-contained subprogram that is meant to do some specific, well-defined task. A program consists of one or more functions. If a program has only one function then it must be the main function.

Advantages of functions: A larger program is divided into sub programs and then solved. When some specific code is to be used more than once at different places in the programs the use of functions avoid repetition of code. Debugging becomes easy. Simple to write the program and can understand what work is done by each part of the program.

Types of functions C programs has two types of functions Library Functions. User defined functions. Library functions: Also called as predefined functions like printf(), scanf(), getchar() etc. User defined Functions: Function that is defined by the user according to his requirement.

User defined functions Users can create their own functions for performing any specific task of the program. These types of functions are called user-defined functions. To create and use these functions, we should know about 1. Function definition 2. Function declaration 3. Function call

Example of user defined function

HOW TO CALL C FUNCTIONS IN A PROGRAM? There are two ways that a C function can be called from a program. They are, Call by value Call by reference Types of arguments Actual parameter – This is the argument which is used in function call. Formal parameter – This is the argument which is used in function definition

Call by value It is a process of calling a function by passing a normal values as arguments then it is called as call by value. In call by value, the modifications which are performed in a specific function will not effect to its calling area.

While Passing Parameters using call by value , xerox copy of original parameter is created and passed to the called function. Any update made inside method will not affect the original value of variable in calling function. In the above example num1 and num2 are the original values and xerox copy of these values is passed to the function and these values are copied into number1,number2 variable of sum function respectively. As their scope is limited to only function so they cannot alter the values inside main function

Call by reference It is a process of calling a function by passing the address as arguments. In case of call by reference the modifications which are performed in a specific function will directly effect to its calling area.

While passing parameter using call by address scheme , we are passing the actual address of the variable to the called function. Any updates made inside the called function will modify the original copy since we are directly modifying the content of the exact memory location.

Recursive function A function which can call itself is called as recursive function. By using recursive functions we can simplify the logic and we can also avoid the loops (while, do while, for). It will repeat until stack is overflow. The recursive function may also leads to the stack overflow.

Math Functions Used to perform different mathematical operations on the given data. Generally included in math.h header file. abs(), sqrt(), pow(), ceil(), floor(), log(), log10(), exp(), sin(), cos(), tan().

All C functions can be called either with arguments or without arguments in a C program. These functions may or may not return values to the calling function. Now, we will see simple example C programs for each one of the below. C function with arguments (parameters) and with return value. C function with arguments (parameters) and without return value. C function without arguments (parameters) and without return value. C function without arguments (parameters) and with return value.

Storage classes What is an extent? The period of time during which memory is associated with a variable is called the extent of the variable. Storage class: It defines the lifetime of variables in a C program. auto static register extern

auto An auto variable is a variable which is created automatically when control enters into block and is destroyed when the control leaves the block. The local variables are often referred as automatic variables. Syntax: auto int a;

register Register variables are stored in CPU’s registers. Register variables are faster in execution as they are part of processor. Syntax: register int a;

static The static storage class instructs the compiler to keep a local variable in existence during the life- time of the program instead of creating and destroying it each time it comes into and goes out of scope.  Syntax: static int a;

extern An external variable is a variable defined outside any function block. They are permanent. They are declared out of main function. They are similar to global variables. Syntax : extern int a;