Day 22 | Learn C & C++ Coding Live & Free with Sanjay Gupta | Sanjay Gupta Tech School

Sanjay Gupta Tech School
28 followers
Live Session Tracker: https://docs.google.com/spreadsheets/d/1a-bxGs9nw37zpc2QyGCuoPMt3obFKMwVHdZSB4rHw1U/edit#gid=1126625766
Youtube Playlist: https://www.youtube.com/watch?v=5au6vtXV-7o&list=PL-gW8Fj5TGrpfpr2uwkwUTcKqx-PrFBDw
Exercise Sheet Link: https://docs.google.com/document/d/1Slzc8Eub-VU5EN-rz29hbAwKuztv2PCqOsCADZnaTBc/edit?usp=sharing
#cprogramming #coding #clanguage
Hello everyone, I am Sanjay Gupta, I welcome you on Sanjay Gupta Tech School.
So after a few days, like I am back on this C and C++ coding bootcamp.
So I was busy with other bootcamps, so was not able to continue session.
So now we have day 22 for this bootcamp and in today's session, I will be explaining
a very important topic which is functions in C.
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.
So I am 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 of 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 am 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, so 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.
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, so how you can do that.
So first I will be covering the theoretical concepts related to function and then we will
be implementing some programs.
So basically a function is a collection of 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 built 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, some are predefined or library functions and some are user defined functions that we
implement as per our need.
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 function,
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 ok.
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 ok.
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 functions 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 am 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 am 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 am 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 am going to do, I am creating a variable
n and variable f then printf, 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 am going to
show it, so factorial equals to 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 will 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 am going to write the
logic, so f equals to 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 5, so this loop will
be repeating 5 times, 5 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 transfer 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, 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, 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, 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 am creating one more variable that is r and here I am 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 printf reverse equals to %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 am 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 am creating one variable, so a directly then s is 0, now here I am implementing while
loop while n not equals to 0, 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 am 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 am 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 am going to implement this problem and we have two more problems like palindrome
and armstrong, so these you can do as part of your practice, okay, so now I am 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 am going to implement declaration of some
functions, so we need to receive two inputs because for sum 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 sum,
r2 will be storing second result that is subtraction, then here I am 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 sum 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 I am writing sum, so here I am receiving int
a, int 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 am 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 sum and subtraction two functions are implemented,
okay, so I hope you are able to understand these function implementation, now I am going
to run this code, so that I can provide two values, so 10 and 5 I am putting as an input
and you can see sum is 15 and subtraction is 5, so both the functions are working properly
as I called 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 session 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 may be 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 are 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.
