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

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE) PDF Download

Introduction

 

The slides cover the following topics:

  1. Functions
  2. Advantages of Functions
  3. Function Prototypes
  4. Function Definitions
  5. Function Return Types
  6. Types of Functions
  7. Pointers

 

Functions in C Programming ---------------------------------------Next slide

 

What is a function

 

  1. A large program in c can be divided to many subprogram
  2. The subprogram posses a self contain components and have well define purpose.
  3. The subprogram is called as a function
  4. Basically a job of function is to do something
  5. C program contain at least one function which is main().

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

 

Functions in C Programming ---------------------------------------Next slide

 

 Advantages of function

 

  1. It is much easier to write a structured program where a large program can be divided into a smaller, simpler task.
  2. Allowing the code to be called many times
  3. Easier to read and update
  4. It is easier to debug a structured program where there error  is  easy to find and fix
 
Functions in C Programming ---------------------------------------Next slide
 

Example

 

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

 
Functions in C Programming ---------------------------------------Next slide
 
 

 How the function works

 

  1. C program doesn't execute the statement in function until the function is called.
  2. When function is called the program can send the function information in the form of one or more argument.
  3. When the function is used it is referred to as the called function
  4. Functions often use data that is passed to them from the calling function
  5. Data is passed from the calling function to a called function by specifying the variables in a argument list.
  6. Argument list cannot be used to send data. Its only copy data/value/variable that pass from the calling function.
  7. The called function then performs its operation using the copies.
 
Functions in C Programming ---------------------------------------Next slide
 

Function prototypes

 

  1. Provides the compiler with the description of functions that will be used later in the program
  2. Its define the function before it been used/called
  3. Function prototypes need to be written at the beginning of the program.
  4. The function prototype must have :
    A return type indicating the variable  that the function will be return

 

Syntax for Function Prototype

 return-type function_name( arg-type name-1,...,arg-type name-n);

 

Function Prototype Examples

  1. double squared( double number );
  2. void print_report( int report_number );
  3. int get_menu_choice( void); 
 
Functions in C Programming ---------------------------------------Next slide
 

Function Definitions

 

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

 

Functions in C Programming ---------------------------------------Next slide

 

Function Definition Examples

 

float conversion (float celsius) 

 {

        float fahrenheit;

        fahrenheit = celcius33.8

        return fahrenheit;

  }

The function name’s is conversion

 

This function accepts arguments celcius of the type float. The function return a float value.

 

So, when this function is called in the program, it will perform its task which is to convert fahrenheit by multiply celcius with 33.8 and return the result of the summation.

 

Note that if the function is returning a value, it needs to use the keyword return.

 

Functions in C Programming ---------------------------------------Next slide

 

Function return types

 

 Can be any of C’s data type:

  char

  int

  float

  long………

 

Examples:

 

int func1(...)          / Returns a type int.   /

float func2(...)        / Returns a type float. /

void func3(...)         / Returns nothing.      /

 

Functions in C Programming ---------------------------------------Next slide

 

Types of Functions

 

Function can be divided into 4 categories:

  1. A function with no arguments and no return value
  2. A function with no arguments and a return value
  3. A function with an argument or arguments and returning no value
  4. A function with arguments and returning a values

 

Functions in C Programming ---------------------------------------Next slide

 

A function with no arguments and no return value

 

  1. Called function does not have any arguments
  2. Not able to get any value from the calling function
  3. Not returning any value
  4. There is no data transfer between the calling function and called function.
Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

#include<stdio.h>

#include<conio.h>

void printline();

 

void main()

{

    printf("Welcome to function in C");

    printline();

    printf("Function easy to learn.");

    printline();

    getch();

}

 

void printline()

{

    int i;

    printf(" ");

    for(i=0;i<30;i++)

    {        printf("-");    }

    printf(" ");

}

 

Functions in C Programming ---------------------------------------Next slide

 

A function with no arguments and a return value

 

  1. Does not get any value from the calling function
  2. Can give a return value to calling program

 

#include <stdio.h>

#include <conio.h>

int send();

 

void main()

{

    int z;

    z=send();

    printf(" You entered : %d.",z);

    getch();

}

 

int send()

{

    int no1;

    printf("Enter a no: ");

    scanf("%d",&no1);

    return(no1);

}

 

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

 

Functions in C Programming ---------------------------------------Next slide

 

A function with an argument or arguments and returning no value

 

  1. A function has argument/s
  2. A calling function can pass values to function called , but calling function not receive any value
  3. Data is transferred from calling function to the called function but no data is transferred from the called function to the calling functionGenerally Output is printed in the Called function
  4. A function that does not return any value cannot be used in an expression it can be used only as independent statement.
 
Functions in C Programming ---------------------------------------Next slide
 

#include<stdio.h>

#include<conio.h>

void add(int x, int y);

 

void main()

{

    add(30,15);

    add(63,49);

    add(952,321);

    getch();

}

 

void add(int x, int y)

{

    int result;

    result = x+y;

    printf("Sum of %d and %d is %d. ",x,y,result);

}

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

 

Functions in C Programming ---------------------------------------Next slide

 

A function with arguments and returning a values

 

  1. Argument are passed by calling function to the called function
  2. Called function return value to the calling function
  3. Mostly used in programming because it can two way communication
  4. Data returned by the function can be used later in our program for further calculation.
 
Functions in C Programming ---------------------------------------Next slide
 
 

#include <stdio.h>

#include <conio.h>

int add(int x,int y);

 

void main()

 

{

    int z;

 

    z=add(952,321);

    printf("Result %d. ",add(30,55));

    printf("Result %d. ",z);

 

    getch();

}

int add(int x,int y)

 

{

    int result;

    result = x + y;

    return(result);

}

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

Send 2 integer value x and y to add()

 

Function add the two values and send back the result to the calling function

 

int is the return type of function

 

Return statement is a keyword and in bracket we can give values which we want to return.

 

Functions in C Programming ---------------------------------------Next slide

 

Pointers

 

Variable that declared occupies a memory according to it size

 

It has address for the location so it can be referred later by CPU for manipulation

 

The ‘’ and ‘&’ Operator

 

Int x= 10

 

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

We can use the address which also point the same value.

 

Functions in C Programming ---------------------------------------Next slide

 

#include <stdio.h>

#include <conio.h>

 

 

void main()

 

{

    int i=9;

 

    printf("Value of i : %d ",i);

    printf("Adress of i %d ", &i);

 

    getch();

}

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

 

Functions in C Programming ---------------------------------------Next slide

 

#include <stdio.h>

#include <conio.h>

 

 

void main()

 

{

    int i=9;

 

    printf("Value of i : %d ",i);

    printf("Address of i %d ", &i);

    printf("Value at address of i: %d", (&i));

 

    getch();

}

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

 

Functions in C Programming ---------------------------------------Next slide

 

Passing arguments by value or reference

 

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

 

Functions in C Programming ---------------------------------------Next slide

 

Passing arguments by value or reference

 

#include <stdio.h>

#include <conio.h>

void callByValue(int, int);

void callByReference(int , int );

 

 

int main()

{

    int x=10, y =20;

    printf("Value of x = %d and y = %d. ",x,y);

 

    printf(" CAll By Value function call... ");

    callByValue(x,y);

    printf(" Value of x = %d and y = %d. ", x,y);

 

    printf(" CAll By Reference function call... ");

    callByReference(&x,&y);

    printf("Value of x = %d and y = %d. ", x,y);

 

    getch();

    return 0;

}

 

Functions in C Programming ---------------------------------------Next slide

 

void callByValue(int x, int y)

 

{

    int temp;

    temp=x;

    x=y;

    y=temp;

 

    printf(" Value of x = %d and y = %d inside callByValue function",x,y);

}

 

void callByReference(int x, int y)

 

{

    int temp;

    temp=x;

    x=y;

    y=temp;

 

}

 

Functions in C Programming ---------------------------------------Next slide

 

Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

The document Chapter - Function in C Programming, PPT, 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)

FAQs on Chapter - Function in C Programming, PPT, Semester, Engineering - Computer Science Engineering (CSE)

1. What is a function in C programming?
Ans. A function in C programming is a block of code that performs a specific task. It is a reusable piece of code that can be called from different parts of a program. Functions help in modularizing the code and make it easier to understand, test, and maintain.
2. How do you declare a function in C programming?
Ans. In C programming, a function is declared by specifying its return type, followed by the function name, and then the parameters it accepts (if any). The syntax for function declaration is: return_type function_name(parameter1, parameter2, ...); For example, to declare a function that returns an integer and takes two integer parameters, the declaration would be: int add(int num1, int num2);
3. What is the purpose of a return statement in a function?
Ans. The return statement in a function is used to send a value back to the calling function. It terminates the execution of the current function and returns the value specified. The return type of the function must match the type specified in the function declaration. For example, if a function is declared to return an integer, the return statement should be followed by an integer expression or variable.
4. Can a function in C programming have multiple return statements?
Ans. Yes, a function in C programming can have multiple return statements. The return statements can be used at different points within the function to return a value and terminate the function's execution. However, it is important to ensure that all possible execution paths within the function have a return statement, otherwise, it may lead to undefined behavior.
5. How do you call a function in C programming?
Ans. To call a function in C programming, you simply need to write the function name followed by parentheses that may contain arguments (if any). The syntax for function call is: function_name(argument1, argument2, ...); For example, to call the "add" function with two integer arguments, the function call would be: int result = add(5, 3);
Download as PDF

Top Courses for Computer Science Engineering (CSE)

Related Searches

video lectures

,

Engineering - Computer Science Engineering (CSE)

,

ppt

,

Semester

,

Previous Year Questions with Solutions

,

past year papers

,

PPT

,

Chapter - Function in C Programming

,

Semester

,

Exam

,

PPT

,

mock tests for examination

,

Semester

,

Engineering - Computer Science Engineering (CSE)

,

Engineering - Computer Science Engineering (CSE)

,

study material

,

Viva Questions

,

Sample Paper

,

Chapter - Function in C Programming

,

Extra Questions

,

PPT

,

Objective type Questions

,

Chapter - Function in C Programming

,

practice quizzes

,

Free

,

Important questions

,

Semester Notes

,

pdf

,

Summary

,

shortcuts and tricks

,

MCQs

;