1 / 19

Programming In C++

Programming In C++. Spring Semester 2013 Lecture 5. Function. What is function?. 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.

draco
Télécharger la présentation

Programming In C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming In C++ Spring Semester 2013 Lecture 5 Programming In C++, Lecture 5 By UmerRana

  2. Function • What is function? Programming In C++, Lecture 5 By UmerRana

  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 UmerRana

  4. 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_typefunction_name (type1 arg1,type2 arg2,..,typen argn) Programming In C++, Lecture 5 By UmerRana

  5. Function Definitions • Body of the Function Variable declaration and the program logic are implement in the function body. return_typefunction_name(Arguments/void) { Body of the function. } Programming In C++, Lecture 5 By UmerRana

  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 UmerRana

  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 • returnexpression; Programming In C++, Lecture 5 By UmerRana

  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 UmerRana

  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 UmerRana

  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 UmerRana

  11. Function intadd(intp,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 UmerRana

  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 UmerRana

  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 UmerRana

  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(intp,int q)       {      return p+q;          //Body of the function      } Programming In C++, Lecture 5 By UmerRana

  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(intp,int q)       {      return p+q;          //Body of the function      } Programming In C++, Lecture 5 By UmerRana

  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 UmerRana

  17. Function int add(intp,int q); // function prototype void display(int p, int q, int r); // function prototype void main()        {inta,b,c;clrscr();printf("Enter two numbers\n");scanf("%d%d",&a,&b); c=add(a,b); display(a,b,c); getch();       } int add(intp,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 UmerRana

  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 UmerRana

  19. Recursion • int sum(int n); • int main() • { • intnum,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 UmerRana

More Related