CS101 Introduction To Programming PDF

Document Details

AdequateMulberryTree

Uploaded by AdequateMulberryTree

MEDITERRANEAN INSTITUTE OF TECHNOLOGY

Tags

programming c programming functions computer science

Summary

This document is about functions in C programming. It covers topics such as functions, recursive functions, and examples in C. It also contains details on how function calls are handled and how to use return values in programs.

Full Transcript

CS101 INTRODUCTION TO PROGRAMMING 11/18/2024 1 WORKING WITH FUNCTIONS Lecture 5 11/18/2024 2 Outlines Functions & procedures Recursion 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 3 Functio...

CS101 INTRODUCTION TO PROGRAMMING 11/18/2024 1 WORKING WITH FUNCTIONS Lecture 5 11/18/2024 2 Outlines Functions & procedures Recursion 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 3 Functions A self-contained block of codes or sub programs with a set of statements that perform some specific task or coherent task when it is called. Also known as procedure or subroutine or module, in other programming languages. It takes some input, processes it, and returns an output. Any ‘C’ program contain at least one function : main() Advantage of functions 1. Code Reusability: By creating functions in C, you can call it many times. So, we don't need to write the same code again and again. 2. Code optimization: It makes the code optimized we don't need to write much code. 3. Easily to debug the program 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 4 Functions There are basically two types of function: 1. Library function 2. User defined function 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 5 Functions Elements of user-defined functions: To write an efficient user defined function, the programmer must be familiar with the following three elements: 1. Function Declaration (Function Prototype) 2. Function Call 3. Function Definition 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 6 Functions: 1-Function Declaration (Function Prototype) A function declaration is the process of giving the compiler the function name. Syntax return_type function_name(parameter/argument); return_type function-name(); Ex : int add(int a,int b); int add(); Note: At the time of function declaration, function must be terminated with ; 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 7 Functions: 2- Function Call When we call any function, the control goes to function body and execute entire code. Syntax : function-name(); function-name(parameter/argument); return value/ variable = function-name(parameter/argument); Ex : add(); // function without parameter/argument add(a,b); // function with parameter/argument c=fun(a,b); // function with parameter/argument and return values 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 8 Functions: Function Definition Defining of function is giving body of function that means write a logical code inside the function body. Syntax: return_ type function-name(parameter list) // function header. { declaration of variables; body of function; // Function body return statement; (expression or value) //optional } Example: int add(int x, int y) int add(int x, int y) { { int z; return ( x + y ); z = x + y; } return z; } 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 9 Functions 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 10 Functions 1. The execution of a C program begins from the main() function. 2. When the compiler encounters functionName(); inside the main function, control of the program 3. jumps to void functionName() 4. Then, the compiler starts executing the codes inside the user-defined function. 5. The control of the program jumps to statement next to functionName(); once all the code inside has been executed. 6. The function definition are executed. 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 11 Functions: Example 1 int a=10,b=20, c;// Global variables void multiply(); // function prototype void sum() // defining function { c=a+b; printf("Sum: %d", c); } int main() { sum(); // calling the function multiply(); // calling the function } void multiply() // defining function { c=a*b; printf("\nMultiplication: %d", c); } 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 12 Functions: Example 2 #include int addNumbers(int a, int b); // function prototype int main() { int n1,n2,sum; printf("Enters two numbers: "); scanf("%d %d",&n1,&n2); sum = addNumbers(n1, n2); // function call printf("sum = %d",sum); return 0; } int addNumbers(int a,int b) // function definition { int result; result = a+b; return result; // return statement } 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Functions 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Functions: Return Statement Syntax of return statement: return; // does not return any value or return(exp); // the specified exp value to calling function. Ex return a; return (a+b); The return statement terminates the execution of a function and returns a value to the calling function. The program control is transferred to the calling function after return statement. 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Functions 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Functions: Parameters Parameters provides the data communication between the calling function and called function. They are two types of parameters: 1 : Formal parameters: These are the parameters transferred into the calling function (main program) from the called function(function). 2 : Actual parameters: These are the parameters transferred from the calling function (main program) to the called function (function).  The parameters specified in calling function are said to be Actual Parameters.  The parameters declared in called function are said to be Formal Parameters.  The value of actual parameters is always copied into formal parameters. 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Functions: Example #include int addNumbers(int x, int y) { // x and y are formal parameters return x + y; } int main() { int n1,n2,sum; printf("Enters two numbers: "); scanf("%d %d",&n1,&n2); sum = addNumbers(n1, n2); // n1 and n2 are actual parameters printf("sum = %d",sum); return 0; } 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Functions: Summary 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Functions: Difference between Actual Parameters and Formal Parameters 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Let's Practice 1 Scenario: You want to create a simple program that asks for temperature readings from three different days, calculates the average, and then displays a message based on that average. Problem Statement: Write a C program that: 1) Reads the temperatures for three days from the user. 2) Calls a function calculateAverage that takes three integers (representing the temperatures) as input and returns the average temperature as a float. 3) Calls a void function displayTemperatureMessage that takes the average temperature as input and prints a message: "It's hot!" if the average is above 30°C. "It's warm." if the average is between 20°C and 30°C. "It's cold!" if the average is below 20°C. 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Let's Practice 2 Scenario: You are creating a program that reads a set of grades, identifies the highest and lowest grades, and displays them. Problem Statement: Write a C program that: 1. Reads an array of grades for n students (with n provided by the user). 2. Includes a function findBestGrade that returns the highest grade. 3. Includes a function findWorstGrade that returns the lowest grade. 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Recursion Recursion When function is called within the same function, it is known as recursion in C. The function which calls the same function, is known as recursive function. Features :  There should be at least one if statement used to terminate recursion.  It does not contain any looping statements. Advantages :  It is easy to use.  It represents compact programming structures. Disadvantages :  It is slower than that of looping statements because each time function is called. Note: while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop (Base condition). 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Recursion:Example recursionfunction() { recursionfunction(); //calling self function } 11/18/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY Recursion: Example of tail recursion in C int factorial(int n) { Output: if ( n == 0) factorial of 5 is return 1; else return (n * factorial (n -1));} void main() { int number= 0; int result=0; do{ printf("Enter a positive integer: "); scanf("%d", &number); }while (number

Use Quizgecko on...
Browser
Browser