Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Chapter - C++ Functions, PPT, C++ Programming, Semester, Engineering

Chapter - C++ Functions, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE) PDF Download

Review: What = and & Mean

•In C++ the = symbol means either initialization or assignment

      –If it’s used with a type declaration, it means initialization

      –If it’s used without a type declaration, it means assignment

         int j(7);        // j is initialized with value 7

         int k = 4;       // k is initialized with value 4

         j = 3;           // j is assigned value 3

 

•In C++ the &  symbol also has a similar “dual nature”

       –If it’s used inside a type declaration, it means a reference (an alias)

               •Arguments to function are always declared along with their types

       –If it’s used outside a type declaration, it means “address of”

            int swap (int & i, int & j); // references to int

            int & s = j; // reference s initialized to refer to j

            int * p = & j; // pointer p initialized w/ j’s address

 


 

 


C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

 

Review: Parameter/Variable Declarations

•Hint: read parameter and variable declarations right to left

       int i;     “i is an integer”

      int & r = i;      “r is a reference to an integer (initialized with i)”

      int * p;               “p is a pointer to an integer”

      int * & q = p;  “q is a reference to a pointer to an integer

                                 (initialized with p)”

 

•Read function declarations inside out

       “function main takes an integer and an array of pointers to  char, and returns an  integer”

        int main (int argc, char * argv[]);

 

        “function usage takes a pointer to char, and returns void (nothing)”

         void usage (char * program_name);

 

           “function setstring takes a reference to a (C++) string, and returns void”

             void setstring (string & s);

 

 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

How Function Calls Work

•A function call uses the “program call stack”

     1.Stack frame is “pushed” when the call is made

     2.Execution jumps to the function’s code block

     3.Function’s code block is executed

     4.Execution returns to just after where call was made

     5.Stack frame is “popped” (variables in it destroyed)

•This incurs a (small) performance cost

     –Copying arguments, other info into the stack frame

     –Stack frame management

     –Copying function result back out of the stack frame

 

 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

Review: Pass By Value

Chapter - C++ Functions, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

 

 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

 Review: Pass By Reference

Chapter - C++ Functions, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

 

 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

 What About Pointers as By-Value Arguments?

Chapter - C++ Functions, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

 

 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

 What About Passing Pointers By-Reference?

Chapter - C++ Functions, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

Pass By const Reference

Chapter - C++ Functions, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

 

 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

Default Arguments

•Some functions can take several arguments

      –Can increase function flexibility

      –Can reduce proliferation of near-identical functions

 

•But, callers must supply all of these arguments

      –Even for ones that aren’t “important”

 

•We can provide defaults for some arguments

      –Caller doesn’t have to fill these in

 

 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

Required vs. Default Arguments

 

•Function with required argument

 

      // call as foo(2); (prints 2)

      void foo(int a);     

      void foo(int a) {cout << a << endl;}

 

•Function with default argument

      –Notice only the declaration gives the default value

 

      // can call as foo(2); (prints 2)

      // or can call as foo();  (prints 3)

      void foo(int a = 3);

      void foo(int a) {cout << a << endl;}


 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

Defaults with Multiple Arguments

•Function with one of two arguments defaulted

 

      // can call as foo(2); (prints 2 3)

      // or can call as foo(2, 4); (prints 2 4)

      void foo(int a, int b = 3); 

      void foo(int a, int b)

       {cout << a << “ ” << b << endl;}

 

•Same function, with both arguments defaulted

 

      // can call as foo(); (prints 1 3)

     // or can call as foo(2); (prints 2 3)

     // or can call as foo(2, 4); (prints 2 4)

     void foo(int a = 1, int b = 3); 

     void foo(int a, int b)

      {cout << a << “ ” << b << endl;}

 

 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

Default Argument Limitations

•Watch out for ambiguous signatures

    –foo();  and  foo(int a = 2);  for example

 

•Can only default the rightmost arguments

    –Can’t declare void foo(int a = 1, int b);

 

•Caller must supply leftmost arguments

    –Even if they’re the same as the defaults

 

 

 

 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

Varying Parameters

initializer_list<T> parameters;

 

void foo(initializer_list<string> coffee){

  for (auto beg = coffee.begin(); beg !=          coffee.end(); ++beg)

   { cout << *beg  << “ ”; 

  }

  cout << endl;

}

 

//print out all 4 coffees

foo(“latte”, “mocha”, “eggnog latte”, “peppermint mocha”);

 

//print out only 2 coffees

foo(“latte”, “mocha”);

 

 


 

C++ Functions Concept------------------------------Next Slide-------------------------------

 

 

 

Function Overload

// can call errMsg with 1 or 2 integers or a string

void errMsg(int & errCode)

  {cout << “Error code is: ” << errCode<< endl;}

 

void errMsg(const int & errCode)

  {cout << “Error code is: ” << errCode<< endl;}

 

void errMsg(int & errCode, string msg)

  {cout << “Error --” << msg << “Error code: ”           errCode << endl;}

 

Void errMsg(string msg)

  {cout << “Error --” << msg << endl;}

 

int err5 = 5; err1 = 1;

const int outOfRange = 3;

errMsg (err5);

errMsg (err1, “can’t open file”);

errMsg (“dividing by 0”); errMsg (outOfRange);

 

The document Chapter - C++ Functions, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

Top Courses for Computer Science Engineering (CSE)

FAQs on Chapter - C++ Functions, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

1. What are functions in C programming?
Ans. Functions in C programming are self-contained blocks of code that perform a specific task. They are used to divide a large program into smaller, manageable modules. Functions can be called from the main program or from other functions, making the code more organized and easier to understand.
2. How do you define a function in C programming?
Ans. To define a function in C programming, you need to specify the return type, function name, and any parameters it accepts. The syntax for defining a function is as follows: return_type function_name(parameter1, parameter2, ...){ // function body // code to be executed } The return type specifies the data type of the value the function returns. The function name is the identifier used to call the function. Parameters are optional and specify the data types and names of the values the function accepts.
3. How do you call a function in C programming?
Ans. To call a function in C programming, you need to use the function name followed by parentheses. If the function has parameters, you need to provide the corresponding values inside the parentheses. The syntax for calling a function is as follows: function_name(argument1, argument2, ...); The arguments are the actual values that will be passed to the function parameters. The function will then execute its code and may return a value back to the caller.
4. Can a function in C programming return multiple values?
Ans. No, a function in C programming cannot directly return multiple values. However, you can use pointers or structures to achieve a similar effect. Pointers allow you to modify variables outside the function, while structures can hold multiple values and be returned as a single entity. For example, you can pass pointers as function parameters and modify the values they point to inside the function. Alternatively, you can define a structure with multiple fields to hold the desired values and return the structure from the function.
5. What is recursion in C programming?
Ans. Recursion in C programming is the process of a function calling itself either directly or indirectly. It allows a function to solve complex problems by breaking them down into smaller, simpler subproblems. Each recursive call reduces the problem size until it reaches a base case, where the function does not call itself again. Recursion is often used in scenarios such as traversing tree-like data structures, calculating factorials, or solving problems that exhibit a recursive structure. However, it is important to define base cases and ensure that the recursive calls eventually converge to the base case to avoid infinite loops.
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Objective type Questions

,

Semester Notes

,

video lectures

,

C++ Programming

,

Summary

,

Exam

,

Chapter - C++ Functions

,

Engineering - Computer Science Engineering (CSE)

,

Engineering - Computer Science Engineering (CSE)

,

Viva Questions

,

Semester

,

MCQs

,

PPT

,

PPT

,

C++ Programming

,

Free

,

Chapter - C++ Functions

,

practice quizzes

,

ppt

,

Sample Paper

,

Engineering - Computer Science Engineering (CSE)

,

Semester

,

past year papers

,

Semester

,

Important questions

,

Chapter - C++ Functions

,

pdf

,

Previous Year Questions with Solutions

,

C++ Programming

,

mock tests for examination

,

PPT

,

study material

,

shortcuts and tricks

,

Extra Questions

;