1 / 22

Functions Introduction to Programming

Functions Introduction to Programming. By Engr. Bilal Ahmad. Functions. It allows to structure programs in segments of code to perform individual tasks. In C++, a function is a group of statements that is given a name, which can be called from some point of the program.

jody
Download Presentation

Functions Introduction to Programming

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. FunctionsIntroduction to Programming By Engr. Bilal Ahmad ITP by Engr. Bilal Ahmad

  2. Functions • It allows to structure programs in segments of code to perform individual tasks. • In C++, a function is a group of statements that is given a name, which can be called from some point of the program. • The most common syntax for a function is; Type name (parameter1, parameter2,……..) { Statements } ITP by Engr. Bilal Ahmad

  3. Explanation • Type is the type of the value returned by this function • Name is the identifier by which the function will be called • Parameters as many as you would like, each parameter must consist of the type followed by an identifier, with each parameter being separated by a comma. For example int x, here int is the type and x is the identifier. • Statements is the functions body. It is the block of the statements surrounding by the braces { } that specify where the function actually does. ITP by Engr. Bilal Ahmad

  4. Example • // function example • #include <iostream> • usingnamespace std; • int addition (int a, int b) • { • int r; • r=a+b; • return r; • } • int main () • { • int z; • z = addition (5,3); • cout << "The result is " << z; • } • The result is ? ITP by Engr. Bilal Ahmad

  5. Explanation • The program is divided in two functions; • Addition and main • Remember that no matter the order in which they are defined • A C++ program always start by calling the main function • In fact main is the only function that is called automatically • The code in any other function is only executed if its function is called from the main otherwise the function has no significance ITP by Engr. Bilal Ahmad

  6. Example’s Explanation • Lets have some discussion about the example that we have done before. What we did we created the addition function right? • Can you identify the type, name and parameters etc. in the example below; ITP by Engr. Bilal Ahmad

  7. What is return? • The final statement you all have seen is the return statement do you have any idea why it is used? • This statement is used to send the control back to the main function so that the function can be used for other processing • In the example the return r with have the value of 8 and than will pass on the control to the main function. ITP by Engr. Bilal Ahmad

  8. Can a function be called multiple times within a program? • // function example • #include <iostream> • usingnamespace std; • int subtraction (int a, int b) • { • int r; • r=a-b; • return r; • } • int main () • { • int x=5, y=3, z; • z = subtraction (7,2); • cout << "The first result is " << z << '\n'; • cout << "The second result is " << subtraction (7,2) << '\n'; • cout << "The third result is " << subtraction (x,y) << '\n'; • z= 4 + subtraction (x,y); • cout << "The fourth result is " << z << '\n'; • } ITP by Engr. Bilal Ahmad

  9. Functions with NO types • You will use void in this case • The syntax is type name (argument1, argument2….) {statements} • Requires the declaration to begin with a type because it is the syntax requirement. • If there is a function that doesn’t need to return a value what will you do? Is there any function where there is no need to return a value. remember the first program that we did to start with the C++ Code! ITP by Engr. Bilal Ahmad

  10. Example • // void function example • #include <iostream> • usingnamespace std; • voidprintmessage () • { • cout << "I'm a function!"; • } • int main () • { • printmessage (); • } ITP by Engr. Bilal Ahmad

  11. Quiz • You have 10 minutes maximum to attempt this quiz. • On a blank piece of paper just write your name and roll number and without any other information just write the output of the code. • No copying No cheating • After 10 minutes there will be no marking of your quiz, I am sorry. • The functions will be ended with this quiz, if you need any further information you can read http://www.cplusplus.com/doc/tutorial/functions/ ITP by Engr. Bilal Ahmad

  12. Quiz • #include <iostream> • usingnamespace std;  • int divide (int a, int b=2) • { • int r; • r=a/b; • return (r); • } • int main () • { • cout << divide (12) << '\n'; • cout << divide (20,4) << '\n'; • return 0; • } ITP by Engr. Bilal Ahmad

  13. Surprise At the End of the Lecture there will be a surprise for you all. Stay tuned  ITP by Engr. Bilal Ahmad

  14. Arrays • So now we have started writing functions, which will become a part of our every program. • As C language is an Function Oriented Language so we will be dealing with too many functions, not in this ITP course but OOP and DSA. • Our program tool kit is almost complete but still a very important component is missing and it is called Arrays. This Lecture will be based on Arrays. I will strongly advise you after this Lecture go and read Lecture 11 of CS201-ITP can be found on www.csanditllu.wordpress.com ITP by Engr. Bilal Ahmad

  15. Why Arrays? • Let us consider an Example to calculate the average of 10 students. At first we will declare 10 variables to store the age of 10 students each and then sum up all the ages and divide them with 10. complicated process isn’t it. If yes than can you suggest any other way? • No because you don’t Know • I know ‘Yes’ but why shall I tell you all? • Achakoinahibatatahun next slide dekhoapsab ITP by Engr. Bilal Ahmad

  16. Arrays Explanation • Array is a special data type. Suppose if you have 100 students and you want to calculate the average instead of declaring 100 variables you use the simple and straight forward arrays. • Declaration is simple Data_typearray_name [size]; for example Int ages [10]; ITP by Engr. Bilal Ahmad

  17. Structure of Array ITP by Engr. Bilal Ahmad

  18. Sample Problem 1 • Problem Statement: Write a Program which reads positive integers from the user and store these ones in an array. User can enter a maximum of 100 numbers. Stop taking inputs when user enters -1 ITP by Engr. Bilal Ahmad

  19. Solution • We have to declare an integer array of size 100 • We need to use a loop to get the input from the users • There are two conditions to terminate the loop and they are either user has entered 100 numbers or user entered -1 • For and while loop can execute zero or more times whereas do while may execute one or more times • We take an integer z to get the input from the user and small I as a counter so the condition will be (z!-1 && i< 100) ITP by Engr. Bilal Ahmad

  20. Program Code • # include <iostream> • using namespace std; • main () • { • int c[100]; • inti,z; • do • { • int z, i = 0; • cout<<"please enter the number (-1 to endup the input)"<<endl; • cin >> z; • if (z!= -1) • { • c[i]=z; • } • i++; • } • while • (z!=-1 && i< 100); • cout <<"the total number of positive integers entered by user is"<<i - 1; • } ITP by Engr. Bilal Ahmad

  21. Surprise • Well it is not any prize for the surprise but it was the Last Lecture to cover the course content, in case if we get some time we will try to have some labs but your course is completed. Read till chapter no 12 of the Book uploaded on www.csanditllu.wordpress.com • It was a pleasure to have all you in my class and I think so I have done my level best to give you a nice experience in the University. I would like you to write a 1 page document and share all your experiences with me. I will mark it for your assignment as well, it will be a treat for you. Be positive and write whatever you want related to me as a person, course etc. ITP by Engr. Bilal Ahmad

  22. You all are very dear and highly respectful for me and I wish you all the very best of Luck for the future. • You are not going to have any courses with me in the next semester but if I am in the University you will face me in semester 6 to high onwards. • May Allah bless you all “AMEEN”. If you need any help/ advise let me know and I will be there for you, even if I am not in the University you can do so. • Good Luck again and all the best for the examination. I believe that you all have potential and you all will get good grades. • At the end we all are human beings if you are hurt because of me I am sorry for that. • If you want to say any thing for me feel free and I am listening. ITP by Engr. Bilal Ahmad

More Related