Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring Semester 2013 Lecture 5

Similar presentations


Presentation on theme: "Spring Semester 2013 Lecture 5"— Presentation transcript:

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

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

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

4 return_type function_name (type1 arg1,type2 arg2,..,typen argn)
Function Definitions A function definition has two principal components: Function Header Body of the Function Function Header: The first line of function definition is called function header. In consists of three parts: the data type of return value Function name (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 return_type function_name (Arguments/void)
Function Definitions 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 Umer Rana

6 Function Definitions 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 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
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. Programming In C++, Lecture 5 By Umer Rana

9 Function Passing Arguments
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. Programming In C++, Lecture 5 By Umer Rana

10 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… Programming In C++, Lecture 5 By Umer Rana

11 Function int add(int p,int q) { return p+q; //Body of the function }
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.  For Example: int a; a= add(5,10); 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: The number of arguments in the function calls and function declaration must be same. 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 <stdio.h> 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 <stdio.h> 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 <stdio.h> 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();                } 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);         } Programming In C++, Lecture 5 By Umer Rana

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

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


Download ppt "Spring Semester 2013 Lecture 5"

Similar presentations


Ads by Google