 Hello everyone, I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So after a few days like I'm back on this CNC++ coding bootcamp. So I was busy with other bootcamp so was not able to continue sessions. So now we have day 22 for this bootcamp and in today's session, I will be explaining an important topic which is functions in C, right? So if you are a programmer and you want to build some solution with the help of coding stuff, so you should know how to write functions in programming. So the way we write functions in C programming, in the similar way we write functions in other programming languages as well, okay? So I'm jumping on to the next slide. So this is about me. So I hope if you are following all the sessions of the bootcamp, you know about me. But if you are a new beginner and you are like attending any session for the first time on this channel, so with the help of this slide, you will be able to know about me. So I have total 16 plus years of experience and it is mixed off education industry and IT industry and working in Salesforce ecosystem and whenever anybody wants to become a developer, so they need to understand the base of programming. So that's why I'm conducting this C and C++ bootcamp so that beginners can understand the basic concepts of programming languages. So moving forward, be consistent whatever you are doing because consistency is the key and if you are following all the sessions or do regular practice and scan this QR code for this C and C++ coding bootcamp telegram group. So here, lots of folks are connected and if you face any difficulty, so I will be there to help you out. And these are social media handles for Sanjay Gupta Tech School if you want to receive timely notification and if you want to receive any update related to the bootcamp sessions or like whatever video I am producing. So all those updates you will find on these platforms and the session tracker link is available in the description of the video. So from there you can have it. Okay, so let's discuss about functions in C programming. So what functions are and if you want to implement some solutions with the help of functions or how you can do that. So first I will be covering the theoretical concepts related to function and then we'll be implementing some programs. So basically a function is a collection of one or more than like one or more than one related instructions which are implemented to perform a particular task. So suppose you have lots of requirement that you need to implement through coding. So instead of writing one function, we split those requirements and for particular requirement, we build a function, right? So this is very important to know because in all the programming languages no matter whether it is C, C++, Java, Python, PHP, Salesforce, APEX, so whatever language you are learning, you are implementing code. So in every programming language, you need to build functions and there are two types of functions available with every programming language one. Some are predefined or library functions and some are user-defined functions that we implement as per our need. Okay, so now we need to understand the need of function, why we need to implement functions in programming languages, right? So if you want to implement function-oriented approach because normally like if you implement solution in one block, so it will be difficult to identify like which line of code, group of code is basically solving which problem. If you have different, different functions because in functions we provide some name as well to those functions. So it will be very easy to identify like which function will be doing what particular operation. Then we have reusability of code. So if you want to reuse codes that you have implemented, so those codes you can reuse with the help of functions, right? Debugging becomes easy and fast because you have different, different blocks. So generally whenever there is an error, so it comes in particular functions. So you just need to check that particular block of code, not the complete code, right? And compilation and execution of the program becomes faster, right? So last line is specific to certain programming languages. Okay, so now jumping on to the next slide. So here we will be understanding what all different parts we need to implement when we write a function, okay? So if you are implementing a function in any of the programming language, so you mainly focus on what function name should be and what parameters it will be receiving as an input or what output function can produce to another functions, right? So if we have more than one function implemented, so like all the functions can receive some inputs and all the functions can provide some output so that those output can be received as an input from other functions, right? So this way, this function implementation works. So we have three statements. One is declaration, one is calling and one is definition, right? So declaration, we need to provide in few programming languages like in C programming, you need to write declaration, but in some of the programming languages like C++, Java or object-oriented programming languages, you don't need to write the declaration. Declaration is also known as signature of the function, right? So here we have return type, we have function name and we have argument type. So argument type basically works as input, then function name so that we can identify what problem we are solving and return type will be basically working as an output. So other than declaration, calling and definition will be available in every programming language, right? So here we need to call the function, we need to provide some arguments. The result will be stored in particular variable. So this is calling statement. Calling is very important because if you write a calling statement, then only it says like you, whatever function you have implemented that will be executing. Then we have definition, in the definition we write the actual piece of code that will be working whenever you are calling a function, okay? So these three blocks or these three statements basically, we need to implement if we are implementing function-oriented approach. So now I'm going to implement these problems one by one so that you can understand how we can write a function. So first I will be solving like how we can calculate factorial of a number. So let's jump on to the online compiler. So here first of all, I'm going to declare a function factorial and int. So here first int is return type, then factorial is function name and next int is input type, right? So this int, this int which we used in parenthesis will be working as input and factorial is function name and this int is basically return type, okay? So I hope you are able to understand this and I'm breaking it into two comment lines so that it will be readable. Now here I need to implement the code. So basically for factorial calculation, I need to receive input from user as well. So here what I'm going to do, I'm creating a variable and and variable f, then print f, enter a number, then we need to scan it. Now here I will be calling the function and whatever result I will be receiving into f, I'm going to show it. So factorial equals two and the result we are receiving into f. So this is basically function call, which is very much important. This is basically function call. So in function call we are calling this factorial function, we are passing n as input. So here n is input and the result will be received into f variable as an output. Now after implementing this main function, after that we need to define another function which will be factorial and here we'll be receiving the input, right? Then if you need a variable for calculation, a variable for loop implementation, so you can declare them. So this is another block of code. So in this block of code, if you need any variable, so you can declare it. So this way I implemented for loop and here I'm going to write the logic. So f equals two f into i, so factorial logic we already understood in the previous session. So this loop will be repeating n times, so let's say n is five. So this loop will be repeating five times. Five iterations will be there and every time f will be multiplied with i and the result will be stored into f variable. And once this loop is completed, so what we can do, we can write return f. So whatever value will be available into this f variable, it will be returned here. So execution will begin from main, from main whenever you will be calling this function. So from calling control will transferred to this definition. So from calling whatever value of n you are passing, so that value will be stored into this n variable which is available in the definition. And with the help of this loop, the calculations will be done and the result will be available into this f variable. And whenever we write this return f, so return f means the value will be returned and it will be stored into this f variable which is available at line number 10 and this statement is basically function call. And here this you can call as function definition. This is function definition. So this way, if you want to implement more function you can do, right? So this way we implemented one function. So I see Prince, welcome Prince. I recognize like you are from PIT college. I hope now you are doing some job. So now jumping on to the next problem. So here, what we need to do? We need to read two numbers and perform addition and subtraction, okay? So before implementing the second problem, let me implement one more problem that is printing reverse of a number. So whatever number we are receiving here and calculating factorial for the same number I need to print the reverse, right? So what I can do? Here I can declare one more function named as reverse and this reverse function will be receiving an integer and it will be like giving an output in form of integer, okay? Now here I am going to call that function. So I'm creating one more variable that is r and here I'm going to write r equals to reverse of n. So whatever value will be available into n that will be passed into reverse function that we are going to implement next. And then we can write print if reverse equals to percent d and r, okay? So here we have another function call. Here we have another function call. So in the same code, we are going to define one more function. So in the same program, like you have main function, which is calling other functions, then we have factorial function and then I'm going to write one more function that is reverse, okay? Now here we can write the logic of reverse, okay? So this way we basically implement the function. So here I'm creating one variable. So a directly then s is zero. Now here I am implementing while loop while n not equals to zero. So a equals to n modulus 10. Then s equals to s into 10 plus a then n equals to n divided by 10. So this way this while loop will be calculating the result and then I can write return s. So this time result will be available into s variable. So that is why I'm returning s variable from here. So this is my third function. So again you can say this is function definition, okay? So this is one function that I defined here reverse. This is another function that I defined here factorial and this is main function that we already implement, okay? So this way like whenever you are working with any programming language, you need to implement functions, okay? We can add more functions here as per the need. Now I'm going to show you if we want to receive more than one values as input, so how we can do that? So if you read the program, read two numbers and perform addition and subtraction. So I'm going to implement this problem and we have two more problems like Palindrom and Armstrong. So these you can do as part of your practice, okay? So now I'm going to modify this code so that I can show you other implementation. So I am just removing this code, which I already demoed. Now here I'm going to implement declaration of some functions. So we need to receive two inputs because for some we need two values and for subtraction as well. So here two inputs will be passed in form of integer. Now here I am creating variable like N1, N2, then R1, R2. So R1 will be receiving first result, some R2 will be storing second result that is subtraction. Then here I'm going to enter two numbers and then we can call the function. So R1 equals to sum of N1, N2, right? And then we can write printf. So it will display some on output screen, right? So here we are passing two inputs. So this is again function call. This is again function call and then we can have one more function call for subtraction passing the same values. Now here what we can do, we can also reuse R1 variable here also because we are calling functions separately and before calling another function we are displaying the result as well. So you can reuse the variables as per the need. So it should be subtraction and here I am going to display R2. So this way here you can see I am performing another function call, right? So this is main function now. I need to define two definitions. So first time writing some. So here I'm receiving int A into B. Now you might be thinking we are passing N1, N2 from here but we are receiving into A and B. So this is possible. From function call you can use different variable names. In definition you can use different variable names. So here I'm writing C equals to A plus B and then return C. Okay, so this way this function is implemented. So again you can see we are returning C variable and the result will be received into R1 so that is also possible. Then next is subtraction. So subtraction will be receiving two values and here I can write minus sign. So this way some and subtraction two functions are implemented, okay? So I hope you are able to understand these function implementation. Now I'm going to run this code so that I can provide two values. So 10 and five I am putting as an input and you can see some is 15 and subtraction is five. So both the functions are working properly as I call them in the main function. So this way I hope you understood like how we can implement function and this function topic is very much important because in every programming language you need to implement functions. You need to pass some inputs as parameter. You need to receive output. So all those things you need to implement. So practice all these functions properly. Implement these programs with the help of function. So three problems I already showed you how we can implement. Two problems last two problems I didn't solve. Those you will be implementing as part of practice. Okay so that you will be able to understand. So this way in short I demoed you how we can implement function. It is very easy and the problems which we are implementing we already discussed. So logic will be same. The thing you need to understand how we can break your code into pieces and you can create separate function for particular problem, okay? So this is all for today's session. In next session I will be discussing new topics. So I think in one or two sessions the important topics which are related to C programming will be completed and then I will be explaining C++ related sessions, right? So C++ is very much important if you are trying to learn Java and Apex programming. So C++ basically helps you to build a base to write code in object oriented programming way. So maybe in 15, 20 sessions those C++ related topics will be completed so that you can create a good base for you. Okay, so this is it for today's session. Thank you so much for attending. And if you're watching the recording thanks to you as well. I hope whatever I demoed you it makes sense and you are able to understand. Thank you everyone. See you in the next session.