Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming In C++ Spring Semester 2013 Lecture 5 Programming In C++, Lecture 5 By Umer Rana.

Similar presentations


Presentation on theme: "Programming In C++ Spring Semester 2013 Lecture 5 Programming In C++, Lecture 5 By Umer Rana."— Presentation transcript:

1 Programming In C++ Spring Semester 2013 Lecture 5 Programming In C++, Lecture 5 By Umer Rana

2 Function n What is function? Programming In C++, Lecture 5 By Umer Rana

3 Function To avoid having to write the same code over and over. n A number of statements grouped into a single logical unit are called a function. n The use of function makes programming easier since repeated statements can be grouped into functions. n Splitting the program into separate function make the program more readable and maintainable. There are two types of function in C 1.Built in function 2.User-Defined function. Programming In C++, Lecture 5 By Umer Rana

4 Function Definitions n A function definition has two principal components: 1. Function Header 2. Body of the Function n Function Header: –The first line of function definition is called function header. –In consists of three parts: 1.the data type of return value 2.Function name 3.(optionally) a set of arguments separated by commas and enclosed in parenthesis. return_type function_name (type1 arg1,type2 arg2,..,typen argn) Programming In C++, Lecture 5 By Umer Rana

5 Function Definitions n Body of the Function Variable declaration and the program logic are implement in the function body. return_type function_name (Arguments/void) { Body of the function. } Programming In C++, Lecture 5 By Umer Rana

6 Function Definitions n Function definition format return-value-type function-name( parameter-list ) { declarations and statements } –Function-name: any valid identifier –Return-value-type: data type of the result (default int ) void – indicates that the function returns nothing –Parameter-list: comma separated list, declares parameters A type must be listed explicitly for each parameter.Or the variables who receive the argument Programming In C++, Lecture 5 By Umer Rana

7 Function Definitions n Function definition format (continued) return-value-type function-name( parameter-list ) { declarations and statements } –Declarations and statements: function body (block) Variables can be declared inside blocks (Local Variables) Functions can not be defined inside other functions –Returning control If nothing returned –return; –or, until reaches right brace If something returned –return expression ; Programming In C++, Lecture 5 By Umer Rana

8 Function Return statement Programming In C++, Lecture 5 By Umer Rana A function may or may not return a value. A ‘return’ statement returns some value to the calling function and it may be assigned to the variable in the left side of the calling function. The return value can be a constant, variable, a general expression, a pointer to function, or a function call. If a function does not return a value, the return type in the function definition and declaration is specified as void.

9 Function Passing Arguments Programming In C++, Lecture 5 By Umer Rana Arguments can be passed to a function by two methods, They are:- pass by value pass by reference Pass by value Function in C passes all arguments by value. When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function. Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of an argument to a function is known as passing by value.

10 Programming In C++, Lecture 5 By Umer Rana Function Passing Arguments Pass by reference When passing by reference technique is used, the address of the data item is passed to the called function. Using & operator we can determine the address of the data item. Note that function once receives a data item by reference, it acts on data item and the changes made to the data item also reflects on the calling function. Here you don't need to return anything to calling function. We discuss in detail later in later chapters…

11 Function int add(int p,int q) { return p+q; //Body of the function } n A function can be invoked whenever it is needed. It can be accessed by specifying its name followed by a list of arguments enclosed in parenthesis and separated by commas. n For Example: int a; a= add(5,10); n The corresponding arguments in the function call are called actual arguments or actual parameters, since they define the data items that are actually transferred. Programming In C++, Lecture 5 By Umer Rana

12 Function Prototype The Function Prototype means tells the compiler the name of the function, the data type the function returns, and the number and data of the function’s arguments. It is declare before the main function and ends with semicolon. –A prototype declares a function. –A function call executes a function. –A function definition is the function itself. Programming In C++, Lecture 5 By Umer Rana

13 Calling Function Function Call is a mechanism that is used to invoke a function to perform a specific task. Function call can be invoked at any point in the program. Function calling statement terminate my semicolon (;). The following condition must be satisfied for function call: 1. The number of arguments in the function calls and function declaration must be same. 2. The prototype of each of the argument in the function call should be same as the corresponding parameter in the function declaration statement. Programming In C++, Lecture 5 By Umer Rana

14 Function #include int add(int p, int q); void main(void) { int total; printf(“Add two numbers.\n"); total=add(10,20); printf(“Total of two numbers is %d“,total); } int add(int p,int q) { return p+q; //Body of the function } Programming In C++, Lecture 5 By Umer Rana

15 Function #include int add(int p, int q); void main(void) { int total,num1=10, num2=20; printf(“Add total of num1 & num2.\n"); total=add(num1,num2); printf(“Total of two numbers is %d“,total); } int add(int p,int q) { return p+q; //Body of the function } Programming In C++, Lecture 5 By Umer Rana

16 Function #include void my_function(); void main(void) { printf("Main function.\n"); my_function(); printf("Back in function main.\n"); } void my_function() { printf(“This is BSCS-Group A.\n"); } Programming In C++, Lecture 5 By Umer Rana

17 Function int add(int p,int q); // function prototype void display(int p, int q, int r); // function prototype void main() { int a,b,c; clrscr(); printf("Enter two numbers\n"); scanf("%d%d",&a,&b); c=add(a,b); display(a,b,c); getch(); } Programming In C++, Lecture 5 By Umer Rana int add(int p,int q) { return(p+q); } void display(int p, int q, int r) { printf("Sum of %d and %d is %d",p,q,r); }

18 Recursion Programming In C++, Lecture 5 By Umer Rana Recursive functions are those functions, which call itself within that function. Recursion is the process of repeating items in a self-similar way.

19 Recursion Programming In C++, Lecture 5 By Umer Rana int sum(int n) { int n; if(n<10) n=sum(n+1); else return n; } int sum(int n); int main() { int num,input; printf("Enter a integer:\n"); scanf("%d",&num); input=sum(num); printf("sum=%d",add); }


Download ppt "Programming In C++ Spring Semester 2013 Lecture 5 Programming In C++, Lecture 5 By Umer Rana."

Similar presentations


Ads by Google