Programming Fundamental Instructor Name: Lecture-10 Today’s Lecture Introduction – Functional Design Approach Functions in C++ Functions Types Structure of the Function Declaration & Definition of Functions Function Prototype Functions Definition Understanding Functions with Examples Problem Statements for Practice 2 Introduction Basic Programming Constructs The basic constructs of programming are sequence, decision making and loops We have discussed all these and we are in position to write any kind of programs Programs can be refined more with some other techniques and constructs Like other programming languages C++ also have one of major programming construct that is known as function. Just consider the making of any thing that consists of different parts. You will find that all the parts are individually constructs before finals big product. For example a stool, table, car, bike, room , building and etc You will see that all these are major task but they came into existence after completion of many other smaller tasks 3 Introduction Divide and Conquer Strategy Divide and Conquer strategy s used to dive a large problem into smaller manageable problem Find solution for smaller problems Then combine the solutions of smaller problem to get solution of your major problem This concept is of Functional Design or top down designing Functional Design is a paradigm used to simplify the design of hardware and software devices such as computer software . A functional design assures that each modular part of a device or software has only one responsibility and performs that responsibility with the minimum of side effects on other parts. 4 Guide lines for structured programming Modular Single entry - single exit Introduction Functional Design Approach 6 Introduction Functional Design Approach 7 Laboratory Stool Constructing a laboratory Stool Constructing a laboratory Stool Task: Making a stool Subtask: Make a seat Make legs for the stool Assemble them Functions in C++ What is a function? The functions are like subtasks. They receive some information, do some process and provide a result. A function is an assignment or a task that must be performed to complement the other part(s) of a program. Functions allow to structure programs in segments of code to perform individual tasks. 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 so each function performs a specific task. 11 What we will study today … What are functions? How are they declared ? How are they defined ? What values are passed to functions ? What values do functions return ? Function(Objective) Function: A piece of code that perform a specific task Divide the Task into sub-Tasks Divide and conquer approach Better Understanding Information Hiding Reusability Function Steps involve in functions: Declare a function Define a function Call a function Functions in C++ Calling Mechanism To call a function we just need to write the name of the function and provides its arguments (if any) When a function is called we do not write its return type or type of the arguments But they must be called with correct argument types If a function return a value then it must be assigned to variable of correct data type (if required) 15 Functions in C++ Declarations and Definition of Functions 16 Functions in C++ Function Declaration A function declaration , also called the function prototype, tells the compiler about a function name, return type, and parameters of the function. It actually tells how to call the function. The actual body of the function can be defined separately. Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function. A function declaration has the following parts: return_type function_name( parameter list ); This statement is placed after #include<iostream> (and other headers) and before main(void). 17 Functions in C++ Function Definition The function definition tells the compiler what task the function will be performing. A function definition cannot be called unless the function is declared. The function prototype and the function definition must agree EXACTLY on the return type, the name, and the parameters. The only difference between the function prototype and the function header is a semicolon . The function definition is placed AFTER the end of the main(void) function. 18 // Function Declaration void pak(); void india(); main() { cout<<" im in main"<<endl; // Call Function pak(); india(); getch(); } // Function Definition void pak() { cout<<" i am in pakistan"<<endl;; } void india() { cout<<" i am in india"; } Functions in C++ Types of function in c++ There are two categories of functions: 1. Functions that return a value 2. Functions that do not return a value 20 Functions in C++ Structure of a function The declaration syntax of a fun ions is as follows return-value-type function-name( argument-list ) { declarations and statements } return-value-type: Function may or may not return a value. If a function returns a value, that must be of a valid data type. This can only be one data type that means if a function returns an int data type then it can only return int and not char or float. Return type of a function may be int, float, char or any other valid data type. The function which do not return a value, the return type of such function is void. 21 Functions in C++ Returning a value The keyword return is used to return some value from the function. It does two things, returns some value to the calling program and also exits from the function. We can only return a value from a function. Function-Name The same rules of variable naming conventions are applied to functions name. Function name should be self-explanatory like square, squareRoot, circleArea etc. Argument-List Argument list contains the information which we pass to the function. Some function does not need any information to perform the task. In this case, the argument list for such functions will be empty. Arguments to a function are of valid data type like int number, double radius etc. 22 Functions in C++ Understanding Function with Example Lets look at example that have a function which adds two number using namespace std; #include <iostream> int addition (int a, int b); main () { int z; z = addition (5, 3) ; cout << "The result is " << z; } int addition (int a, int b) { int r; r=a+b; return r; } 23 Functions in C++ Understanding Function with Example Program is divided into two functions, additions & main. Order of writing the main and other functions doesn’t matter, program will always start by calling main Main calls the function addition with two values 5 & 3 that corresponds to the parameter a & b as below From the calling point the control goes to the function addition and execution of main stops at this point Function addition after execution returns result and control is transferred back to the point from where function was called. 24 Functions in C++ Problems for Practice Lets discuss some problem statements and find out their solutions. You have to practice these problems at home 1. Write a function that calculates the square of a number. Call this function from the main 2. Write a program that has a function to calculate the integer power of some number (Xn) 3. Write a program that has function to calculate the area of a ring. 4. Write a function that test whether a given number is even or not 25 Functions in C++ Problems for Practice Lets discuss some problem statements and find out their solutions. You have to practice these problems at home 1. Write a function that calculates the square of a number. Call this function from the main 2. Write a program that has a function to calculate the integer power of some number (Xn) 3. Write a program that has function to calculate the area of a ring. 4. Write a function that test whether a given number is even or not 5. Find the functions that can help you in any of above problem solution in math library. 26 27
© Copyright 2024