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

Similar documents
Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Subject: Fundamental of Computer Programming 2068

SUHAIL T A

UNIT 3 FUNCTIONS AND ARRAYS

C Programming Lecture V

Functions BCA-105. Few Facts About Functions:

UNIT III (PART-II) & UNIT IV(PART-I)

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Unit III Functions. C functions can be classified into two categories, namely, library functions and user defined functions.

CSE202-Lec#4. CSE202 C++ Programming

FUNCTIONS OMPAL SINGH

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

MODULE 3: Arrays, Functions and Strings

Functions. (transfer of parameters, returned values, recursion, function pointers).

Chapter-13 USER DEFINED FUNCTIONS

FUNCTIONS. Without return With return Without return With return. Example: function with arguments and with return value

Q 1. Attempt any TEN of the following:

Functions. Arash Rafiey. September 26, 2017

A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions.

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

CS 345. Functions. Vitaly Shmatikov. slide 1

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

Government Polytechnic Muzaffarpur.

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

Unit 7. Functions. Need of User Defined Functions

Downloaded from

PROGRAMMAZIONE I A.A. 2017/2018

BSM540 Basics of C Language

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

Lab Instructor : Jean Lai

Lecture 9 - C Functions

'C' Programming Language

Lecture 5: Methods CS2301

Functions. Introduction :

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

ME 172. C Programming Language Sessional Lecture 8

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Multiple Choice Questions ( 1 mark)

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

Programming & Data Structure Laboratory. Day 2, July 24, 2014

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Chapter 8: Function. In this chapter, you will learn about

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Chapter 5 C Functions

UNDERSTANDING THE COMPUTER S MEMORY

Chapter 4 Functions By C.K. Liang

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

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?

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

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

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY, VIRUDHUNAGAR Department of CSE & IT Internal Test I

CS1150 Principles of Computer Science Methods

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER

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

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CSCI 2132 Software Development. Lecture 18: Functions

CSE 230 Intermediate Programming in C and C++ Functions

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Darshan Institute of Engineering & Technology for Diploma Studies


CSCI 2132 Software Development. Lecture 17: Functions and Recursion

15 FUNCTIONS IN C 15.1 INTRODUCTION

Procedural programming with C

Func%ons. CS449 Fall 2017

Fundamentals of Programming & Procedural Programming

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Solution Set(Reference Book - Programming with c By Byron Gottfried Thrid Edition)

User Defined Functions

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

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Fundamentals of Programming Session 14

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Computers Programming Course 5. Iulian Năstac

Pointers. Mr. Ovass Shafi (Assistant Professor) Department of Computer Applications

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Prepared by: Shraddha Modi

SUMMER 13 EXAMINATION Model Answer

Vidyalankar F.Y. Diploma : Sem. II [CD/CM/CO/CW/DE/ED/EE/EI/EJ/EN/EP/ET/EV/EX/IC/IE/IF/IS/IU/MU] Programming in C

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Functions in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

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

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

Lecture 2:- Functions. Introduction

UNIT III ARRAYS AND STRINGS

CS1150 Principles of Computer Science Methods

A brief intro to pointers for the purposes of passing reference parameters

Transcription:

Computer Programming and Utilization (CPU) 110003 G) Functions 1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific task. The functions which are created by programmer are called user-defined functions. The functions which are in-built in compiler are known as system functions. The functions which are implemented in header libraries are known as library functions. It has a unique name and it is reusable i.e. it can be called from any part of a program. Parameter or argument passing to function is optional. It is optional to return a value to the calling program. Function which is not returning any value from function, their return type is void. While using function, three things are important 1. Function Declaration Like variables, all the functions must be declared before they are used. The function declaration is also known as function prototype or function signature. It consists of four parts, 1) Function type (return type). 2) Function name. 3) Parameter list. 4) Terminating semicolon Syntax: <return type> FunctionName (Argument1, Argument2, Argument3 ); int sum(int, int); In this example, function return type is int, name of function is sum, 2 parameters are passed to function and both are integer. 2. Function Definition Function Definition is also called function implementation. It has mainly two parts. Function header : It is same as function declaration but with argument name. Function body : It is actual logic or coding of the function 3. Function call Function is invoked from main function or other function that is known as function call. Function can be called by simply using a function name followed by a list of actual argument enclosed in parentheses. 1

Syntax or general structure of a Function <return type> FunctionName (Argument1, Argument2, Argument3 ) Statement1; Statement2; Statement3; An example of function int sum(int, int); \\ Functionn Declaration or Signature int a, b, ans; scanf( %d%d, &a, &b); ans = sum(a, b); \\ Functionn Calling printf( Answer = %d, ans); int sum (int x, int y) \\ Functionn Definitionn int result; result = x + y; return (result); 2 Explain different categories of functions. Functions can be classified in one of the following category based on whether arguments are present or not, whether a value is returned or not. 1. Functions with no arguments and no return value \\ void printline(void) 2. Functions with no arguments and return a value \\ int printline(void) 3. Functions with arguments and no return value \\ void printline(int a) 4. Functions with arguments and one return value \\ int printline(int a) 5. Functions that return multiple values using pointer \\ void printline(int a) 1. Function with no argument and no return value. When a function has no argument, it does not receive any data from calling function. When it does not return a value, the calling function does not receive any data from the called function. In fact there is no data transfer between the calling function and called function. 2

void printline(void) ); // No argument No return value clrscr(); printline(); printf("\n GTU \n"); void printline(void) Fun1() int i; for(i=0;i<10; ;i++) printf("-"); 2. Function with no arguments and return a value When a function has no argument, it does not receive data from calling function. When a function has return value, the calling function receives one data from the called function. int get_number(void) ); // No argument int m; No int Fun1() m= =get_number( (); Input printf("%d",m); a = Fun1() int get_number(void) return no; Function int number; result printf("enterr number:") ; scanf("%d",&number); return number; // Return value 3. Function with arguments and no return values When a function has argument, it receives data from calling function. When it does not return a value, the calling function does not receive any data from the called function. 3 No Input No Output void Fun1()

#include< <conio.h> void sum( (int,int); // Argument int no1,no2; printf("enterr no1,no2:" ); scanf("%d%d",,&no1,&no2); sum(no1,no2); Fun1(a) void sum( (int no1,int no2) if(no1>no2) printf("\n no1 is gretest") ; else printf("\n no2 is gretest") ; /// No return value Value of argument No return value void Fun1(int a) 4. Function with arguments and one return value When a function has argument, it receives data from calling function. When a function has return value, the calling function receives any data from the called function. #include< <conio.h> int sum(int); // Argument int no,x; clrscr(); printf("enterr no:"); Value of int fun2(f) scanf("%d",&no); argument x= =sum(no); printf("sum=% %d",x); b = Fun1(a) return(e) getch(); Function int sum(int no) result int add=0,i; while(no>0) i=no%10; 4

5. Function that returns a multiple value Function can return either one value or zero value. It cannot return more than one value To receive more than one value from function, we have to use pointer. So functionn should be called with reference not with value #include<stdio.h> void mathoperation(int x, int y, int *s, int *d); int x=20,,y=10,s,d; mathoperation(x,y,&s,&d); printf("s=%d \nd=%d", s,d); void mathoperation(int a, int b, int *sum, int *diff) add=add+i; no=no/ /10; return add; *sum = a + b; *diff = a - b; // Return value 3 Explain actual argument and formal argument with example. Arguments passed to the function during function calling are called actual arguments or parameters. Arguments received in the definition of a function are called formal arguments or parameters. int max(int, int ) // Function Declaration. int a=5,b=3, ans; ans=max(a, b) ); //a and b are actual arguments. printf("max=% %d", ans); int max(int x, int y) // x and y are formal arguments. if(x>y) return x; 5

else return y; 4 Explain call by value (pass by value) and call by reference (pass by reference) with example. The parameters can be passed in two ways during function calling, Call by value Call by reference Call by value In call by value, the values of actual parameters are copied to their corresponding formal parameters. So the original values of the variables of calling function remain unchanged. Even if a function tries to change the value of passed parameter, those changes will occur in formal parameter, not in actual parameter. #include<stdio.h> void swap(int, int); int x, y; printf("enter the value of X & Y:"); scanf("%d%d", &x, &y); swap(x, y); printf( \ \n Values inside the main function ); printf( \ \n x=%d, y=% %d, x, y); getch(); void swap(int x,,int y) int temp; temp=x; x=y; y=temp; printf( \ \n Values inside the swap function ); printf("\ \n x=%d y=%d", x, y); Output: Enter the value of X & Y: 3 5 Values inside the swap function X=5 y=3 6

Values inside the main function X=3 y=5 Call by Referencee In call by reference, the address of the actual parameters is passed as an argument to the called function. So the original content of the calling function can be changed. Call by reference is used whenever we want to change the value of local variables through function. void swap(int *, int *); int x,y; printf("enterr the value of X & Y: scanf("%d%d", &x, &y); swap(&x, &y);; printf( \n Value inside the main function ); printf("\n x= =%d y=%d", x, y); void swap(int *x, int *y) int temp; temp=*x; *x=*y; *y=temp; printf( \n Value inside the swap function ); printf("\n x= =%d y=%d", x, y); Output: Enter the value of X & Y: 3 5 Value inside the swap function X=5 y=3 Value inside the main function X=5 y=3 Difference between two program is marked as bold in call by reference program 4 What do you mean by recursive function? Explain with example. 7

Recursive function is a function that calls itself. If a function calls itself then it is known as recursion. Recursion is thus the process of defining something in terms of itself. Suppose we want to calculate the factorial of a given number then in terms of recursion we can write it as n! = n * (n-1)!. First we have to find (n-1)! and then multiply it by n. the (n-1)! is computed as (n-1)! = (n-1) * (n-2)!. This process end when finally we need to calculate 1!.which is 1. Ex: 4! =4* *3! =4*3*2! =4*3*2*1! =4*3*2*1 Terminating condition must be there which can terminates the chain of process, otherwise it will lead to infinite number of process. Pictorial presentation of recursion. fact(4) f=1; if(..)..; f=4*fact(3) return 24; fact(3) f=1; if(..)..; f=3*fact(2) return 6; fact(2) f=1; if(..)..; f=2*fact(1) return 2; fact(1) f=1; if(..).. return 1; Tracing of function fact() for n=4 8

Find factorial of a given number using recursion. #include< <conio.h> int fact( (int); int f,n; printf("enterr number:") ; scanf("%d",&n); f= =fact(n); printf("\n factorial=%d",f); int fact( (int n) int f=1; if(n==1) return 1; else f=n*fact(n-1); return f; Advantages Easy solution for recursively defined solution. Complex programs can be easily written with less code. Disadvantages Recursive code is difficult to understand and debug. Terminating condition is must; otherwise it will go in an infinite loop. Execution speed decreases because of function calll and return activity many times. 5 What is scope, lifetime and visibility of variable? Scope The scope of variable can be definedd as that a part of a program where the particular variable is accessible In what part of the program the variable is accessible is depends on where the variable is declared. Local variables which are declared inside the body of function cannot be accessed outside the body of function. 9

Global variables which are declared outside any function definition can be accessible by all the function in a program. Lifetime Lifetime is a time limit during the program execution until whichh a variable exist in a memory. It is also referred to the longevity of variable. Visibility Visibility is the ability of the program to access a variable from the memory. If variable is redeclared within its scope of variable, the variable losses the visibility in the scope of variable which is redeclared. 10