Programming Fundamentals Chapter 05 - Functions, Slides of Programming Languages

The concept of functions in programming. It describes how functions group program statements into a unit and give it a name, which can be invoked from other parts of the program. The document also explains the reasons to use functions, such as aiding in the conceptual organization of a program and reducing program size. It covers topics such as passing by value and passing by reference, inline functions, function overloading, recursion, and returning by reference. code examples and explanations for each topic.

Typology: Slides

2021/2022

Available from 11/16/2022

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming Fundamentals
Chapter 05 - Functions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Programming Fundamentals Chapter 05 - Functions and more Slides Programming Languages in PDF only on Docsity!

Programming Fundamentals

Chapter 05 - Functions

Functions

◼ A function groups a number of program statements

into a unit and gives it a name.

◼ This unit can then be invoked from other parts of the

program.

◼ The most important reason to use functions is to aid

in the conceptual organization of a program

◼ Another reason to use functions is to reduce program

size. Any sequence of instructions that appears in a

program more than once is a candidate for being

made into a function.

◼ The function’s code is stored in only one place in

memory, even though the function is executed many

times in the course of the program.

Functions

//demonstrates a simple function #include using namespace std; int cube(int x); // function deration int main(){ // tests the cube() function: int n = 1; while (n != 0){ cout <<"Enter a Number: "; cin >> n; cout << "\tcube(" << n << ") = " << cube(n) << endl; // Calling a function } // end of while loop system("PAUSE"); return 0; }//end of main int cube( int x ){ // function definition return xxx; // returns cube of x: } // { function body } Input Arguments Return type

Functions

Each integer read is passed to the cube() function by the call cube(n). The value returned by the function replaces the expression cube(n) and then is passed to the output object coutThe main() function passes the value 5 to the cube() function, and the cube() function returns the value 125 to the main() function.The argument n is passed by value to the formal parameter x. This simply means that x is assigned the value of n when the function is called.

Writing function before main

// Calculates Factorial #include #include <stdlib.h> using namespace std; long fact(int n){ // returns n! = n(n-1)(n-2)...(2)(1) if (n < 0) return 0; int f = 1; while (n > 1) f = n--; // first f=fn then n decrements return f; } int main(){ // tests the factorial() function: for (int i=-1 ; i < 6 ; i++) cout << i <<"!= " << fact(i) << endl; system("PAUSE"); return 0; }//end of main

Calculating Permutations

// Calculates Permutation using Factorial #include #include <stdlib.h> using namespace std; long fact(int n){ // returns n! = n(n-1)(n-2)...(2)(1) if (n < 0) return 0; int f = 1; while (n > 1) f = n--; // first f=fn then n decrements return f; } long perm(int n, int r){ // returns n Permutation k if ( n<0 || r<0 || r>n ) return 0; return fact(n)/fact(n-r); }

Passing by Value and Passing by Reference

#include #include <stdlib.h> using namespace std; void f(int x, int &y){ // changes reference argument to 99: x = 88; y = 99; } int main(){ // tests the f() function: int a = 22,b = 44; cout << "a = " << a << ", b = " << b << endl; // 22, f(a,b); cout << "a = " << a << ", b = " << b << endl; // 22, f(2*a-3,b); cout << "a = " << a << ", b = " << b << endl; // 22, system("PAUSE"); return 0; }//end of main

Passing by Value and Passing by Reference

#include #include <stdlib.h> using namespace std; void swap(float &x, float &y){ // exchanges value of x and y float temp = x; x = y; y = temp; } int main(){ // tests the swap() function: float a = 22.2, b = 44.5; cout << "a = " << a << ", b = " << b << endl; swap(a,b); cout << "a = " << a << ", b = " << b << endl; cout << "&a = " << &a << ", &b = " << &b << endl; system("PAUSE"); return 0; }//end of main

Local and Global Variables #include #include <stdlib.h> using namespace std; int x = 10; // a global variable x int main(){ int x = 50; // local variable x cout << "I m local x and x = " << x << endl; cout << "I m global x and x = " << ::x << endl; x = 70; // modify local x :: x = 30; // modify global x cout << "New value of local x = " << x << endl; cout << "New value of global x = " << ::x << endl; system("PAUSE"); return 0; }

Default Arguments #include #include <stdlib.h> using namespace std; //declaration with default arguments void repchar(char='*', int=45); int main(){ repchar(); //prints 45 asterisks repchar('='); //prints 45 equal signs repchar('+', 30); //prints 30 plus signs system("PAUSE"); return 0; } void repchar(char ch, int n){// displays line of characters // defaults supplied if necessary for(int j=0; j<n; j++) // loops n times cout << ch; // prints ch cout << endl; }

Inline Function // demonstrates inline functions #include #include <stdlib.h> using namespace std; inline float lbstokg(float pounds){ // converts pounds to kilograms return 0.453592 * pounds; } int main(){ float lbs; cout << "\nEnter your weight in pounds: "; cin >> lbs; cout << "Your weight in kilograms is " << lbstokg(lbs) << endl; system("PAUSE"); return 0; }

Function Overloading // Calculates Factorial #include #include <stdlib.h> using namespace std; int add(int a, int b){ cout << " Calling Int " << endl; return a+b; } float add(float a, float b){ cout << " Calling float " << endl; return a+b; } int main(){ // tests the factorial() function: cout << add(5,3) << endl; // Calls int add( int, int) cout << add(5.5F, 4.7F) << endl; // Calls float version system("PAUSE"); return 0; }//end of main

Recursion #include #include <stdlib.h> using namespace std; // calls itself to calculate factorials unsigned long fct(unsigned long n){ static int I = 0; I++; cout << "You Called Me " << I << " times" << endl; if(n > 1) return n * fct(n-1); //self call else return 1; } int main(){ int n; cout << "Enter an integer: "; cin >> n; cout << "Factorial of " << n << " is " << fct(n) << "\n"; system("PAUSE"); return 0; }

Recursion