22
Functions Introduction to Programming By Engr. Bilal Ahmad 1 ITP by Engr. Bilal Ahmad

Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

Embed Size (px)

Citation preview

Page 1: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad1

FunctionsIntroduction to Programming

By Engr. Bilal Ahmad

Page 2: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad2

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 }

Page 3: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad3

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.

Page 4: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad4

Example // function example #include <iostream> using namespace 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 ?

Page 5: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad5

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

Page 6: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad6

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;

Page 7: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad7

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.

Page 8: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad8

Can a function be called multiple times within a program? // function example #include <iostream> using namespace 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'; }

Page 9: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad9

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!

Page 10: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad10

Example // void function example #include <iostream> using namespace std;   void printmessage () { cout << "I'm a function!"; }   int main () { printmessage (); }

Page 11: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad11

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/

Page 12: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad12

Quiz

#include <iostream> using namespace 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; }

Page 13: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad13

Surprise

At the End of the Lecture there will be a surprise for

you all. Stay tuned

Page 14: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad14

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

Page 15: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad15

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? Acha koi nahi batata hun next slide

dekho ap sab

Page 16: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad16

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_type array_name [size];

for example

Int ages [10];

Page 17: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad17

Structure of Array

Page 18: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad18

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

Page 19: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad19

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)

Page 20: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad20

Program Code # include <iostream> using namespace std; main () { int c[100]; int i,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; }

Page 21: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad21

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.

Page 22: Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

ITP by Engr. Bilal Ahmad22

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.