Download presentation
Presentation is loading. Please wait.
2
Introduction To Programming
:Author: Muhammad Meer Hazaar Khan Introduction To Programming
3
Salam & Hello! I am Muhammad Meer Hazaar Khan
Alhamdolilah A licensed professional engineer a schooled and skilled in the application of engineering discipline to the creation of software and seeking to share my knowledge and experience with students . You can find me at CS Department Government College University Layyah Campus or
4
Some Words, Muhammad Meer Hazaar Khan
“Freelancing & Enterprenuership“. LinkedIn Profile: ammad-meer-hazaar-khan- 6b11ab96/ Upwork Profile: cers/~01c0fdce3eda554427 YouTube Channel: nel/UC0Ifqps33SDm5LdYTbEkO1A ?view_as=subscriber “Engineering Degree". B.S.C Software Engineering University And Engineering And Technology Taxila Approved By PEC & Washington Accord U.S.A M.S Degree M.S Computer Science Institute Of Southern Punjab Research Area: Semantic Web Gold Medalist Muhammad Meer Hazaar Khan @ Registered Software Engineer Pakistan Engineering Council No Comp 11516 Lecturer Computer Science Government College University Faisalabad Layyah Campus Freelancer at Upwork Enterprenuer More info on how to find and understand the lectures and the blogs post at Official Website: .
5
Seeking knowledge is an obligation upon every Muslim”
(Sunan Ibn Majjah, 224)
6
Let’s start with the first set of slides
1 Basics OF Programming Let’s start with the first set of slides
7
Text Book Object Oriented Programming in c++ any updated Edition by Robert Lafore
8
C++ Functions A function is known with various names like a method or a sub-routine or a procedure etc. A function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is such that each function performs a specific task.
9
Parts A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. Calling used to call within the other functions
10
Defining a Function The general form of a C++ function definition is as follows − return_type function_name( parameter list ) { body of the function }
11
Return Type − A function may return a value
Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature. Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. Function Body − The function body contains a collection of statements that define what the function does.
12
Function Declarations
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. A function declaration has the following parts − return_type function_name( parameter list );
13
#include <iostream>
using namespace std; // function declaration int sum(); int main () { // local variable declaration: int c; // calling a function to get max value. c = max(); cout << “sum value is : " << c << endl; getchar(); } // function returning the sum between two numbers int sum() { // local variable declaration int d; int a = 100; int b = 200; d= a+b; return d;
14
Parameterized Functions
#include <iostream> using namespace std; // function declaration int max(int num1, int num2); int main () { // local variable declaration: int a = 100; int b = 200; int ret; // calling a function to get max value. ret = max(a, b); cout << "Max value is : " << ret << endl; return 0; } // function returning the max between two numbers int max(int num1, int num2) { // local variable declaration int result; if (num1 > num2) result = num1; else result = num2; return result; Parameterized Functions
15
call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
16
#include <iostream>
using namespace std; // function declaration void swap(int x, int y); int main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; // calling a function to swap the values. swap(a, b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; }
17
call by pointer The call by pointer method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.
18
#include <iostream>
using namespace std; // function declaration void swap(int *x, int *y); int main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; /* calling a function to swap the values. * &a indicates pointer to a ie. address of variable a and * &b indicates pointer to b ie. address of variable b. */ swap(&a, &b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; }
19
call by reference The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.
20
#include <iostream>
using namespace std; // function declaration void swap(int &x, int &y); int main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; /* calling a function to swap the values using variable reference.*/ swap(a, b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.